作者dryman (dryman)
看板MacDev
標題[心得] dot syntax
時間Wed Feb 18 11:04:18 2009
dot syntax很好用,不過官方obj-c並沒有教怎麼寫
(至少沒有寫在很明顯的地方,最後我是在cocoaDev找到寫法的)
code:
@interface MyClass : NSObject
@property int foo;
@property(getter=_getBaz,setter=_setBaz) float baz;
@end
@implementation MyClass
- (int)_getBaz { return baz; }
- (void)_setBaz: (float)newBaz { baz = abs(newBaz); }
@end
@protocol MyProtocol
@property(copies,readonly) NSString *bar;
@end
int main(void) {
MyClass *m = [MyClass new];
m.baz = -5.0;
printf("%f\n", m.baz); /* -> 5.0000 */
}
對版上大部分的人來說大概早就已經會了:p
不過還是有一些新手可能會用到,所以就分享一下囉
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.4.234
1F:→ yllan:dot syntax is evil… 02/18 11:54
2F:→ dryman:我試了這個程式碼發現不能用耶... 02/18 13:37
3F:→ zonble:這年頭請愛用 NSInteger 與 CGFloat :D 02/18 13:38
4F:→ adrianshum:為什麼 dot syntax is evil? (可能我慣寫 C++...) 02/18 16:09
5F:推 HalfLucifer:請問property用dot syntax有什麼問題? @_@ 02/18 18:00
6F:→ yllan:會讓你光看 code 分不清這到底是 object or struct or ... 02/18 21:17