在Swift 3中扩展ObjC泛型

cou*_*elk 14 generics parameters objective-c swift swift3

Swift 3附带SE-0057实现,除其他外,意味着:

默认情况下,参数化Objective-C类的扩展不能以任何方式引用类型参数.例如:

extension MySet {
  func someNewMethod(x: T) { ... } // error: cannot use `T`.
}
Run Code Online (Sandbox Code Playgroud)

... MySet在ObjC中声明的地方@interface MySet<T : id<NSCopying>> : NSObject.

一切都很清楚(甚至还有某种解决方法).但是,尽管我没有使用我想要扩展的ObjC类中的任何类型参数,但以下内容仍无法编译.我只使用另一个不相关的Swift类作为扩展方法的返回参数:

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
Run Code Online (Sandbox Code Playgroud)

这是一个错误吗?或者我错过了什么?

Vas*_*huk 2

细节

xCode 8.3.1、斯威夫特 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;
Run Code Online (Sandbox Code Playgroud)

我的设置.m

#import "MySet.h"

@implementation MySet

@end
Run Code Online (Sandbox Code Playgroud)

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() }
}
Run Code Online (Sandbox Code Playgroud)

桥接头

#import "MySet.h"
Run Code Online (Sandbox Code Playgroud)

用法

NSLog(@"%@", [set foo].str);
NSLog(@"%@", [set bar].str);
Run Code Online (Sandbox Code Playgroud)