所有文章目录:http://my.oschina.net/ChenTF/blog/677112
本篇文章地址: https://my.oschina.net/ChenTF/blog/1162665
如果对您有帮助还请 "赞" 一下哈, 有问题可留言或加QQ群:323276186
OC中的经常使用nil, NSNUll, 你对这些理解正确吗?
题目: 以下各个表达式的结果是 YES or NO ?
NSNull *null = [NSNull null];
NSString *str = nil;
if (!null)
if (null == nil)
if ([null isKindOfClass:[NSNull class]])
if (!str)
if ([str isKindofClass:[NSNull class]])
if ([str isKindofClass:[NSString class]])
if(nil == 0)
if(null == 0)
if(nil == NULL)
答案:
NilStudy[29401:12682571] ========================================
NilStudy[29401:12682571] = NSNull =
NilStudy[29401:12682571] | NSNull *null = [NSNull null]; |
NilStudy[29401:12682571] if (null) ==> YES // null是对象
NilStudy[29401:12682571] if (!null) ==> NO
NilStudy[29401:12682571] if (null == nil) ==> NO
NilStudy[29401:12682571] if (null != nil) ==> YES
NilStudy[29401:12682571] if ([null isKindOfClass:[NSNull class]]) ==> YES
NilStudy[29401:12682571] if ([null isEqual:[NSNull class]]) ==> NO //class是类, null是对象
NilStudy[29401:12682571] if ([null isEqual:[NSNull null]]) ==> YES
NilStudy[29401:12682571] ========================================
NilStudy[29401:12682571] ========================================
NilStudy[29401:12682571] = nil =
NilStudy[29401:12682571] | NSString *str = nil; |
NilStudy[29401:12682571] if (str) ==> NO
NilStudy[29401:12682571] if (!str) ==> YES // nil为0,则!0为YES
NilStudy[29401:12682571] if (str == nil) ==> YES
NilStudy[29401:12682571] if (str != nil) ==> NO
NilStudy[29401:12682571] if ([str isKindOfClass:[NSNull class]]) ==> NO //nil与NSNull没关系
NilStudy[29401:12682571] if ([str isKindOfClass:[NSString class]]) ==> YES //类型不变
NilStudy[29401:12682571] ========================================
NilStudy[29401:12682571] ========================================
NilStudy[29401:12682571] = 值类型 =
NilStudy[29401:12682571] if (0) ==> NO
NilStudy[29401:12682571] if (3) ==> YES // 非0即真
NilStudy[29401:12682571] if (nil == 0) ==> YES
NilStudy[29401:12682571] if (null == 0) ==> NO // NSNull为对象
NilStudy[29401:12682571] if (nil == NULL) ==> YES // NULL为C中的空指针
结论:
- NSNull是类;
- null是对象;
- nil是空指针;
- NULL是C中的空指针
- Nil 对应的Class类型的空标示
定义:
Nil:
#ifndef Nil
# if __has_feature(cxx_nullptr)
# define Nil nullptr
# else
# define Nil __DARWIN_NULL
# endif
#endif
nil:
#ifndef nil
# if __has_feature(cxx_nullptr)
# define nil nullptr
# else
# define nil __DARWIN_NULL
# endif
#endif
NSNull:
@interface NSNull : NSObject <NSCopying, NSSecureCoding>
+ (NSNull *)null;
@end
© 著作权归作者所有