2014-04-29 69 views
2

讓我們假設有一個UIView包含UILabel內部。的財產語義轉發到複製的財產在Objective-C

UIView的公共界面如下,在它的.h

@interface MyView : UIView 

@property (nonatomic, copy) NSString *text; 

@end 

,私下內它的.m

@interface MyView() 

@property (nonatomic, strong) UILabel coolLabel; 

@end 

與它的.m實現:

@implementation 

- (void)setText:(NSString *)text 
{ 
    self.coolLabel.text = text; 
} 

- (NSString *)text 
{ 
    return self.coolLabel.text; 
} 

@end 

尋找一個T中的公共接口我宣佈@property (nonatomic, copy) NSString *textcopy因爲文字是由內部的coolLabel複製。但是,我不確定這是否正確。

應該聲明@property (nonatomic, assign) NSString *textassign,而不是因爲我的班級沒有專門執行內存管理?或者是我的當前接口聲明正確?

僅供參考,一切假設ARC

回答

4

text屬性應該是copy因爲最終它的價值正在被標籤複製。

property定義是一樣的合同。您正在告訴用戶該物業的價值將如何處理。由於標籤複製文本,您應該指出您的資產正在被複制。

+0

這是否在ARC如何運作的任何方式改變? –

+1

不是。 ARC會做正確的事情。 – rmaddy