指向 nil 的指针会与 NULL 匹配吗?
示例:
验证方法包含此检查以查看是否应创建 NSError 对象:
- (BOOL)validateCompanyName:(NSString *)newName error:(NSError **)outError {
if (outError != NULL) {
// do it...
现在我传递一个 NSError 对象,如下所示:
NSError *error = nil;
BOOL ok = [self validateCompanyName:@"Apple" error:&error];
我不确定这是否与非 NULL 检查相匹配。我认为它不是 NULL,因为我相信 NULL 不是零。也许有人可以解决这个问题?
Example:
A validation method contains this check to see if an NSError object shall be created or not:
- (BOOL)validateCompanyName:(NSString *)newName error:(NSError **)outError {
if (outError != NULL) {
// do it...
Now I pass an NSError object, like this:
NSError *error = nil;
BOOL ok = [self validateCompanyName:@"Apple" error:&error];
I'm not sure if this matches the check for not NULL. I think it's not NULL, since I believe NULL is not nil. Maybe someone can clear this up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
nil
(全部小写)是指向 Objective-C 对象的空指针。Nil
(大写)是指向 Objective-C 类的空指针。NULL
(全部大写)是指向其他任何内容的空指针。然而它们都编译为
0
,所以(nil == Nil == NULL == 0)
(感谢 Dave)。nil
(all lower-case) is a null pointer to an Objective-C object.Nil
(capitalized) is a null pointer to an Objective-C class.NULL
(all caps) is a null pointer to anything else.Yet they all compile to
0
, so(nil == Nil == NULL == 0)
(thanks Dave).