Re: [問題] Property的關係
※ 引述《darktt (小朱)》之銘言:
推
06/17 09:02,
06/17 09:02
→
06/17 09:03,
06/17 09:03
顯然你還是一整個沒搞懂...
@interface Foo : NSObject
{
}
@property (weak, nonatomic) UIImage *image;
@end
@implementation Foo
@synthesize image = _image;
@end
這個宣告大略等同於
@interface Foo : NSObject
{
UIImage *_image;
}
- (UIImage *)image;
- (void)setImage:(UIImage *)newImage;
@end
@implementation Foo
- (UIImage *)image
{
return _image;
}
- (void)setImage:(UIImage *)newImage
{
_image = newImage;
}
@end
(實際上 implementation 產生的對等程式碼視 property 的各種參數會有不同, 這裡
的對等程式碼對應的 nonatomic, weak 只是最簡單的狀況)
仔細看看後面的那段, 根本沒有 image 這個變數
簡單的概念, 當你寫 @synthesize image = _image; 時, 前面那個 image 代表
property 的名稱, 後面的 _image 代表 instance variable 的名稱, 所以你只能
使用 self.image (在 Objective-C class 中等同於 [self image]) 或 _image,
不能用 image (因為根本沒有這個變數!)
這樣有懂了嗎...
--
我還是覺得 Objective-C 初學者應該要全力避免使用 property 和 dot-syntax
直到他們完全了解這兩個東西背後的意義為止
不然這兩個東西根本超容易搞爛改念讓人寫出無法言喻的程式碼...
--
Les grandes et les meilleurs tone from "Zadok the Priest"
Eine grosse stattliche Veranstaltung by F. Handel
THE MAIN EVENT! These are the men
Sie sind die Besten
"Champions League" by Tony Britten THESE ARE THE CHAMPIONS!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 60.245.64.174
※ 編輯: uranusjr 來自: 60.245.64.174 (06/17 16:08)
推
06/17 23:27, , 1F
06/17 23:27, 1F
→
06/17 23:29, , 2F
06/17 23:29, 2F
推
06/19 00:06, , 3F
06/19 00:06, 3F
→
06/19 00:07, , 4F
06/19 00:07, 4F
推
06/19 06:41, , 5F
06/19 06:41, 5F
→
06/19 06:42, , 6F
06/19 06:42, 6F
→
06/19 07:16, , 7F
06/19 07:16, 7F
→
06/19 13:05, , 8F
06/19 13:05, 8F
→
06/19 13:06, , 9F
06/19 13:06, 9F
→
06/19 14:25, , 10F
06/19 14:25, 10F
→
06/19 14:28, , 11F
06/19 14:28, 11F
推
07/02 11:11, , 12F
07/02 11:11, 12F
討論串 (同標題文章)
MacDev 近期熱門文章
PTT數位生活區 即時熱門文章