我正试图把Objective-C代码转换成Swift代码。
目标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”类型不符合协议“NSSecureCoding”
我错过了什么?
提前谢谢!

最佳答案

supportsSecureCoding函数需要在类级别:

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

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

10-12 21:28