1

I have an array that's populated from a JSON response from an API server. Sometimes the values for a key in this array are Null

I am trying to take the given value and drop it into the detail text of a table cell for display.

The problem is that when I try to coerce the value into an String I get a crash, which I think is because I'm trying to coerce Null into a string.

What's the right way to do this? ex- my response is below type and I'm trying to fetch that array in self.imageArray variable

response = ["abc.jpg","null","xyzzy.jpg"]

 self.imageArray = (self.dataArray.value(forKey: "product_image") as? [String])!

but at second iteration it gets crashed coz second value is null.

2

5 Answers 5

1

In your case only (Solution according to given question 'response' array) :-

response = ["abc.jpg","null","xyzzy.jpg"]

Use following code

      if !response.contains("") && !response.contains("null"){
        //  No null or empty string now
        // your code
      }else{
           print("Contain null or empty value")
       }
Sign up to request clarification or add additional context in comments.

2 Comments

how will you validate " " empty string
@Anbu.Karthik Bro ,first of all , this type of response are not good practice (my opinion) . If ur Back end developer can't manage this then you have to trim the all value of array string and then do like answer code . You can trim string like : let trimmedString = string.trimmingCharacters(in: .whitespaces) . This is actually what I will do in this scenario. It might be handle more optimize way .
1

You can user compatcMap in this case.

 let response = ["abc.jpg", nil, "xyzzy.jpg"]
 let result = response.compactMap { $0 }
 print(result)

If you are sure the value will be "null" in string type then you can use the following way.

let response = ["abc.jpg", "null", "xyzzy.jpg"]
let result = response.filter { $0 != "null" }
print(result)

the combined result is

 let result = response.filter { $0 != "null" }.compactMap { $0 }
print(result)

1 Comment

how will you validate "null" this type string of null .. ?
1

This will handle the issue for both null and nil.

response = ["abc.jpg","null","xyzzy.jpg", nil]
var result = response.filter { return ($0 != "null" && $0 != nil) }

print(result) // return option result.

5 Comments

That is iteration through the array twice. Not very good performance-wise. What if his array has 5000 items?
Now it is better, but as someone posted a comment in an another answer: what if there is an object like this: " "? :)
then you can add one more check the same as the other.
@ Shauket Sheikh- I get a crashes
Whats the response array?
0

It should be like

var image = ["abc.jpg", nil, "xyzz.jpg"]
image = image.filter { $0 != nil}
print(image)

You have to filter out the nil values.

if you remove specific string "null"

var image = ["abc.jpg", "null", "xyzz.jpg"]
image = image.filter { $0 != "null"}
print(image)

2 Comments

how will you validate "null" this type string of null .. ?
Contact with your backend, it is their fault. "null" is a valid string.
0

If you know for sure that a valid value will always contain an image type like .jpg, then you can check against that and it will remove nil values, "null" or any other random strings, including empty strings " ".

response = ["abc.jpg","null","xyzzy.jpg", nil, " "]
self.imageArray = response.filter { $0?.contains(".jpg") ?? false }

3 Comments

Could not cast value of type 'NSNull' (0x10ea06e48) to 'NSString' (0x10d5554a8). I get this error
@ Starsky- I can't fetch array from response because of null value.it crashes
You can assign to your self.imageArray directly the filtered version of response. If it still has issues, then can you post a bigger portion of your code, where you handle your request response and you try to parse that JSON?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.