Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending ObjC generic in Swift 3

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?

like image 523
0x416e746f6e Avatar asked Sep 20 '16 21:09

0x416e746f6e


1 Answers

Details

xCode 8.3.1, swift 3.1

Sample

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"

Usage

NSLog(@"%@", [set foo].str);
NSLog(@"%@", [set bar].str);
like image 55
Vasily Bodnarchuk Avatar answered Nov 04 '22 17:11

Vasily Bodnarchuk