简体   繁体   English

保留Objective-c(再次但更精确)

[英]retain with objective-c (again but with more precision)

I post this question again but with more precision this time, 我再次发布这个问题,但这次更准确,

first, I have this function who return a string, there is some error about memory management or this fonction is ok? 首先,我有一个返回字符串的函数,有关内存管理的错误或此功能还可以吗?

-(NSString *) motAvecCle:(NSString *) cle
{
    NSString *motRetour;

    motRetour = @"";

    cle = [FonctionUtile concatener:[[tableauConfig singletonTableauConfig] langueAffichage] chaine2:@"_" chaine3:[FonctionUtile trim:[cle uppercaseString]] chaine4:@""];

    motRetour =  [FonctionUtile trim:[dictionnaireLangue objectForKey:cle]];

    if (motRetour == nil) {
        motRetour = @"Erreur";
    } 

    return motRetour;
}

and when I call this fonction, 当我称此功能为

NSString *myString = @"";
myString = [self motAvecCle:@"fr"]; // I must do this?
myString = [[self motAvecCle:@"en"]retain]; //or do this?

thx again... 再次......

The method motAvecCle: returns an object you do not own. motAvecCle:方法motAvecCle:返回您不拥有的对象。 Therefore, at some point it is going to disappear. 因此,在某些时候它会消失。 Whether you care or not depends on where myString is defined. 是否关心取决于myString的定义位置。 If it's in the same scope: 如果它在相同的范围内:

-(void) foo
{
    NSString *myString = [self motAvecCle:@"fr"];
    // do some stuff
}

you do not want to retain it (except in one circumstance) because the reference will disappear when foo exits which means if you had retained it you'd need to release it again first. 你不想保留它(除了在一种情况下),因为当foo退出时引用将消失,这意味着如果你保留它,你需要先再次释放它。

The one circumstance for retaining is if you modify the object you got the string from ie self in this case. 保留的一种情况是,如果您修改了对象,则在这种情况下是从self那里获得的字符串。 That might cause the string to go away (although probably not in your specific example). 可能会导致字符串消失(尽管在您的特定示例中可能不会)。

If myString is an instance variable of the object, you do want to retain it because otherwise it will disappear (possibly) the next time the auto release pool is drained. 如果myString是对象的实例变量,您确实希望保留它,否则它将在下次自动释放池耗尽时消失(可能)。 However, before assigning the instance variable, you must be sure to release the old value of the instance variable, unless it's actually the same string you are assigning ie you need to do something like this: 但是,在分配实例变量之前,必须确保释放实例变量的旧值,除非它实际上是您指定的相同字符串,即您需要执行以下操作:

-(void) foo
{
    NSString *tmp = [[self motAvecCle:@"fr"] retain]; // it's a string, technically you should copy, not retain
    [myString release];
    myString = tmp;
    // do some stuff
}

Since you'd have to do that every time you want to assign the ivar, it's normal to create an accessor eg 由于每次想要分配ivar时都必须这样做,所以创建一个访问器是正常的,例如

-(void) setMyString: (NSString*) newValue
{
    NSString* tmp = [newValue copy];
    [myString release];
    myString = tmp;
}

-(void) foo
{
    [self setMyString: [self motAvecCle:@"fr"]];
    // do some stuff
}

If you use properties, you can use @synthesize to create the accessors. 如果使用属性,则可以使用@synthesize创建访问器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM