简体   繁体   中英

Getting crash when “<null>” as response from api is received in SWIFT

Getting the image url null as response from api, is there any way for fix it. Response from api is:

"image_path" = "<null>";

Tried to handle it like this:

if !(arrList.objectAtIndex(indexPath.row).valueForKey("media")?.objectAtIndex(0).valueForKey("image_path") is NSNull) {
            // optional is NOT NULL, neither NIL nor NSNull
        }

It is still crashing please guide. Update:

Found the problem, null was not the issue actually. Posting the response below to make things clear.

media =             (
                            {
                id = "97eb48a0-429a-11e6-873c-ad7b848648f1";
                "image_path" = "https://s3-eu-west-1.mazws.com/facebook_profile_images/15105131.png";
                "room_post_id" = "97c572e0-429a-11e6-b72b-fd489f63a1fc";
                "user_id" = "66fe22a0-4296-11e6-a1d9-69dc307add4b";
                "video_path" = "<null>";
            },
                            {
                id = "981a6880-429a-11e6-b039-736a0bf954dc";
                "image_path" = "<null>";
                "room_post_id" = "97c572e0-429a-11e6-b72b-fd489f63a1fc";
                "user_id" = "66fe22a0-4296-11e6-a1d9-69dc307add4b";
                "video_path" = "https://s3-eu-west-1.naws.com/facebook_profile_images/1255803.mp4";
            }
        );

This is the normal response, now the response where trouble is:

media =             (
        );

Can it be handle any how? Please guide.

Try like this .. it'll not crash

if let imgPath = arrList.objectAtIndex(indexPath.row).valueForKey("media")?.objectAtIndex(0).valu‌​eForKey("image_path") as? String {

//... your value is string 

}

Let me guess types by information you provided:

if let arrList = arrList as? NSArray,
    firstObject = arrList.objectAtIndex(indexPath.row) as? NSDictionary,
    media = firstObject.valueForKey("media") as? NSArray,
    secondObject = media.objectAtIndex(0) as? NSDictionary,
    image_path = secondObject.valueForKey("image_path") as? String {

    print(image_path)
    // now validate your string
}

Validating your string can be like this:

if !image_path.isEmpty && image_path != "<null>" {
    //do something
}

or like this:

if !image_path.isEmpty {
    if let image_pathURL = NSURL(string: image_path), _ = image_pathURL.host {
        //do something
    }
}

为什么不用==来简单检查:

if !(arrList.objectAtIndex(indexPath.row).valueForKey("media")?.objectAtIndex(0).valueForKey("image_path") == "<null>")
if let image = myJson["image_path"] as? String {
  imageView.image = image
}
else {
  imageview.image = "default.png"
}

also try this

if !(arrList.objectAtIndex(indexPath.row).valueForKey("media")?.objectAtIndex(0).valueForKey("image_path") == "<null>") {
            // optional is NOT NULL, neither NIL nor NSNull
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM