Which way is the correct way to allocate memory with a property?

后端 未结 4 864
谎友^
谎友^ 2020-12-22 08:24

Which way is correct?

NSString *c = [[NSString alloc] init];
self.character = c;
[c release];

or

self.character = [[NSStrin         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-22 08:37

    It depends on how your property is declared. If you used

    @property (nonatomic, retain) NSString *someString;
    

    The setter will be created to retain someString, in which case the correct way is your first method. If you used:

    @property (nonatomic, assign) NSString *someString;
    

    Your second method will be correct, since it will just assign the pointer and not retain anything.

提交回复
热议问题