gpt4 book ai didi

switch-statement - switch 语句出错了 - swift 语言

转载 作者:可可西里 更新时间:2023-11-01 00:18:44 28 4
gpt4 key购买 nike

在 Obj-c 中,我做了一个 switch 语句,我曾经使用 UIsplitviewcontroller 在我的 iPad 应用程序中四处移动现在我想快速地做同样的事情……我试了几个小时,现在我唯一没能尝试的是代码,因为它说了某种编译错误反正这是我在 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 中得出的结论。对此仍然很陌生,即使我有快速编程语言书,理解它也有点困难

这是我到目前为止在 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:


}

错误是:Expected declaration at the line with 让 viewController = UIViewcontroller()

最佳答案

让我们从您的 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)
}

您必须记住的主要事情是 varlet 的区别。通常,您将使用 let 来创建事物,除非这些事物稍后会更改其值,您可以使用 var

另一件事是可选值的使用,带有 ? 后缀。这是当值可能为 nil(未设置)时,否则它必须包含一个值。

关于switch-statement - switch 语句出错了 - swift 语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24148401/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com