我們知道在objective-c 語言中,支援property的方式來為變數宣告getset方法,還有synthesize的方式為變數完成getset方法。那麼當省略synthesize的時候,或者synthesize的時候指明操作的變數時等。情況又是怎麼樣呢?
@inte***ce test :nsobject
@property (nonatomic,retain)nsstring str;
@end
@implementation test
像這種情況,實際上test類有兩個變數,乙個str ,還有乙個_str,後者是有系統自動產生。
這種情況只有str乙個變數。@inte***ce test :nsobject
@property (nonatomic,retain)nsstring str;
@end
@implementation test
@synthesize str;
這種情況也只有str乙個變數。@inte***ce test :nsobject
@property (nonatomic,retain)nsstring str;
@end
@implementation test
這樣的是test有乙個_str變數@inte***ce test :nsobject
@property (nonatomic,retain)nsstring str;
@end
@implementation test@synthesize str = _str;
所以寫property ,synthesize以及定義類變數時候,要注意一下,以免操作錯誤的類變數
iOS面試 屬性 成員變數
成員變數 是不與外界接觸的變數,應用於類的內部。ps 如果你說那用 public 外部不就是可以訪問了麼?確實是可以,但是 public 只能適當使用,不要氾濫,否則就像你把鑰匙插在你自己家門上了,誰來都可以開門,毫無安全性。屬性 個人認為最大的好處就是讓其他物件可以訪問這個變數。而且你可以設定唯讀...
iOS成員變數和屬性
成員變數的優點 1.能夠定義作用於 protect public package private 2.不走setter和getter方法,讀取速度快 這個走的是什麼 屬性1.能夠定義讀寫屬性,原子屬性,setter和getter方法名稱 2.不需要自己去管理記憶體 這裡有個疑問 就是在解除迴圈引用的...
ios變數和屬性的使用建議
inte ce裡宣告的變數和property宣告的變數區別。我認為有兩點 1.放在.h檔案裡的property是乙個public屬性,可以供外部類訪問。2.property和ivars宣告,本來就是兩個東西,乙個是通過setter和getter去訪問物件,而另乙個是直接訪問。property可以通過...