2017-05-04 8 views
1

私は迅速な構造体を_ObjectiveCBridgeableに準拠させようとしていますが、私はプロトコルを満たすために他に何が必要なのか分かりません。以下は私の構造体と_ObjectiveCBridgeableの適合です。私は何かが欠けているが、それが何であるか分からない。 //にObjCswift 3 _ObjectiveCBridgeable適合

@interface thing : NSObject 
@property (readonly) id contents; 
-(instancetype)initWithContents:(id)contents; 
@end 
@implementation thing 
- (instancetype)initWithContents:(id)contents { 
    if ((self = [super init])) { 
     _contents = contents; 
    } 
    return self; 
} 
@end 

答えて

1

アンダースコアがあなたに伝えたよう

struct Box { 
    let contents: Any 
} 

extension Box: _ObjectiveCBridgeable { 
    typealias _ObjectiveCType = thing; 

    init(fromObjectiveC source: _ObjectiveCType) { 
     contents = source.contents 
    } 

    static func _isBridgedToObjectiveC() -> Bool { 
     return true 
    } 
    static func _getObjectiveCType() -> Any.Type { 
     return _ObjectiveCType.self 
    } 
    func _bridgeToObjectiveC() -> Box._ObjectiveCType { 
     return thing(contents: self.contents) 
    } 

    static func _forceBridgeFromObjectiveC(_ source: Box._ObjectiveCType, result: inout Box?) { 
     result = Box(contents: source.contents) 
    } 

    static func _conditionallyBridgeFromObjectiveC(_ source: Box._ObjectiveCType, result: inout Box?) -> Bool { 
     _forceBridgeFromObjectiveC(source, result: &result) 
     return true 
    } 
} 

_ObjectiveCBridgeableはプライベートです。その目的は、Objective-Cオブジェクト型をSwift型に橋渡しする特定のニーズに対応することです。あなたは自分のタイプのためにそれを採用することはできません。 「コンパイラの魔法の下で」動作します。

公開バージョンを提供するためにproposal on the tableがありますが、まだ実装されていません。

+0

しかし、あなたはhttp://stackoverflow.com/questions/38837198/define-struct-that-is-treated-like-a-class-in-swiftを見てみたいかもしれません(そのうちの質問はおそらく、複製)。 – matt

+0

まあ、Xcodeでは、 ''(Box._ObjectiveCType?) - > Box 'タイプの関数' _unconditionallyBridgeFromObjectiveC 'を実装する必要があると言います。しかし、私はどのように困惑しています。 – DerrickHo328

+0

私はそれが私的だと気づいていますが、それ以前のバージョンのswiftではうまくいきました。 – DerrickHo328