iOS单例和内存管理 [英] iOS Singletons and Memory Management

查看:626
本文介绍了iOS单例和内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定我缺少对iOS内存管理的一些基本了解,尽管有大量的阅读和搜索,我仍然没有得到它。



我在我的应用程序中使用单身,有关于当前登录的用户的信息,从多个视图控制器访问的信息等。它有多个ivar,在整个应用程序中获取和设置。它们在.h文件中声明和适用:

  NSString * myString; 

并保留如下:

  @property(non atomic,retain)NSString * myString; 













$ b <我得到和设置他们的值在方法在单例如下:

  myString = @value 

  methodLocalString = myString; 

在其他地方,我包括单例 - 称为 CurrentUser 我导入它:

  #importCurrentUser.h

除了单例,我得到和设置如下:

  [CurrentUser sharedCurrentUser] .myString = @Bob; 

  myOutsideString = [CurrentUser sharedCurrentUser] .myString; 

大多数情况下,这是非常好的,值从一个获取或设置持久保存到另一个。麻烦的是,有时当我得到他们的方式,我发现他们已经释放(崩溃的应用程序),NSZombieEnabled幸运地告诉我。



我没有得到的是他的情况。我认为单例从未被释放,并且为此保留单例的属性将永远不会被释放。我会注意到,这个问题似乎更常见的非真实对象属性,如NSDate和绝对非对象属性,如int和BOOL,无法保留,但它也发生对象属性。 p>

我在这里不知道什么?感谢您的耐心等待。

解决方案

您的问题是:


我在单例中的方法
中获得并设置它们的值,如下所示:



myString = @value;


当直接分配给iVar而不是使用属性语法( self。 myString = @value),您将绕过合成的setter方法,这意味着 retain 从不发生。



属性不是魔术。他们只是一点点的语法糖为。

  self.myString = 

@值;

只是

的简写:

  [self setMyString:@value]; 

合成的 setMyString

  if(myString!= newValue){
[myString release];
myString = [newValue retain];
}

(假设 retain 选项@synthesize)


I'm certain that I'm missing some fundamental understanding of iOS memory management and, despite lots of reading and searching, I'm still not getting it.

I use a singleton in my app that has info about the user currently signed into it, info accessed from multiple view controllers, etc. It has multiple ivars that are get and set throughout the app. They're declared and propertied in the .h file like so:

NSString *myString;

and are made retained like so:

@property (non atomic, retain) NSString *myString;

and synththesized in the implementation.

I get and set their values in methods in the singleton like this:

myString = @"value";

and

methodLocalString = myString;

In other places I include the singleton -- call it CurrentUser -- I import it:

#import "CurrentUser.h"     

Outside of the singleton I get and set it like this:

[CurrentUser sharedCurrentUser].myString = @"Bob";

and

myOutsideString = [CurrentUser sharedCurrentUser].myString;

Most of the time this works great, with the values appropriately persisted from one getting or setting to another. The trouble is that sometimes when I get them that way I find that they've been released (crashing the app), which NSZombieEnabled thankfully tells me.

What I don't get is how his can happen. I thought the singleton was never released, and that therefor retained properties of the singleton would never be released. I'll note that the problem seems to be more common with non-real-object properties like NSDate and definitely-not-object properties like int and BOOL which can't be retained, but it happens with object properties as well.

What am I ignorant about here? And thanks for your patience.

解决方案

Your problem is:

I get and set their values in methods in the singleton like this:

myString = @"value";

When you assign directly to the iVar, instead of using the property syntax (self.myString = @"value"), you are bypassing the synthesized setter method, which means that the retain never happens.

Properties aren't magic. They're just a bit of syntactic sugar for the "." access, and the ability to have synthesized getter/setter methods to save you the tedium of writing your own.

self.myString = @"value";

is just shorthand for

[self setMyString:@"value"];

The synthesized setMyString method will do something like:

if (myString != newValue) {
    [myString release];
    myString = [newValue retain];
}

(assuming retain option on the @synthesize)

这篇关于iOS单例和内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆