Objective C 何时使用 alloc,何时不使用
我正在尝试学习 Objective C,我发现很奇怪的一件事是何时使用 alloc,何时不使用。以这段代码为例:
NSURL *url =[NSURL URLWithString:@"http://www.apple.com"];
为什么您不必先执行类似的操作来分配它?
UIAlert *alert = [[UIAlertView alloc]]
我确信目标 C 中只是缺少一些基本的东西,但奇怪的是,我很难在不发布的情况下找到解释。谢谢!!
I'm trying to learn objective C and one of the things i find very weird to follow is when to use alloc and when not to. Take for instance this snip of code:
NSURL *url =[NSURL URLWithString:@"http://www.apple.com"];
Why don't you have to do something like this to alloc it first?
UIAlert *alert = [[UIAlertView alloc]]
I'm sure there is just some basic thing in objective C i'm missing, but oddly enough am having a hard time finding an explanation without posting. Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
+alloc
的问题在于它会保留其结果,这就是为什么它必须稍后通过调用-release
或-autorelease
来平衡的原因在。为了避免每次使用类时都必须一遍又一遍地输入该内容,API 设计者通常会创建所谓的便捷构造函数或便捷方法 。+URLWithString:
是其中之一,在内部它看起来像这样:所以
+alloc
正在为您调用,+alloc
也是为您调用的,+alloc
也是为您调用的。 >-自动释放。背景
Objective-C 中有两大类方法:类方法和实例方法。类方法被发送到类本身,并且不需要创建该类的实例。实例方法被发送到实例并可以访问实例占用的内存。类方法以
+
开头;带有-
的实例方法。+alloc
是一个类方法。它并不是一种具有特定物体创造能力的魔法。它在内部所做的一切都是这样的:(实际上比这更复杂一些,但在这里应该足够了。)请注意,
+alloc
被定义为NSObject
的一部分, 不是所有对象的一部分。 Cocoa内存管理,有+alloc
、-init
、-retain
、-release
等并不总是Objective-C 的一部分,并且可以创建不使用它的对象。如果您知道正确的咒语,您实际上可以创建类的实例,而无需调用
+alloc
。我不会推荐它。The problem with
+alloc
is that it retains its result, which is why it must be balanced with a call to-release
or-autorelease
later on. To avoid having to type that out over and over every time a class is used, API designers commonly create what's called a convenience constructor or convenience method.+URLWithString:
is one of them, and internally it looks like this:So
+alloc
is getting called for you, and so is-autorelease
.Background
There are two broad kinds of methods in Objective-C: class methods and instance methods. Class methods are sent to a class itself and do not require the creation of an instance of that class. Instance methods are sent to an instance and can access the memory that instance occupies. Class methods start with
+
; instance methods with-
.+alloc
is a class method. It's not a magical one with particular object-creating powers. All it does internally is something like:(It's actually a little more complicated than that but it should suffice here.) Note that
+alloc
is defined to be part ofNSObject
, not part of all objects. Cocoa memory management, with+alloc
,-init
,-retain
,-release
, etc. is not always part of Objective-C, and objects can be created that don't use it.You can actually create instances of a class without calling
+alloc
if you know the right incantations. I wouldn't recommend it.使用 alloc 方法创建一个由您拥有的新对象。我的意思是像这样创建它
NSURL *url =[[NSURL allo]initWithString:@"http://www.apple.com"]
,您成为该对象的所有者。在你使用它之后,可以说
你必须释放这个对象(url)
这是关于 Objective C 内存管理的主要主题之一
你的声明怎么样,
NSURL *url =[NSURL URLWithString:@"http://www .apple.com"];
我必须说系统会返回一个带有字符串的url,但它不属于你,所以你不需要释放它,因为系统会自动释放它(到这个对象自动发送autorelease消息)
Use alloc method to create a new object, owned by you. I mean creating it like this
NSURL *url =[[NSURL allo]initWithString:@"http://www.apple.com"]
,you become owner of this object. After you using it , lets say
You must release this object (url)
This is one of main topic about memory managment in Objective C
What about your statment ,
NSURL *url =[NSURL URLWithString:@"http://www.apple.com"];
I must say that system will return a url with a string , but wich is not OWNED BY YOU , so you dont need to release it , because system automatically will release it (to this object automatically is send autorelease message)