iOS - Data return fields null, <null> resulting in program crash problem solving

Keywords: github iOS Attribute

In the process of iOS development, it is often necessary to process data with the server, but in the process of data connection, there will be problems such as null ", which lead to inexplicable collapse. I believe you will write all kinds of judgments to deal with these exceptions, and even you will change one interface after another, which really frustrates us.

Or maybe you'll write a sort of tune. This will also make you very upset!

Recently, a better solution has been found: if you use the third party of AFNetworking.
Set the following properties:

serializer.removesKeysWithNullValues = YES;

Where to set it? Search for the AFURLResponseSerialization.m class and locate the AFJSONResponseSerializer class as follows:

Then locate: AFJSONObjectByRemovingKeys WithNullValues

static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingOptions readingOptions) {
    if ([JSONObject isKindOfClass:[NSArray class]]) {
        NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:[(NSArray *)JSONObject count]];
        for (id value in (NSArray *)JSONObject) {
            [mutableArray addObject:AFJSONObjectByRemovingKeysWithNullValues(value, readingOptions)];
        }

        return (readingOptions & NSJSONReadingMutableContainers) ? mutableArray : [NSArray arrayWithArray:mutableArray];
    } else if ([JSONObject isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:JSONObject];
        for (id <NSCopying> key in [(NSDictionary *)JSONObject allKeys]) {
            id value = (NSDictionary *)JSONObject[key];
            if (!value || [value isEqual:[NSNull null]]) {
                //Here is the source code of the author of this library
                //[mutableDictionary removeObjectForKey:key];
                //Following is the change to change the null pointer type to an empty string
                mutableDictionary[key] = @"";
            } else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
                mutableDictionary[key] = AFJSONObjectByRemovingKeysWithNullValues(value, readingOptions);
            }
        }

        return (readingOptions & NSJSONReadingMutableContainers) ? mutableDictionary : [NSDictionary dictionaryWithDictionary:mutableDictionary];
    }

    return JSONObject;
}

The modification is to change the null pointer value to an empty string.

It doesn't matter if you don't use AF. Let's take a look at the ultimate solution.
Finally, a once-and-for-all solution was found. Niubi's foreigner wrote a Category called NullSafe, which operates at runtime and sets the unpleasant null value to nil, which is safe and can send any message to nil objects without running away. This category is very convenient to use, as long as it is added to the project, you don't need to do anything else, right, it's so simple. For more details, please go to Github.
After extracting the file, import the NullSafe.m file directly into the project.

https://github.com/nicklockwood/NullSafe

But the second method does not know whether there is AF Networking. I poured it into AF Networking and found it ineffective. Maybe it is because of the conflict between AF Networking and AF Networking.

Posted by xray_griff on Tue, 11 Dec 2018 21:15:08 -0800