undeclared identifier

Use of undeclared identifier 'XXX'; did you mean '_XXX'?

Object-C在2.0引入@property
但要注意的是,@property自動幫我們生成的getter/setter中,實際的變數名稱會被加上底線(underline),例如名為"ABC"的變數透過property宣告後,其實際可被存取的名稱是"_ABC"

.h file
@property(nonatomic) NSString *contentOfTest;

.m file
// 錯誤用法
contentOfTest =  @"這是測試";

// 正確用法1 (use the property)
self.contentOfTest  = @"這是測試";

// 正確用法2 (use the generated instance variable)
_contentOfTest  = @"這是測試"; 

沒有留言: