在 Objective-c 中将版本发送到 NIL
有人可以向我解释一下这个程序在 Mac 电脑上的输出,然后在 iPhone 设备上的输出吗?
我创建了一个简单的 Foo 类,其中不包含任何内容, Foo.h :
#import <Foundation/Foundation.h>
@interface Foo : NSObject {
}
@end
和 Foo.m :
#import "Foo.h"
@implementation Foo
@end
为了在 Mac 上测试它,我使用这个 main.m :
#import "Foo.h"
int main(int argc, const char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Foo *myFoo;
[myFoo description];
printf("%p\n", myFoo);
[myFoo release];
[pool drain];
return 0;
}
这个程序输出 :
0x0
但在 iphone 上它直接崩溃。
谢谢。
can someone explain to me the output of this program in a mac computer and then on an iphone device.
I create a trivial Foo class which contain nothing, Foo.h :
#import <Foundation/Foundation.h>
@interface Foo : NSObject {
}
@end
and the Foo.m :
#import "Foo.h"
@implementation Foo
@end
To testing this on a Mac, i use this main.m :
#import "Foo.h"
int main(int argc, const char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Foo *myFoo;
[myFoo description];
printf("%p\n", myFoo);
[myFoo release];
[pool drain];
return 0;
}
This program output :
0x0
But on an iphone it crashes directly.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Objective-C 中,当您在 nil 指针上调用方法时,不会发生任何事情。
但是,在上面的代码中,您没有将指针设置为 nil,因此您正在向内存位置发送消息。如果您的应用程序无权访问该内存位置,它将崩溃并显示 EXC_BAD_ACCESS 消息。
如果崩溃的话添加这个应该会停止:
Foo *myFoo = nil;
In Objective-C, when you call a method on a nil pointer, nothing happens.
But, in your code above, you do not set the pointer to nil, so you are sending a message to a memory location. If you application does not have access to that memory location it will crash with an EXC_BAD_ACCESS message.
Adding this should stop if from crashing:
Foo *myFoo = nil;
这里的底线是这一行:
上面的代码没有创建了 Foo 的实例。它所做的只是声明一个变量,如果您创建并初始化这样的实例,则可以使用该变量来引用它。要声明变量并创建实例,您需要执行以下操作:
此外,这一行:
根本不执行任何操作。 -description 方法返回一个字符串,但您不会将返回的字符串存储在任何地方,也不会对其执行任何其他操作。
我可以继续,但我不会 - 我给你的建议是去苹果的网站并阅读他们的 学习 Objective-C 教程,以及从那里链接的其他文章。这样做将是一种比仅仅编造东西并期望编译器理解它更有效的学习语言的方法。
The bottom line here is that this line:
The above does not create an instance of Foo. All it does is declare a variable that can, if you create and initialize such an instance, be used to refer to it. To declare the variable and create the instance, you need to do this:
Additionally, this line:
That does nothing at all. The -description method returns a string, but you're not storing the returned string anywhere, or doing anything else with it.
I could go on, but I won't - my advice to you is to go to Apple's site and read their Learning Objective-C tutorial, as well as the additional articles linked from there. Doing that will be a far more effective way to learn the language than just making stuff up and expecting the compiler to understand it.
您没有将 release 发送到 nil 变量。就像在 C 中一样,
创建一个未初始化的堆栈变量,其中可以包含任何垃圾值。在 Mac 上,它可能恰好具有值 0 (nil),而在 iPhone 上,它恰好具有导致崩溃的其他值。
You're not sending release to a nil variable. Just as in C,
creates an uninitialized stack variable, which can contain any garbage value. On your Mac, it is presumably happening to have the value 0 (nil), while on the iPhone it is happening to have some other value that results in a crash.