Objective C 何时使用 alloc,何时不使用

发布于 2024-12-14 10:49:06 字数 297 浏览 2 评论 0原文

我正在尝试学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靑春怀旧 2024-12-21 10:49:06

+alloc 的问题在于它会保留其结果,这就是为什么它必须稍后通过调用 -release-autorelease 来平衡的原因在。为了避免每次使用类时都必须一遍又一遍地输入该内容,API 设计者通常会创建所谓的便捷构造函数便捷方法+URLWithString: 是其中之一,在内部它看起来像这样:

+ (id)URLWithString: (NSString *)str {
    return [[[self alloc] initWithString: str] autorelease];
}

所以 +alloc 正在为您调用,+alloc 也是为您调用的,+alloc 也是为您调用的。 >-自动释放。

背景

Objective-C 中有两大类方法:类方法和实例方法。类方法被发送到类本身,并且不需要创建该类的实例。实例方法被发送到实例并可以访问实例占用的内存。类方法以+开头;带有 - 的实例方法。

+alloc 是一个类方法。它并不是一种具有特定物体创造能力的魔法。它在内部所做的一切都是这样的:(

+ (id)alloc {
    id result = malloc(class_getInstanceSize(self));
    if (result) {
        memset(result, 0, class_getInstanceSize(self));
        result->isa = self;
        result->retainCount = 1;
    }
    return result;
}

实际上比这更复杂一些,但在这里应该足够了。)请注意,+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:

+ (id)URLWithString: (NSString *)str {
    return [[[self alloc] initWithString: str] autorelease];
}

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:

+ (id)alloc {
    id result = malloc(class_getInstanceSize(self));
    if (result) {
        memset(result, 0, class_getInstanceSize(self));
        result->isa = self;
        result->retainCount = 1;
    }
    return result;
}

(It's actually a little more complicated than that but it should suffice here.) Note that +alloc is defined to be part of NSObject, 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.

美人如玉 2024-12-21 10:49:06

使用 alloc 方法创建一个由您拥有的新对象。我的意思是像这样创建它

NSURL *url =[[NSURL allo]initWithString:@"http://www.apple.com"]
您成为该对象的所有者。在你使用它之后,可以说

NSLog ("Url path is %@",url);

你必须释放这个对象(url)

[url release];

这是关于 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

NSLog ("Url path is %@",url);

You must release this object (url)

[url release];

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文