AHTabBarController与Swift不兼容

时间:2015-03-02 16:34:59

标签: swift

我想使用" AHTabBarController"在一个快速的项目中。

我不知道为什么这段代码不起作用:

        var tabBarController = self.window?.rootViewController as AHTabBarController

        var tab1 = AHTabView()
        tab1.image = UIImage(named: "")
        tab1.title = "Tab 1"

        var tab1_subItem = AHSubitemView(frame: UIScreen.mainScreen().bounds)
        tab1_subItem.image = UIImage(named: "")
        tab1_subItem.title = "Tab 1 Title"
        tab1_subItem.viewControllerIdentifier = "channelsListSB"

        tab1.addSubitem(tab1_subItem)


        tabBarController.tabs.addObjectsFromArray([tab1])

        self.window?.rootViewController = tabBarController

Objective-C代码是:

AHTabBarController *tabBarController = (AHTabBarController*)self.window.rootViewController;

/******* PURUS *******/
AHTabView *purus = [AHTabView new];
[purus setImage:[UIImage imageNamed:@"persondot"]];
[purus setTitle:@"Purus"];

AHSubitemView *pFirst = [AHSubitemView new];
[pFirst setImage:[UIImage imageNamed:@"persondot"]];
[pFirst setTitle:@"First"];
[pFirst setViewControllerIdentifier:@"PurusFirst"];
[purus addSubitem:pFirst];

// …  

AHSubitemView *fFirst = [AHSubitemView new];
[fFirst setImage:[UIImage imageNamed:@"photos"]];
[fFirst setTitle:@"First"];
[fFirst setViewControllerIdentifier:@"FringillaFirst"];
[fringilla addSubitem:fFirst];
//Don't forget to add your AHTabView instances to the AHTabBarController!
[tabBarController.tabs addObjectsFromArray:@[purus, fringilla, ipsum]];

错误是:

  

致命错误:在解包可选值时意外发现nil

1 个答案:

答案 0 :(得分:1)

你没有指定一行,所以我只是猜测问题在这里:

tab1.image = UIImage(named: "")

这将返回nil,但您将其分配给image,这是一个隐式解包的可选项。您需要指定保证返回图像的实际图像名称。或者,您可以有条件地执行此操作:

if let image = UIImage(named: "") {
    tab1.image = image
} else {
    // handle failure
}