对象符合协议Swift

时间:2015-02-03 05:40:53

标签: ios objective-c swift

任何人都可以帮助找到如何在Swift

中写这个
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];

上述ObjectiveC代码表示object类型的NSMutableDictionary符合协议FBOpenGraphObject。我试图快速代表这个

var object:FBOpenGraphObject, Dictionary = FBGraphObject.openGraphActionForPost()

// specify that this Open Graph object will be posted to Facebook
object.provisionedForPost = true   //This will not work

但这不合适,我无法为对象分配任何值。帮助我弄清楚如何在swift中表示符合协议的对象

1 个答案:

答案 0 :(得分:3)

也许你可以这样做:

var obj = FBGraphObject.openGraphActionForPost()
if let fbObj = obj as? FBOpenGraphObject {
    // specify that this Open Graph object will be posted to Facebook
    fbObj.provisionedForPost = true // use fbObj for FBOpenGraphObject-specific members
    obj["title"] = "Test hot spot" // use obj for Dictionary-specific members
    // Both obj and fbObj vars point to the same object
}