Swift:语法翻译
Swift: Syntax Translation
需要帮助将下面的代码翻译成 Swift。
Objective-C 代码(效果很好):
- (UIViewController *)getViewControllerFromStoryboard:(NSString *)storyboardName sceneName:(NSString*)sceneName iconName:(NSString*)icon title:(NSString*)title
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:sceneName];
UIImage *tabIcon = [UIImage imageNamed:icon];
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:tabIcon selectedImage:nil];
vc.title = NSLocalizedString(title,nil);
return vc;
}
翻译成Swift:
func getViewControllerFromStoryBoar(storyboardName: String, sceneName: String, iconName: String, title: String) -> UIViewController{
let sb : UIStoryboard = UIStoryboard(name: storyboardName, bundle: nil)
let vc = sb.instantiateViewControllerWithIdentifier(sceneName) //Warning A
let tabIcon : UIImage = UIImage(named: iconName)!
vc.tabBarItem = UITabBarItem(initWithTitle:title, image:tabIcon) //Error A
vc.title = title //Error B
return vc as UIViewController
}
警告 A: 常量 'vc' 推断为类型 'AnyObject!',这可能是意外的
为什么 'AnyObject!' ?这似乎修复了错误
错误 A: 无法分配给 'vc'
中的 'tabBarItem'
错误 B: 无法分配给 'vc'
中的 'title'
上面两个错误我没看懂
Error A: Cannot assign to 'tabBarItem' in 'vc'
Error B: Cannot assign to 'title' in 'vc'
因为 instantiateViewControllerWithIdentifier
returns 一个 AnyObject。这就是警告警告您的内容!你应该注意那个警告。 (您将警告静音;那是错误的做法。不要将其静音;听。)
您需要先强制转换此引用,然后才能使用它。我知道你知道它是一个UIViewController,但是Swift不知道这个;你必须投射并告诉它。
let vc = sb.instantiateViewControllerWithIdentifier(sceneName) as UIViewController
一举解决
需要帮助将下面的代码翻译成 Swift。
Objective-C 代码(效果很好):
- (UIViewController *)getViewControllerFromStoryboard:(NSString *)storyboardName sceneName:(NSString*)sceneName iconName:(NSString*)icon title:(NSString*)title
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:sceneName];
UIImage *tabIcon = [UIImage imageNamed:icon];
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:tabIcon selectedImage:nil];
vc.title = NSLocalizedString(title,nil);
return vc;
}
翻译成Swift:
func getViewControllerFromStoryBoar(storyboardName: String, sceneName: String, iconName: String, title: String) -> UIViewController{
let sb : UIStoryboard = UIStoryboard(name: storyboardName, bundle: nil)
let vc = sb.instantiateViewControllerWithIdentifier(sceneName) //Warning A
let tabIcon : UIImage = UIImage(named: iconName)!
vc.tabBarItem = UITabBarItem(initWithTitle:title, image:tabIcon) //Error A
vc.title = title //Error B
return vc as UIViewController
}
警告 A: 常量 'vc' 推断为类型 'AnyObject!',这可能是意外的 为什么 'AnyObject!' ?这似乎修复了错误
错误 A: 无法分配给 'vc'
中的 'tabBarItem'错误 B: 无法分配给 'vc'
中的 'title'上面两个错误我没看懂
Error A: Cannot assign to 'tabBarItem' in 'vc'
Error B: Cannot assign to 'title' in 'vc'
因为 instantiateViewControllerWithIdentifier
returns 一个 AnyObject。这就是警告警告您的内容!你应该注意那个警告。 (您将警告静音;那是错误的做法。不要将其静音;听。)
您需要先强制转换此引用,然后才能使用它。我知道你知道它是一个UIViewController,但是Swift不知道这个;你必须投射并告诉它。
let vc = sb.instantiateViewControllerWithIdentifier(sceneName) as UIViewController
一举解决