Using Objective-C bitmask in Swift - ios

I'm porting some objective-c code to Swift. I need to check a bitmask but I'm getting an error:
MCOMessageFlag is not convertible to Bool
How should I be accessing this from Swift.
self.message.flags = NSNumber(int: self.message.flags.intValue & ~MCOMessageFlag.Flagged)
I'm using the MailCore2 library.
Edit:
Here is the line of objective-c I'm porting:
[self.message setFlags:[NSNumber numberWithInt: self.message.flags.intValue & ~MCOMessageFlagFlagged]];
Edit:
RawOptionSetType no longer implements BooleanType by default, so standard bitmask checks (if opt & .Option {...) only work if you add BooleanType protocol conformance manually.
I'm guessing this is my issue.

You can use .rawValue to get the value you need:
MCOMessageFlag.Flagged.rawValue

Related

How to call a swift method which parameter is a protocol from Objective-C

How to call a swift method which parameter is a protocol from Objective-C.
Class A
{
func sendMessage<Serializable: Encodable>(message:Serializable){
let jsonData = try! JSONEncoder().encode(message)
}
}
Objective-C code
A a = [[A alloc] init];
[a sendMessage] ;// Not Available
How can I call this method from Objective-C.
I don't think sendMessage will ever appear in compiled TargetName-Swift.h because Generics and Encodable protocol is not available in Objective-C.
Clearly when you use
sendMessage<Serializable: Encodable>(message:Serializable)
You employ Generics as well as Encodable protocol which is only available in Swift not in Objective-C
When you try to access the Swift functions in your Objective-C files, compiler creates a Objective-C equivalent/ counterpart of your Swift functions hence you see the signature for those methods in your TargetName-Swift.h files.
But Generics and Encodable protocol are not available in Objective-C so it can't translate your Swift function using Generics and Encodable protocol to Objective-C equivalent. Hence You can never call such Swift methods from Objective-C files

Disambiguate overriden method written in ObjC used in Swift

Complete code: https://github.com/minbi/ambiguous-swift
Is there a way to disambiguate use of an ObjC method from Swift 3?
I need to make the modification in ObjC so that the usage in Swift is as seamless as possible.
Objective C bridged to Swift
#interface A
+ (instancetype)singletonA;
#end
#interface B: A
+ (instancetype)singletonB;
#end
Usage in Swift 3.
I don't understand why this would be ambiguous. Doesn't the fact that I call the method from B "disambiguate the usage even a little?
var b: B! = B.singleton()
Error
Ambiguous use of 'singleton()'
Apparently this is a known bug. Swift does not handle overriding methods or properties well.
"Ambiguous use of 'propertyName'" error given overridden property with didSet observer

Swift 2.1 ErrorType does not conform protocol RawRepresentable

I have declared error type
enum UserServicesError: ErrorType {
case UserNotLogged
}
but I get an error
Argument type 'UserServicesError' does not conform to expected type 'ErrorType'
Type 'UserServicesError' does not conform to protocol 'RawRepresentable'
Any idea y? Official documentation says that this declaration is sufficient.
Apple Swift 2.1 Error handling documentation
I've finally figure it out. I had declared enum ErrorType in objective-c shared class from pre-swift ages.
typedef NS_ENUM(NSUInteger, ErrorType) {
...
};
I would expect to see a Redefined type error rather than does not conform to protocol 'RawRepresentable'
Are you by any chance using UserServicesError with Cocoa classes? If so, the Errors subsection of Using Swift with Cocoa and Objective-C guide suggests it should be declared like this:
#objc enum UserServicesError: Int, ErrorType {
case UserNotLogged
}
The #objc designation is needed for any protocols that interact with Objective-C Cocoa objects. Conforming to Int (or some other RawRepresentable-conforming type) gets you RawRepresentable conformance automatically (vs. leaving it a pure Swift enum).
I hope this helps. If it does, let me know if it was one, the other, or both needed to fix it. I'm curious. :-)

NSSet setByAddingObject in Swift 1.2

What is equivalent of setByAddingObject: in Swift. I have an NSSet property from Objective-C in Swift. But since Apple released Xcode 6.3 I have an error in this place:
Example:
NSSet *set;
object.set = object.set.setByAddingObject("text/html")
It produces error:
'Set<NSObject>' does not have a member named 'setByAddingObject'
Any idea, how to fix this? insert method doesn't work also.
Swift 1.2 added its own Set type, which is bridged over to Swift from Objective-C in a similar fashion to NSArray/Array and NSString/String. Try object.set.insert("text/html").

How to use the CoreAudio API in Swift

I am in the process of migrating my streaming audio engine to swift. i am finding it difficult to use the C Audio API in swift.
I have a problem with AudioFileStreamOpen api where it takes 2 C functions as a parameter. I don't know how to use this API is swift.
AudioFileStreamOpen(self as UnsafePointer<()>, propertyProc, packetProc, kAudioFileMP3Type, audioStreamId)
I have defined the callback method as below for this API. But i am getting the compilation error.
func propertyProc(inClientData: UnsafePointer<()>,inFileStreamId: AudioFileStreamID,inPropertyId: AudioFileStreamPropertyID,ioFlags: UnsafePointer<UInt32>) -> Void {
.....
}
func packetProc(inClientData: UnsafePointer<()>,inNumberOfBytes: UInt32,inNumberOfPackets: UInt32, ConstUnsafePointer<()>, inPacketDescriptions: UnsafePointer<AudioStreamPacketDescription>) -> Void {
.....
}
Any help is appreciated to correctly define this C API in swift
You can't (currently) use an API requiring a C callback pointer from pure Swift code. Calling Swift functions or methods using a C function pointer is not supported by the current beta 4 language implementation, according to replies in the Swift forum at devforums.apple.com
UPDATE: The above answer is obsolete as of Swift 2.0
One alternative is to put some small trampoline C callback functions in an Objective C file, which can interoperate with Swift, and have those C functions in turn call a block or closure, which can be in Swift code. Configure the C callbacks with your Swift closures, and then pass those C callbacks to the CoreAudio functions.
I don't know much about Audio API, however, you should replace UnsafePointer by a pointer to an Object. for example:
var clientData : AnyObject?
var listenerProc : AudioFileStream_PropertyListenerProc = AudioFileStream_PropertyListenerProc.convertFromNilLiteral()
var packetsProc : AudioFileStream_PacketsProc = AudioFileStream_PacketsProc.convertFromNilLiteral()
var audioFileTypyeId : AudioFileTypeID = 0
AudioFileStreamOpen(&clientData, listenerProc, packetsProc, audioFileTypyeId, &streamId)
the initialization code for listenerProc, packetsProc or other variables is just to by-pass the compiler error.
To your situation, try to replace 'self as UnsafePointer<>' by '&self'. However 'self' must be something that can be converted to compatible data type.
https://developer.apple.com/library/prerelease/ios/documentation/MusicAudio/Reference/AudioStreamReference/index.html#//apple_ref/c/func/AudioFileStreamOpen

Resources