English 中文(简体)
@property(retain)做了些什么?
原标题:What does @property(retain) do?

@propert(retain)做了些什么? 我的测试实际上没有保留我的物体:

id obj = getObjectSomehow();
NSLog(@"%d", [obj retainCount]);
propertyWithRetain = obj;
NSLog(@"%d", [obj retainCount]);

// output:
// 1
// 1

我怎么能够使财产真正保留物体?

最佳回答

你们不要在那里使用你的财产,为什么不保留!

引证:

id obj = getObjectSomehow();
NSLog(@"%d", [obj retainCount]);
self.propertyWithRetain = obj; // Note the self. :)
NSLog(@"%d", [obj retainCount]);

Using self. will use the property. Just using the variable name won t.


EDIT especially for @bbum (who raises a very fair point in the comments)

Don t依赖使用Cunt, 你们不知道还有什么东西保留了你的物体, 你们不知道有些保留者是否实际安排了自动释放,这样它通常会产生误导性:

问题回答
propertyWithRetain = obj;

这只是直接夺回财产。 当合成“property”时,如果没有宣布的事例变数,则自动产生。 以上是直接使用这一工具。

self.propertyWithRetain = obj;

这将实际上通过<条码>@synthesized setter和保值。

这也是为什么我们许多人使用<代码>@synsize property WithRetain = PropertyWithRetain_; to causes the iVar to be大同。

请注意,即便如此,打电话retainCount<>/code>也可能令人产生可怕的误解。 标有<代码> 印有:2;或不间断地加以说明。 确实,没有打字<代码> 可持续<>。 从来没有。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签