简体   繁体   English

Xcode中的致命错误-我知道原因,但不知道如何解决

[英]Fatal error in Xcode - I know the cause but don't know how to fix it

code highlighted in green by Xcode (Thread 1: EXC_BAD_INSRTUCTION) Xcode以绿色突出显示的代码(线程1:EXC_BAD_INSRTUCTION)

amountDueLabel.text = String(HomeViewController().getAllowanceDue())

the function getAllowanceDue() 函数getAllowanceDue()

func getAllowanceDue() -> Int{
    var allowance = allowanceTextField.text.toInt()
    return allowance!
}

thank you so much! 非常感谢!

Go easy on me - I am a beginner in iOS development 轻松一点-我是iOS开发的初学者

In Swift unlike Objective C, you have to explicitly let the compiler know that a variable can be 'nil' in future by marking that variable as optional (?). 在Swift中,与Objective C不同,您必须通过将变量标记为可选(?)来明确让编译器知道该变量将来可能为“ nil”。

toInt() method also returns an 'optional Integer value' which means toInt() method can also return 'nil' value in case it is not able to convert the supplied string value to integer value. toInt()方法还返回一个“可选的整数值”,这意味着如果toInt()方法无法将提供的字符串值转换为整数值,则它也可以返回“ nil”值。

Here you are unwrapping the optional Int value with allowance! 在这里,您可以使用allowance!展开可选的Int值allowance! . But before unwrapping the optional value you must check if the optional value is not 'nil'. 但是在解开可选值之前,您必须检查可选值是否不是'nil'。

func getAllowanceDue() -> Int {
    if let allowance = anotherString.toInt() {
        return allowance
    }
    return -1
}

By checking if let allowance = anotherString.toInt() we are actually using concept of Optional Binding in Swift to check whether optional contains a value. 通过检查if let allowance = anotherString.toInt()我们实际上在Swift中使用了Optional Binding的概念来检查optional是否包含一个值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不知道这个xcode错误是什么意思? - Don't know what this xcode error means? 我不了解并且不知道如何解决的ImagePicker IOS错误 - ImagePicker IOS errors i don't understand and don't know how to fix UIViewController.storyboard 必须仅在主线程中使用 - 新错误我不知道如何修复? - UIViewController.storyboard must be used from main thread only - New Error I don't know how to fix? Xcode:我刚遇到一个Apple Mach-O Linker(Id)错误,不知道为什么 - Xcode: I just got an Apple Mach-O Linker (Id) Error and don't know why Xcode swift 3 不知道如何在我单击的位置显示图像 - Xcode swift 3 don't know how to display an image where I click UIEdgeInsetsMake在单元格上创建一个奇怪的乐队,我不知道如何解决它 - UIEdgeInsetsMake creating a weird band on the cell, and I don't know how to fix it 我的应用由于高度限制而崩溃,我不知道如何解决 - My App crashes because of the heightConstraints I don't know how to fix it 如何在Xcode 8.1中修复此错误(返回host_statistics)? 我想知道设备的可用内存 - How to fix this error(return host_statistics) in Xcode 8.1? I want to know the free memory of the device 安装 pod 'CLTypingLabel' 时出错。 我不知道如何解决它。 我已经尝试了很多次。 请有人帮助我 - An error occured while installing pod 'CLTypingLabel'. I don't know how to fix it. I have tried many time. Please someone help me 我不知道如何将弹出框与按钮对齐 - I don't know how to align my popovers to button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM