What is the right way to check for a null string in Objective-C?

后端 未结 20 1684
梦毁少年i
梦毁少年i 2020-11-27 09:26

I was using this in my iPhone app

if (title == nil) {
    // do something
}

but it throws some exception, and the console shows that the ti

相关标签:
20条回答
  • 2020-11-27 09:45

    MACRO Solution (2020)

    Here is the macro that I use for safe string instead of getting "(null)" string on a UILabel for example:

    #define SafeString(STRING) ([STRING length] == 0 ? @"" : STRING)
    

    let say you have an member class and name property, and name is nil:

    NSLog(@"%@", member.name); // prints (null) on UILabel
    

    with macro:

    NSLog(@"%@", SafeString(member.name)); // prints empty string on UILabel
    

    nice and clean

    0 讨论(0)
  • 2020-11-27 09:47

    If you want to test against all nil/empty objects (like empty strings or empty arrays/sets) you can use the following:

    static inline BOOL IsEmpty(id object) {
        return object == nil
            || ([object respondsToSelector:@selector(length)]
            && [(NSData *) object length] == 0)
            || ([object respondsToSelector:@selector(count)]
            && [(NSArray *) object count] == 0);
    }
    
    0 讨论(0)
  • 2020-11-27 09:47

    Complete checking of a string for null conditions can be a s follows :<\br>

        if(mystring)
         {
           if([mystring isEqualToString:@""])
            {
              mystring=@"some string";
            }
         }    
        else
         {
            //statements
         }
    0 讨论(0)
  • 2020-11-27 09:47

    I only check null string with

    if ([myString isEqual:[NSNull null]])

    0 讨论(0)
  • 2020-11-27 09:48

    There are two situations:

    It is possible that an object is [NSNull null], or it is impossible.
    Your application usually shouldn't use [NSNull null]; you only use it if you want to put a "null" object into an array, or use it as a dictionary value. And then you should know which arrays or dictionaries might contain null values, and which might not.
    If you think that an array never contains [NSNull null] values, then don't check for it. If there is an [NSNull null], you might get an exception but that is fine: Objective-C exceptions indicate programming errors. And you have a programming error that needs fixing by changing some code.

    If an object could be [NSNull null], then you check for this quite simply by testing
    (object == [NSNull null]). Calling isEqual or checking the class of the object is nonsense. There is only one [NSNull null] object, and the plain old C operator checks for it just fine in the most straightforward and most efficient way.

    If you check an NSString object that cannot be [NSNull null] (because you know it cannot be [NSNull null] or because you just checked that it is different from [NSNull null], then you need to ask yourself how you want to treat an empty string, that is one with length 0. If you treat it is a null string like nil, then test (object.length == 0). object.length will return 0 if object == nil, so this test covers nil objects and strings with length 0. If you treat a string of length 0 different from a nil string, just check if object == nil.

    Finally, if you want to add a string to an array or a dictionary, and the string could be nil, you have the choice of not adding it, replacing it with @"", or replacing it with [NSNull null]. Replacing it with @"" means you lose the ability to distinguish between "no string" and "string of length 0". Replacing it with [NSNull null] means you have to write code when you access the array or dictionary that checks for [NSNull null] objects.

    0 讨论(0)
  • 2020-11-27 09:50
    if ([strpass isEqual:[NSNull null]] || strpass==nil || [strpass isEqualToString:@"<null>"] || [strpass isEqualToString:@"(null)"] || strpass.length==0 || [strpass isEqualToString:@""])
    {
        //string is blank  
    }
    
    0 讨论(0)
提交回复
热议问题