Nil and (NSString *) [NSNull null] equivalent when checking for an empty NSString

I have an NSString object,

 NSString *aString; 

that is, the next two versions are equivalent?

Version 1:

 if ( (NSString *)[NSNull null] == aString ) { // Logic handling } 

Version 2:

 if ( nil == aString ) { // Logic handling } 

Help Messages

  • The difference between nil, nil and null

  • How to determine if NSString is null?

  • Apple NSNull Reference

  • How to check if a string is empty in Objective-C?

Update - test result

My simple test result shows that these two versions have different types of behavior:

  • When aString initialized and then assigned with nil :

    false for expression in version 1,

    true for expression in version 2.

  • When aString initialized to @"" .

    false for expression in version 1,

    false for expression in version 2.

So, it is clear that these two versions are not equivalent in their behavior.

Security Code:

 NSString *aString = nil; NSString *bString = [NSString stringWithFormat:@""]; if ((NSString *)[NSNull null] == aString) { NSLog(@"a1 - true"); } else { NSLog(@"a1 - false"); } if (nil == aString) { NSLog(@"a2 - true"); } else { NSLog(@"a2 - false"); } if ((NSString *)[NSNull null] == bString) { NSLog(@"b1 - true"); } else { NSLog(@"b1 - false"); } if (nil == bString) { NSLog(@"b2 - true"); } else { NSLog(@"b2 - false"); } 

Console output:

 2013-10-31 00:56:48.132 emptyproject[31104:70b] a1 - false 2013-10-31 00:56:48.133 emptyproject[31104:70b] a2 - true 2013-10-31 00:56:48.133 emptyproject[31104:70b] b1 - false 2013-10-31 00:56:48.133 emptyproject[31104:70b] b2 - false 

Update - What I mean by "Empty String" **

Now I have made it clear that the NSString object will be nil , and it must be a valid initialized instance containing an empty string value @"" . What I really need in this post is how to check if my NSString object was successfully initialized, i.e. if aString is nil . I want to know if there is a difference for the two versions of the test code.

+7
null objective-c nsstring nsnull
source share
6 answers

[NSNull null] and nil are not equivalent. [NSNull null] intended to represent the concept of NULL (as in the case without an object) in cases where nil cannot be used, for example, in NSArray (since you can only insert objects in them). [NSNull null] is an object (always the same object), and nil is a pointer to 0.

NSHipster has a nice discussion here . He says:

NSNull is used throughout the Foundation and other frameworks to skirt around collection constraints such as NSArray and NSDictionary without being able to contain nil values. You can think of NSNull as effectively boxing a NULL or nil value so that it can be used in a collection.

If you have:

 NSString *aString; if ( aString == (NSString *)[NSNull null] ) { // Logic handling } 

then something is wrong, aString must point to an NSString (or subclass) or nil object. But not [NSNull null] , which is an object of another class, you should not drop it from one to another.

EDIT:

The comments indicate that you want to check if the line is empty (as in @"" ), this is different. See question . The empty string is an NSString object, it is not nil , not [NSNull null] .

+28
source share

they do not match, NSNull is a valid object (inherited from NSObject ), opposite the nil pointer, which points to nothing.

then how you can check if an object is an NSNull object, but the first version is ok too.

 id _object = // any kind of NSObject ... if ([_object isKindOfClass:[NSNull class]]) { // Logic handling } 
+3
source share

nil doesn't mean anything. [NSNull null] is an object, an instance of the NSNull class
== means equality

something equals to something else does not match something is equal to nothing

0
source share

[NSNull null] returns an instance of singleton NSNull .

aString == [NSNull null] compares two pointers. As long as aString does not point to a singleton NSNull , they will never be equal.

0
source share

If you want to match string with nil:
1. if (aString.length == 0) {}
2.if (sString isEqualToString: @ "") {}
3. If (aString! = Zero) {} else {// do your stuff here}

0
source share
 +(NSString*)replaceNullValuesWithEmptyString:(id)tempObj { if (([tempObj isKindOfClass:[NSNull class]])|| (tempObj == nil) || (tempObj == (id)[NSNull null])|| [tempObj isEqual:[NSNull null]] || [tempObj isEqual:nil]) { } else { if([tempObj respondsToSelector:@selector(isEqualToString:)]) { if ([tempObj isEqualToString:@"<null>"] || [tempObj isEqualToString:@"(null)"]) { } else { if ([tempObj respondsToSelector:@selector(length)]) { if ([tempObj length]>0) { NSLog(@"Check Passed."); return tempObj; } } } } } NSLog(@"Check failed."); return @""; 

}

0
source share

All Articles