我在翻译这段Objective-C代码时遇到了困难:
- (void)_accessTokenChanged:(NSNotification *)notification
{
FBSDKAccessToken *token = notification.userInfo[FBSDKAccessTokenChangeNewKey];
if (!token) {
[self _deselectRow];
} else {
SUProfileTableViewCell *cell = (SUProfileTableViewCell *)[self.tableView cellForRowAtIndexPath:_currentIndexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSInteger slot = [self _userSlotFromIndexPath:_currentIndexPath];
SUCacheItem *item = [SUCache itemForSlot:slot] ?: [[SUCacheItem alloc] init];
if (![item.token isEqualToAccessToken:token]) {
item.token = token;
[SUCache saveItem:item slot:slot];
cell.userID = token.userID;
}
}
}
分为:
func accessTokenChanged(notification: NSNotification) -> Void {
var token: AnyObject? = notification.userInfo![FBSDKAccessTokenChangeNewKey] as! FBSDKAccessToken
if (token == nil) {
self.deselectRow()
} else {
let cell = self.usersTable.cellForRowAtIndexPath(currentIndexPath!) as! FBUserCell
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
var slot: Int = self.userSlotFromIndexPath(currentIndexPath!)
var item: CacheItem = Cache.itemForSlot(slot)!
if (item.token.isEqualToAccessToken(token) == false) {
item.token = token
Cache.saveItem(item, slot: slot)
cell.userIDLabel = token?.userID
}
}
}
我在if语句中收到两个令牌的错误:找不到' =='接受提供的参数。如果我在" item"之前加上感叹号,我会得到同样的错误。在if语句中,然后用'!'明显。
我做错了什么?
谢谢!