简体   繁体   English

致命错误:在展开Optional值时意外发现nil,获取json错误swift

[英]fatal error: unexpectedly found nil while unwrapping an Optional value, Get json error swift

在此输入图像描述

Get json error 得到json错误

I am getting the error fatal error: unexpectedly found nil while unwrapping an Optional value when I try to return the json : 我收到错误fatal error: unexpectedly found nil while unwrapping an Optional value当我尝试返回jsonfatal error: unexpectedly found nil while unwrapping an Optional valuefatal error: unexpectedly found nil while unwrapping an Optional value

func getJson (str:NSString) -> AnyObject{

    var proxiesURL = NSURL(string:str)
    var proxiesDataJson = NSData.dataWithContentsOfURL(proxiesURL, options: NSDataReadingOptions.DataReadingUncached, error: nil)
    var json: AnyObject!

    if (proxiesDataJson != nil ){
        json = NSJSONSerialization.JSONObjectWithData(proxiesDataJson, options: NSJSONReadingOptions.AllowFragments, error: nil) as AnyObject
    }
    return json
}

I assume the question is "what am I doing wrong". 我认为问题是“我做错了什么”。

Your function returns AnyObject (non-optional, thus cannot be nil). 您的函数返回AnyObject(非可选,因此不能为零)。 You declared json as AnyObject! 你将json声明为AnyObject! (implicitly unwrapped optional - it may be nil, but you promise to compiler that it will have non-nil value). (隐式解包可选 - 它可能是零,但你保证编译器它将具有非零值)。 And yet json is nil when you attempt to return it - either code at json = NSJSONSerialization.JSONObjectWithData line wasn't executed, or NSJSONSerialization.JSONObjectWithData returned nil. 但是当你试图返回它时json是nil - 在json = NSJSONSerialization.JSONObjectWithData行的代码没有被执行,或者NSJSONSerialization.JSONObjectWithData返回nil。

You used ! 你用过! with optional object which should be used only when you have checked its not nil, else it will crash. 使用可选对象,只有在检查了它不是nil时才应该使用它,否则它会崩溃。 Instead you should wrap it with ? 相反,你应该用它包装吗? instead of !. 代替 !。 It won't crash and you don't have to check for nil. 它不会崩溃,你不必检查零。

暂无
暂无

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

相关问题 在“ swift”项目中删除故事板时出错。 错误:“致命错误:解开可选值时意外发现nil” - Error while removing story board in 'swift' project. Error : “fatal error: unexpectedly found nil while unwrapping an Optional value” Obj-C框架返回nil,并且崩溃我的Swift代码说'致命错误:在展开Optional值时意外发现nil' - Obj-C frameworks returns nil, And crashes my Swift Code saying 'fatal error: unexpectedly found nil while unwrapping an Optional value' 值不是nil,但是在展开Optional值时意外发现nil - value is not nil but getting unexpectedly found nil while unwrapping an Optional value 将现有的obj c代码移植到swift-解开可选值时意外发现nil - Port existing obj c code to swift - unexpectedly found nil while unwrapping an Optional value PhysicsWorld:解开可选值时意外发现nil - PhysicsWorld: unexpectedly found nil while unwrapping an optional value 在Swift中解开Optional值错误 - Unwrapping an Optional value error in Swift 仅在 TestFlight/Prod 中崩溃 - UIButton 初始化覆盖 - 在展开可选时意外发现 nil - Crash only in TestFlight/Prod - UIButton Init Override - Unexpectedly found nil while unwrapping an optional 解包可选的 IBOutlet 值时为 Nil - Nil while unwrapping an Optional IBOutlet value 在NSIndexPath上意外发现nil运行时错误 - Getting unexpectedly found nil runtime error on NSIndexPath 如果错误== nil解析迅速 - if error == nil parse swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM