Objective-C 内存处理 (iPhone)

发布于 2024-08-23 20:06:17 字数 608 浏览 2 评论 0原文

我不能说我真的理解 Objective-C 中的内存处理,所以我有几个与之相关的问题。

  • 我是否必须删除下面框中的对象“url”和“urlRequest”,还是“urlConnection”承担这样做的责任?

    NSURL* url = [NSURL URLWithString:url];
    NSURLRequest* urlRequest = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection* urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    
  • 以下对象创建之间有什么区别。是参考号。在所有情况下都保留计数器吗?

    [[NSString alloc] init];  
    [[NSString alloc] initWithFormat:...];  
    [NSString stringWithString:...];  
    
  • 分配属性时,是引用。无论是否将“分配”或“保留”设置为属性,计数始终保留?

I can't say I really understand the memory handling in Objective-C so I have a couple of questions concerning that.

  • Do I have to remove the objects "url" and "urlRequest" in the box below or does "urlConnection" take on the responsibility for doing that?

    NSURL* url = [NSURL URLWithString:url];
    NSURLRequest* urlRequest = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection* urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    
  • What is the difference between the following object creations. Is the ref. counter retained in all cases?

    [[NSString alloc] init];  
    [[NSString alloc] initWithFormat:...];  
    [NSString stringWithString:...];  
    
  • When assigning a property, is the ref. count always retained regardless of whether "assign" or "retain" was set as attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

罪#恶を代价 2024-08-30 20:06:17

一般来说,如果您通过以 alloc、new 或 copy 开头的方法获取对象,则您将负责释放该对象。因此,在您的第一个查询中,您需要释放 urlRequest 和 urlConnection。 url 对象是不需要释放的对象的示例,因为它是使用静态工厂方法 (URLWithString) 实例化的。

[[NSString alloc] init];

将初始化一个保留计数为 1 的 NSString。

[[NSString alloc] initWithFormat:...];  

同样,会生成一个保留计数为 1 的 NSString。唯一的区别是您调用了不同的初始值设定项。

[NSString stringWithString:...];

创建一个自动释放的 NSString,保证在当前事件循环期间保持有效。

对于property属性,assign不会保留传递给setter的对象。

我知道这有点枯燥,但是内存管理指南< /a> 对于此类问题来说是一个非常好的参考。

Generally speaking if you obtain an object through a method begining alloc, new or copy, you become responsible for releasing that object. Hence in your first query you'll need to release urlRequest and urlConnection. The url object is an example of an object which you don't need to release, as it is instantiated using a static factory method (URLWithString).

[[NSString alloc] init];

Will initialise a NSString with a reatin count of 1.

[[NSString alloc] initWithFormat:...];  

Again, results in a NSString with retain count of 1. The only difference is you've called a different initializer.

[NSString stringWithString:...];

Creates an autoreleased NSString that is guranteed to remain valid during the current event loop.

As for property attributes, assign will not retain the object passed to the setter.

I know it's a bit dry but the Memory Management Guidelines are a really good reference for this type of question.

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