我偶然发现了这段Objective-C代码:
- (void)updateUser:(NSDictionary<FBGraphUser> *)user inSlot:(NSInteger)slot {
[self validateSlotNumber:slot];
NSString *idKey = [NSString stringWithFormat:SUUserIDKeyFormat, (long)slot];
NSString *nameKey = [NSString stringWithFormat:SUUserNameKeyFormat, (long)slot];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (user != nil) {
NSLog(@"SUUserManager updating slot %li: fbid = %@, name = %@", (long)slot, user.objectID, user.name);
[defaults setObject:user.objectID forKey:idKey];
[defaults setObject:user.name forKey:nameKey];
} else {
NSLog(@"SUUserManager clearing slot %li", (long)slot);
// Can't be current user anymore
if (slot == _currentUserSlot) {
[self switchToNoActiveUser];
}
// FBSample logic
// Also need to tell the token cache to forget the tokens for this user
FBSessionTokenCachingStrategy *tokenCachingStrategy = [self createCachingStrategyForSlot:slot];
[tokenCachingStrategy clearToken];
[defaults removeObjectForKey:idKey];
[defaults removeObjectForKey:nameKey];
}
[defaults synchronize];
[self sendNotification];
}
它来自Facebook iOS SDK的一个示例。我试图在我的Swift书面应用程序中使用它。这就是我想出的:
func updateUser(user: Dictionary<String, FBGraphUser>, slot: Int) {
var idKey: String = String(format: userIDKeyFormat, slot)
var nameKey: String = String(format: userNameKeyFormat, slot)
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
if(user != nil) {
println("UserManager updating slot \(slot): Facebook ID = \(user.objectID), Name = \(user.name)")
defaults.setObject(user.objectID, forKey: idKey)
defaults.setObject(user.name, forKey: nameKey)
} else {
println("UserManager clearing slot \(slot)")
if(slot == currentUserSlot) {
self.switchToNoActiveUser()
}
var tokenCachingStrategy: FBSessionTokenCachingStrategy = self.createCachingStrategyForSlot(slot)
tokenCachingStrategy.clearToken()
defaults.removeObjectForKey(idKey)
defaults.removeObjectForKey(nameKey)
}
defaults.synchronize()
self.sendNotification()
}
问题出现在Dictionary<String, FBGraphUser>
中,我认为这与Obj-C等价物不同。在if语句中检查user
是否为nil,Xcode抛出错误:'Dictionary'不能转换为'String'。
那么如何将Obj-C代码翻译成Swift?
谢谢!
答案 0 :(得分:0)
您必须将字典设置为可选字典:
func updateUser(user: Dictionary<String, FBGraphUser>?, slot: Int) {...}
答案 1 :(得分:0)
func updateUser<val: NSDictionary where val:protocol<FBGraphUser>>(user:val!, slot: Int) {
var idKey: String = String(format: userIDKeyFormat, slot)
var nameKey: String = String(format: userNameKeyFormat, slot)
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
if(user != nil) {
println("UserManager updating slot \(slot): Facebook ID = \(user!.objectID), Name = \(user!.name)")
defaults.setObject(user!.objectID, forKey: idKey)
defaults.setObject(user!.name, forKey: nameKey)
} else {
println("UserManager clearing slot \(slot)")
if(slot == currentUserSlot) {
self.switchToNoActiveUser()
}
var tokenCachingStrategy: FBSessionTokenCachingStrategy = self.createCachingStrategyForSlot(slot)
tokenCachingStrategy.clearToken()
defaults.removeObjectForKey(idKey)
defaults.removeObjectForKey(nameKey)
}
defaults.synchronize()
self.sendNotification()
}