在Obj-c中我做了一个switch语句,我曾经在我的应用程序中使用UIsplitviewcontroller在iPad中移动 现在我想在swift中做同样的事情...我尝试了几个小时,现在我唯一能够尝试的是代码因为它说某种编译错误 无论如何 这是我在Obj-c中得到的东西
-(void)initialSite:(int)viewId {
UIViewController *viewController;
switch (viewId) {
case 0:{
viewController = self.initital;
NSString *star = [NSString stringWithFormat:@"Velkommen til %@'s Bog",[data valueForKey:@"navn"]];
self.navigationItem.title = star;}
break;
case 1:{
viewController = self.startSide;
NSString *start = [NSString stringWithFormat:@"%@'s Bog, start-side",[data valueForKey:@"navn"]];
self.navigationItem.title = start;}
break;
}
[self showChildViewController:viewController];
}
这是我迄今为止在swift中提出的问题。仍然是新手,并了解它有点难,即使我有快速的编程语言书
这是我到目前为止所获得的快速
let viewController = UIViewController()
switch viewController {
case "initial":
let initial : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc0 : UIViewController = initial.instantiateViewControllerWithIdentifier("initial") as UIViewController
self.presentViewController(vc0, animated: true, completion: nil)
let rowData: NSDictionary = self.menuItemArray[indexPath.row] as NSDictionary!
self.navigation.title = rowData["navn"] as? String
case "startSide":
let startSide : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc1 : UIViewController = startSide.instantiateViewControllerWithIdentifier("startSide") as UIViewController
let rowData: NSDictionary = self.manuItemArray[indexPath.row] as NSDictionary!
self.presentViewController(vc1, animated: true, completion: nil)
self.navigation.title = rowData["navn"] as? String
default:
}
错误是: 预期声明 让viewController = UIViewcontroller()
答案 0 :(得分:4)
让我们从您的Obj-C实施开始:
-(void)initialSite:(int)viewId
{
UIViewController *viewController;
switch (viewId)
{
case 0:
{
viewController = self.initital;
NSString *star = [NSString stringWithFormat:@"Velkommen til %@'s Bog",[data valueForKey:@"navn"]];
self.navigationItem.title = star;
}
break;
case 1:
{
viewController = self.startSide;
NSString *start = [NSString stringWithFormat:@"%@'s Bog, start-side",[data valueForKey:@"navn"]];
self.navigationItem.title = start;
}
break;
}
[self showChildViewController:viewController];
}
现在Swift中的这个片段:
func initialSite(viewID:Int)
{
var viewController : UIViewController?
switch (viewID)
{
case 0:
viewController = self.initial
let navn = self.data["navn"] as? String
let star = "Velkommen til \(navn)'s Bog"
self.navigationItem.title = star
case 1:
viewController = self.startSide
let navn = self.data["navn"] as? String
let star = "\(navn)'s Bog, start-side"
self.navigationItem.title = star
default:
viewController = nil
// do nothing
}
self.showChildViewController(viewController)
}
您必须记住的主要事项是var
与let
的区别。通常情况下,您会使用let
来创建内容,除非这些内容的值稍后会更改,您使用var
。
另一件事是使用带有?
后缀的选项。这是当值可能是nil
(未设置)时,否则它必须包含值。
答案 1 :(得分:2)
看起来SiLo打败了我。无论如何,我有我的解决方案,所以我会发布它。我就是这样做的:
func initialSite(viewId: Int) -> () {
var viewController: UIViewController?
let dataValue = data["navn"];
var start: String?
switch viewId {
case 1:
viewController = self.initital
start = "Velkommen til \(dataValue)'s Bog"
case 2:
viewController = self.startSide
start = "\(dataValue)'s Bog, start-side"
default:
break;
}
self.navigationItem.title = start!
showChildViewController(viewController!)
}