简体   繁体   English

与目标C一起保留

[英]retain with objective-c

I have a question about a retain and a NSString, if I have a method who a return a NSString, and I put the return NSString in a nsstring variable, I must do a retain or not? 我有一个关于保留和NSString的问题,如果我有一个返回NSString的方法,并且将返回NSString放在nsstring变量中,我是否必须保留?

NSString *myString = @"";
myString = [self methodWhoReturnString]; // I must do this?
myString = [[self methodWhoReturnString]retain]; // Or I must do this?

The Apple Developer Documentation on Memory Management explains the scenarios where you retain/release objects. 有关内存管理Apple开发人员文档介绍了保留/释放对象的方案。

Simply put, if you want the string to stick around, you need to retain it until you're finished with it. 简而言之,如果您想让弦线缠住,则需要保留它,直到完成为止。 If that is just the scope of the current function, you can get away without retaining it as if the string is already autorelease'd (likely) it won't get released until your function finishes and the current AutoReleasePool is purged. 如果那仅仅是当前函数的范围,那么您可以不保留它而逃脱,就好像字符串已经被自动释放(可能)直到函数完成并且清除了当前的AutoReleasePool才会释放它一样。

Bear in mind that an NSString * could actually be pointing to an NSMutableString *. 请记住,NSString *实际上可能指向NSMutableString *。 If it matters to you if the string is changed by some other function without you realizing, be sure to copy it: NSString * myCopyOfString = [mystring copy]; 如果在您NSString * myCopyOfString = [mystring copy];通过其他函数更改了字符串对您来说很重要,请确保将其复制: NSString * myCopyOfString = [mystring copy];

If the string is set to autorelease, which it most likely is, then yes you will need to retain it somehow. 如果字符串设置为自动释放(很可能是这样),则是的,您将需要以某种方式保留它。 I would suggest doing this though: 我建议这样做:

myString = [[self methodWhoReturnString] copy];

this ensures you have retained the data in the string not just a reference to a string that might still be controlled elsewhere. 这样可以确保您保留了字符串中的数据,而不仅仅是保留对字符串的引用,而该字符串可能仍会在其他位置进行控制。 Be sure you release your copy later! 确保稍后再发布副本!

Any method that has alloc , new or copy in it automatically retains and infers that you have ownership of the object. 任何具有allocnewcopy的方法都会自动保留并推断您对该对象拥有所有权。 All others shouldn't. 所有其他人都不应该。 It would be helpful if you had more context though. 但是,如果您有更多的背景信息,将很有帮助。 If we are in a contained method where this string is used briefly, then you probably don't need to retain. 如果我们在一个包含的方法中简短地使用了此字符串,那么您可能不需要保留。 If it is going to be used for a while, you might want to use the @synthesize syntax to make it a property of the class you are in. When you use @property and @synthesize and call something like self.myProperty = something it will automatically retain. 如果要使用一段时间,则可能需要使用@synthesize语法使其成为您所在类的属性。当使用@property@synthesize并调用诸如self.myProperty = something ,将自动保留。

Usually, methodWhoReturnString would return an autoreleased string, which means you should retain it if you want to keep it around. 通常, methodWhoReturnString将返回一个自动释放的字符串,这意味着如果要保留它,则应保留它。

So, if methodWhoReturnString is your method, I believe that to keep with convention you should return [stringToReturn autorelease]; 因此,如果methodWhoReturnString是您的方法,我相信为了遵守约定,您应该return [stringToReturn autorelease]; from that method, and then retain it if you want to keep it. 从该方法,然后保留它(如果要保留它)。

You use retain if you're going to be using myString at a later point in time (ie after the current method has returned) to prevent it being autoreleased. 如果要在以后的某个时间点(即返回当前方法之后)使用myString来防止其自动释放,则可以使用retain

You don't need to use retain if it's just a temporary variable used within the current method, since in that case you do want it to be autoreleased. 如果仅仅是在当前方法中使用的临时变量,则不需要使用retain ,因为在这种情况下,您确实希望将其自动释放。

One special case is properties. 一种特殊情况是属性。 If you call self.blah = foo , you don't need to retain foo, since the setBlah: method should do it for you. 如果调用self.blah = foo ,则不需要保留foo,因为setBlah:方法应该为您完成此操作。

(there's a whole load of other complexities and edge cases, but this is a good rule of thumb to get you started on understanding what to do) (还有很多其他复杂性和边缘情况,但这是一个很好的经验法则,可以帮助您开始了解如何做)

Given the code you provided, you shouldn't call -retain . 给定您提供的代码,您不应调用-retain In your example, the return value of a method that returns an instance of NSString is assigned to myString , an automatic local variable. 在您的示例中,将返回NSString实例的方法的返回值分配给自动局部变量myString If the assignment had been made to an instance variable or a static variable, you would want to call either retain or copy to guarantee that the reference remains valid beyond the end of the local scope. 如果已对实例变量或静态变量进行了分配,则可能需要调用retaincopy来确保引用在局部作用域之外仍然有效。

In this case though, the reference to the NSString instance is stored in a variable that will be destroyed automatically at the end of the local scope, so your code needn't concern itself with the object's lifetime. 但是,在这种情况下,对NSString实例的引用存储在一个变量中,该变量将在本地作用域的末尾自动销毁,因此您的代码不必关心对象的生存期。

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

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