我从xml文件中获取“sinavid”,我想使用“id”的值(xml中的一个标签..)
如果id = 2,则主视图转到其他视图哪个storyboard id = 2
但由于可选值(我认为)
,我无法使用sinavid的值P.S:对不起我的英文:(
sinavid = haberler.objectAtIndex(indexPath.row).valueForKey("id") as! NSString as String
if (sinavid! == "2"){
row = "b"
let viewcontroller = storyboard?.instantiateViewControllerWithIdentifier(row)
self.navigationController?.pushViewController(viewcontroller!, animated: true)
}
else {
let viewcontroller = storyboard?.instantiateViewControllerWithIdentifier(row)
self.navigationController?.pushViewController(viewcontroller!, animated: true)
}
答案 0 :(得分:1)
<LinearLayout
android:id="@+id/btnLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/prevButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Prev" />
<Button
android:id="@+id/nextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next"/>
</LinearLayout>
答案 1 :(得分:1)
只需删除!
if (sinavid == "2"){
但这一行可能会导致您的应用崩溃,而且风险很高as!
。
sinavid = haberler.objectAtIndex(indexPath.row).valueForKey("id") as! NSString as String
如果您不是100%确定其安全性,最好将as?
与可选绑定结合使用。
答案 2 :(得分:0)
尝试使用?
代替!
。
使用问号,您可以将值转换为可选值。
答案 3 :(得分:0)
首先停止使用valueForKey:
,在这种情况下根本不需要KVC。
检查特定非可选值的合适且简单的方法是使用where
子句进行可选绑定
if let sinavid = haberler[indexPath.row]["id"] as? String where sinavid == "2" {
row = "b"
let viewcontroller = storyboard?.instantiateViewControllerWithIdentifier(row)
self.navigationController?.pushViewController(viewcontroller!, animated: true)
}
else {
let viewcontroller = storyboard?.instantiateViewControllerWithIdentifier(row)
self.navigationController?.pushViewController(viewcontroller!, animated: true)
}