简体   繁体   中英

Swift: How did this nil get into my [String: AnyObject] dictionary and how do I get rid of it?

I'm using a combination of AFNetworking and JSONModel in my Swift app to communicate with a .NET WCF service. Everything was working, right up until the point where I made a call that returned null values

The problem starts here:

if let resultsDict = responseObject as? Dictionary<String, AnyObject> {
    results = AuthModel(dictionary: resultsDict, error: nil)
    ....

responseObject is parsed from what the WCF service gives back to me (see this previous question if you want a little more background). I can cast it to a Dictionary<String, AnyObject> collection (have to, actually) and feed it to AuthModel which is a subclass of JSONModel , but I've found that if the collection contains a .NET null object, then results is nil , which messes up all the code that comes after. And the null from the WCF service is a null string in .NET land.

If I do a print(resultsDict) on the resultsDict object, I see this:

[x: <null>, y: 0, __type: Authenticate:#SomeLibrary.Models]

In this example, x is a string, and y is an int from c#/.NET code.

So yeah, it makes perfect sense that the code from JSONModel is not happy when one of the values is nil/null. However,

  1. I'm not sure how in the heck a nil/null value is in a non-Optional AnyObject object, and
  2. I'm not sure how to sanitize/get rid of it.

I can't cast responseObject to Dictionary<String, AnyObject?> , Xcode complains

I can't replace the particular nul/nill value in the Dictionary by iterating over it, Xcode complains (since you can't really work with an AnyObject object directly, I think)

I can't just create a second, sanitized Dictionary, since JSONModel apparently needs it to be <String, AnyObject> and I run into the same problem as above.

How can I get rid of this null/nil object in my Dictionary?


OK, Martin R's comment gave me the hint I needed to come up with some code that could "clean" the dictionary and insert empty strings where there's NSNull/nil/null values (really just NSNull apparently).

if let resultsDict = responseObject as? Dictionary<String, AnyObject> {
    var cleanedDict = Dictionary<String, AnyObject>()
    for r in resultDict {
        if r.1 as? NSObject == NSNull() {
            cleanedDict[r.0] = ""
        }
        else {
            cleanedDict[r.0] = r.1
        }
    }
    results = AuthModel(dictionary: cleanedDict, error: nil)
    ....

So I guess Xcode wasn't really telling me I couldn't assign anything to that dictionary, just not the way I was doing it.

I guess the question now becomes - is there a better way to do this? The way I'm doing it is a little bit brute force.

This is a slightly simplified version of your code to replace all NSNull values in the dictionary by an empty string:

if let resultsDict = responseObject as? [String:AnyObject] {
    var cleanedDict = resultsDict // a mutable copy
    for (key, elem) in cleanedDict {
        if elem is NSNull { // NSNull is a singleton, so this check is sufficient
            cleanedDict[key] = ""
        }
    }
    // ...
}

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