简体   繁体   English

我在代码中的某个地方获取了nil值,但我不确定它在哪里以及如何修复它

[英]i am getting a nil value somewhere in my code and i am unsure where it is and how to fix it

Here is the error i am receiving 这是我收到的error

error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). 错误:执行被中断,原因:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)。

I know the error is caused by a nil value somewhere in the code but i cannot find it. 我知道错误是由代码中某处的nil值引起的,但我找不到它。 Here is the code is anyone has any insight as to where the nil value is then help would be appreciated. 这是任何人对nil值在哪里都有任何见识的代码,那么将不胜感激。

import UIKit

var randomNumber = 0
var challengesArray = [""]
var challengeNumber = 0
var mad : String = ""
var challenge : String = ""

func ChallengeBankSplitter()  {

    let fileURLProject = Bundle.main.path(forResource: "Challenges", ofType: "txt")
    var readStringProject = ""
    do {
        readStringProject = try String(contentsOfFile: fileURLProject!, encoding: String.Encoding.utf8)
    } catch let error as NSError {
        print ("Failed to read from project")
        print (error)
    }
    challengesArray =  readStringProject.components(separatedBy: ".") as Array
    var arrayLength = challengesArray.count
    randomNumber = arrayLength

}


func ChallengeSelector() {
    ChallengeBankSplitter()
    challengeNumber = Int((arc4random_uniform(UInt32(randomNumber))))
    challenge = challengesArray[challengeNumber]
    print (challenge)
}

ChallengeSelector()

As I can you have given default value for below variable 我可以给下面的变量提供默认值

var randomNumber = 0
var challengesArray = [""]
var challengeNumber = 0
var mad : String = ""
var challenge : String = ""

so accessing above must not an issue and if you facing nil issue. 因此,如果您遇到零问题,则访问上面的内容一定不是问题。 So make sure you access all other variable (check first optional or not) if optional then use if let or guard to avoid crash via accessing nil at run time. 因此,请确保访问所有其他变量(如果为可选,则首先检查是否为可选),然后使用if letguard来避免在运行时访问nil导致崩溃。

暂无
暂无

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

相关问题 为什么在我的代码中隐式解包 Optional 值时意外发现 nil? - Why am I getting a Unexpectedly found nil while implicitly unwrapping an Optional value in my Code? 为什么我的字符串值不为零? - Why am I getting nil for this string value? 为什么我没有在我的 firebase 数据库中的特定位置检索值而只得到零? - Why am I not retrieving a value at a specific location within my firebase database and am only getting nil? 我无法使用此代码从本地主机获取数据。 请帮我。 我越来越零 - I am not able to get data from my localhost using this code. Please help me. I am getting nil 我收到一条警告,将我的OPTIONAL值与not-nil比较会始终返回true - I am getting a warning comparing my OPTIONAL value to not-nil will always return true 如何解析嵌套的 JSON.I 得到 nil 值作为输出 - How do I parse a nested JSON.I am getting nil value as an output 为什么会出现nil错误? - Why am I getting a nil error? 即使我给一个 url 变量一个值,我也得到一个 nil 值,所以我想知道我做错了什么? - I am getting a nil value even if i give a url variable a value, so i want to know what am i doing wrong? 当我尝试在设备中启动我的应用程序时,在xcode中由于启动选项nil出现错误? - when I am trying to launch my app in my device and I am getting the error as launch option nil in my xcode? 为什么我在使用该值时得到一个 nil 值,但是当我打印它时它显示为 none nil? - Why am I getting a nil value when I go to use the value but when I print it it shows up as none nil?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM