ios - Swift 中的 NSSecureCoding(Facebook SDK)

标签 ios facebook swift nssecurecoding

我正在尝试将一段 Objective-C 代码转换为 Swift 代码。

objective-C :

#import "SUCacheItem.h"

#define SUCACHEITEM_TOKEN_KEY @"token"
#define SUCACHEITEM_PROFILE_KEY @"profile"

@implementation SUCacheItem

+ (BOOL)supportsSecureCoding
{
return YES;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
SUCacheItem *item = [[SUCacheItem alloc] init];
item.profile = [aDecoder decodeObjectOfClass:[FBSDKProfile class]   forKey:SUCACHEITEM_PROFILE_KEY];
item.token = [aDecoder decodeObjectOfClass:[FBSDKAccessToken class] forKey:SUCACHEITEM_TOKEN_KEY];
return item;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.profile forKey:SUCACHEITEM_PROFILE_KEY];
[aCoder encodeObject:self.token forKey:SUCACHEITEM_TOKEN_KEY];
}

@end

我把这段代码翻译成这样:

class CacheItem: NSObject, NSSecureCoding {

let CACHEITEM_TOKEN_KEY = "token"
let CACHEITEM_PROFILE_KEY = "profile"
var profile: AnyObject
var token: AnyObject

func supportsSecureCoding() -> Bool {
    return true
}

required init(coder aDecoder: NSCoder) {
    var item = CacheItem(coder: aDecoder)
    item.profile = aDecoder.decodeObjectOfClass(FBSDKProfile.self, forKey: CACHEITEM_PROFILE_KEY)!
    item.token = aDecoder.decodeObjectOfClass(FBSDKAccessToken.self, forKey: CACHEITEM_TOKEN_KEY)!
}


func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(self.profile, forKey: CACHEITEM_PROFILE_KEY)
    aCoder.encodeObject(self.token, forKey: CACHEITEM_TOKEN_KEY)
}   
}

这给我一个错误:类型“CacheItem”不符合协议(protocol)“NSSecureCoding”

我在这里错过了什么?

提前致谢!

最佳答案

supportsSecureCoding 函数需要在类级别:

class func supportsSecureCoding() -> Bool {
    return true
}

关于ios - Swift 中的 NSSecureCoding(Facebook SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806449/

相关文章:

objective-c - 状态栏未隐藏在 Storyboard 中

ios - 内存未在 ios View 层次结构中释放

swift - Swift 中的 AFNetworkReachabilityManager

ios - 超出自定义单元格边界的披露指示符

ios - SMS Message Composer 在联系人位置显示一个洞

ios - 第二个带有 UISearchController 的推送 View Controller 在 UINavigationBar 中没有接收到触摸

google-app-engine - Facebook 连接 GWT 和 App Engine (Java)

email - Facebook 电子邮件始终通过 FQL 和 RestFB 返回 null

jquery ajax请求仅在firefox中有效

swift - 使用 Swift 2.0 (OS X) 在语音合成过程中推进进度条