Swift 3 comes with SE-0057 implemented that, among other things, means that:
By default, extensions of parameterized Objective-C classes cannot reference the type parameters in any way. For example:
extension MySet { func someNewMethod(x: T) { ... } // error: cannot use `T`. }
... where MySet
is declared in ObjC as @interface MySet<T : id<NSCopying>> : NSObject
.
All that is clear (and there is some sort of workaround possible even). However, the following does not compile despite that I am not using any type parameters from ObjC class that I am trying to extend. I am only using another unrelated Swift class as a return parameter to the extension method:
class Foo { }
struct Bar { }
extension MySet {
func foo() -> Foo { return Foo() } // Both produce: Extension of a generic
func bar() -> Bar { return Bar() } // Objective-C class cannot access the
} // class's generic parameters at runtime
Is this a bug? Or am I missing something?
xCode 8.3.1, swift 3.1
MySet.h
#import <Foundation/Foundation.h>
@interface MySet<T : id<NSCopying>> : NSObject
@end
typedef struct ObjCStruct
{
__unsafe_unretained NSString* str;
int num;
} ObjCStruct;
MySet.m
#import "MySet.h"
@implementation MySet
@end
MySetExtension.swift
import Foundation
class Foo: NSObject {
var str = "Foo"
var num = 1
}
extension ObjCStruct {
init() {
num = 2
str = Unmanaged.passRetained("ObjCStruct")
}
}
extension MySet {
func foo() -> Foo { return Foo() }
func bar() -> ObjCStruct { return ObjCStruct() }
}
Bridging-Header
#import "MySet.h"
NSLog(@"%@", [set foo].str);
NSLog(@"%@", [set bar].str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With