How to bridge throwable Swift initialiser with Objective-C code? - objective-c

Let's say we have a Swift class with an initializer which can throw an error. This class must be used in Objective-C codebase (NSObject subclass):
import Foundation
enum EvenError : ErrorType {
case NonEvenNumber
}
class FooEven : NSObject {
var evenNumber : UInt
init(evenNumber: UInt) throws {
guard evenNumber % 2 == 0 else {
throw EvenError.NonEvenNumber
}
self.evenNumber = evenNumber
}
}
Produces compilation warning:
<unknown>:0: warning: no calls to throwing functions occur within 'try' expression
I can work around this warning in 2 ways:
by replacing throwable initialiser (init... -> throws) with failable one (init?)
giving up on subclassing from NSObject
Yet this way I will:
loose information about an error causing the exception,
have to make instances of FooEven optionals and / or handle many: if let fooEven = FooEven.init() {...} statements
... or I will not be able to use it in existing Objective-C code:
None of the above satisfies my needs / requirements.
Is there an other way to remove that warning without loosing information about the error?

Another workaround is to add a throwing convenience initializer that calls your non-throwing designated initializer.

This is a bug in the Swift compiler and is fixed in Xcode 8. When you upgrade Xcode, this warning will go away.
In the meantime, you can call super.init() at the end of your initialiser and this will also make the warning go away.

Related

Passing a Swift generic protocol argument to an Obj-C “Protocol” function

I have a class that needs to pass a Protocol to an Obj-C function. I have a constructor that takes the Protocol, but as the class is a generic that also takes the same protocol, I thought I could optimise it. However, if I try to use the generic value when calling the function, it fails to compile. I've tried various combinations of ".self" and ".Type" and ".Protocol", both in the code and in the generic argument, and nothing works. Is there any way to achieve this?
This is a Playground project to show the problem.
import Foundation
#objc protocol TestProtocol {
}
class Test<P> {
init() {
test(p: P.self) // Fails to compile with: Cannot convert value of type 'P.Type' to expected argument type 'Protocol'
test(p: TestProtocol.self) // Compiles
}
func test(p: Protocol) {
}
}
let c = Test<TestProtocol>()

Swift class properties not initialized when constructed by Objective C code

I'm attempting to create a class in Swift 3 to implement a Cordova plugin. I have this building and running, but the application crashes whenever any properties of the class are accessed. I've tried two ways of initializing the class:
#objc(DSFMediaCentre)
class DSFMediaCentre : CDVPlugin
{
var players = [UUID:DSFPlayerHandler] ();
...
}
and
#objc(DSFMediaCentre)
class DSFMediaCentre : CDVPlugin
{
var players :[UUID:DSFPlayerHandler];
override init () {
players = [:];
}
...
}
However, when my players property is used, the result is a EXC_BAD_ACCESS exception, with an address that looks like a null pointer dereference.
The object is being created by Objective C code, which is a language I have no familiarity with at all, but I think this is the line that creates it:
obj = [[NSClassFromString(className)alloc] initWithWebViewEngine:_webViewEngine];
The CDVPlugin class contains a comment stating that initWithWebViewEngine should not be overridden (and indeed I do not seem to be able to override this method, because while it is declared in the CDVPlugin.m file, it isn't mentioned in CDVPlugin.h, so the Swift compiler doesn't seem to know about it), but rather initialization code should be placed in a method called pluginInitialize instead. However, if I do that I get a compiler error ("Class DSFMediaCentre has no initializers").
Furthermore, if I put my init() method back in and set it to call pluginInitialize(), like this:
override init () {
super.init(); // necessary otherwise next line is an error
pluginInitialize();
}
override func pluginInitialize() {
players = [:];
}
the error then changes to "Property 'self.players' not initialized at super.init call".
How do I make this class initialize correctly?
You have a mismatch between the strict initialization system required by the language and the procedure used by the framework you're working with.
Swift demands that a) properties be initialized as part of object construction, and b) that construction be chained to the type's supertype. But the CDVPlugin type is doing the construction on your behalf; you don't have the ability to customize it. (This makes more sense in ObjC, because it doesn't have the same compile-time restrictions as Swift.)
The situation is similar to unpacking an object from a nib file. In that case too, because it's the nib loading system that's constructing your object, you don't have the ability to customize the initializer. Your type will always be constructed by init(coder:). In a certain sense, your initialization point moves further down, to awakeFromNib(), and among other things, that forces outlets to other objects in the archive to be declared as optional, usually implicitly unwrapped.
The same solution should avail you here. You should consider pluginInitialize() to be your initialization point. The language then requires that properties be optional, since they are not filled at its initialization point. Therefore, make the property an IUO:
#objc(DSFMediaCentre)
class DSFMediaCentre : CDVPlugin
{
var players :[UUID:DSFPlayerHandler]!
override func pluginInitialize() {
players = [:];
}
}
and all should be well.
The other solution is to use lazy keyword
lazy var players :[UUID:DSFPlayerHandler] = [:]
So, you don't need to initialize players in initializer but still make sure players always non-nulable

Typedef Return-Type in Objective-C does not work in Swift

I want to use a Objective-C class in my Swift project and have imported the files and Xcode created the bridge header file and everything is cool... except:
The Objective-C class defines a callback type for a function
typedef void (^SSScanManagerCallback)(BOOL success, NSError *error, NSArray *scannedURLs);
And uses the type in the function declaration
- (void)scanSync:(SSScanManagerCallback)callback; // Synchronous scan.
The class in question is the following: https://github.com/counsyl/scanstream/blob/master/ScanStream/SSScanManager.h#L16
If I then want to use the class in Swift:
let scanManager = SSScanManager();
scanManager.scanSync({(_ success: Bool, _ error: Error, _ scannedURLs: [Any]) -> Void in
if !success {
// ...
}
});
I get the following error:
Cannot convert value of type '(Bool, Error, [Any]) -> Void' to expected argument type 'SSScanManagerCallback!'
Update: Even if I try to set the argument type like so:
scanManager.scanSync({(_ justATry: SSScanManagerCallback!) -> Void in
});
I get the error:
Cannot convert value of type '(SSScanManagerCallback!) -> Void' to expected argument type 'SSScanManagerCallback!'
But how would I set the type to just 'SSScanManagerCallback!' as requested in the error message?
Interestingly, it appears that Swift (tested with 3.0.2) now imports Objective-C block argument types without any nullability annotations as strong optionals (previously they were imported as implicitly unwrapped optionals). I can't seem to find the documentation for this change though.
So in your case, the correct signature is:
scanManager.scanSync {(success: Bool, error: Error?, scannedURLs: [Any]?) -> Void in
// ...
}
But never write it like this, always let Swift infer the argument types where it can, it solves these kinds of type-mismatch problems for you.
scanManager.scanSync { success, error, scannedURLs in
// ...
}
Now you can ⌥ click on the closure arguments and Xcode will tell you the type that Swift infers them to be.

How do I implement Sequence (to allow Swift's for-in syntax) from Objective-C?

I'm writing an API in Objective-C and would like it to play nicely in Swift. I'm having trouble getting "for..in" syntax working though. I think I need to implement the Sequence protocol, but I can't find any examples doing this from Objective-C. Just referencing Sequence gives me error: no type or protocol named 'Sequence'. Is there a special #import to get access to it or something?
I tried implementing the NSFastEnumeration protocol, thinking maybe it'd magically convert to Sequence in Swift, but that didn't work.
///// Obj-C Code
#interface Foo : NSObject<NSFastEnumeration>
...
#end
///// Swift Code
var foo: Foo = Foo()
// ERROR: Type 'Foo' does not conform to protocol 'Sequence'
for y in foo {
print("Got a y.")
}
EDIT: It looks like inheriting from NSEnumerator gets me closer, but doesn't quite work either:
///// Obj-C Code
#interface Foo : NSEnumerator<NSString *>
...
#end
///// Swift Code
// ERROR: 'NSFastEnumerationIterator.Element' (aka 'Any') is not convertible to 'String'
for y: String in foo {
print("Got \(y)")
}
EDIT 2: I still don't have a good solution and have logged a bug: https://bugs.swift.org/browse/SR-2801
The Swift extension of Foundation includes some support for making classes that adopt NSFastEnumeration also support the Swift Sequence protocol... but not automatically.
One way to do it is to extend your ObjC type in Swift and pass through to the NSFastEnumerationIterator type:
extension Foo: Sequence {
public func makeIterator() -> NSFastEnumerationIterator {
return NSFastEnumerationIterator(self)
}
}
NSFastEnumerationIterator (and all forms of ObjC enumeration) are type erasing, though, so they don't provide any insight on the element type you're iterating through. That means that you can do this (after adding the above extension):
var foo: Foo = Foo()
for y in foo {
print("Got a y.")
}
... but the static type of y is always Any. If you want typed access to the members of foo, you'll need a cast or a filtered loop:
for y in foo where y is String {
print("Got \(y)")
}
Sadly, if your class adopts ObjC generics, there doesn't seem to be a way to make this work — you'll get an error "Extension of a generic Objective-C class cannot access the class's generic parameters at runtime", even if you adopt the runtime type introspection method(s) in SE-0057. For non-generic ObjC classes you're good, though.

Bridging from Objective C to Swift with PromiseKit

Using PromiseKit 2.0 with Swift 1.2, I'm trying to use a PMKPromise that was created in Objective C from Swift.
Objective C code:
#interface FooTest : NSObject
+ (PMKPromise *)promise;
#end
Swift code (I've tried a number of variations, none of which work. This one is closest to the example given at http://promisekit.org/PromiseKit-2.0-Released/):
FooTest.promise().then { (obj: AnyObject?) in
self.obj = obj
}
Compiler error: Cannot invoke 'then' with an argument list of type '((AnyObject?) -> _)'
This doesn't work either:
FooTest.promise().then { (obj: AnyObject?) -> AnyPromise in
return AnyPromise()
}
Similar error: "Cannot invoke 'then' with an argument list of type '((AnyObject?) -> AnyPromise)'"
There are two different promise classes in PromiseKit, one for Swift (Promise<T>) and one for ObjC (AnyPromise). The Swift ones are generic and Objective-C cannot see generic classes, so this is why there are two.
If Foo.promise() is meant to be used in both ObjC and Swift then you are doing the right thing. If however you only intend to use this promise in Swift then I suggest rewriting it as a Promise<T>.
To use an Objective-C AnyPromise (PMKPromise is a deprecated alias for AnyPromise: prefer AnyPromise) in Swift code you must splice it into a an existing chain.
someSwiftPromise().then { _ -> AnyPromise in
return someAnyPromise()
}.then { (obj: AnyObject?) -> Void in
//…
}
There should be a way to start from an AnyPromise, probably I will add this later today:
someAnyPromise().then { (obj: AnyObject?) -> Void in
//…
}
Expect a 2.1 update. [edit: 2.1 pushed with the above then added]