首先先在.h宣告一個class property。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface MyClass : NSObject | |
@property (class, nonatomic, copy) NSString *someString; | |
@end |
接著在.m實作Getter與Setter。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation MyClass | |
static NSString *_someString; | |
///定義Setter方法 | |
+ (void)setSomeString:(NSString *)someString{ | |
_someString = someString; | |
} | |
///定義Getter方法 | |
+ (NSString *)someString{ | |
return _someString; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyClass.someString = @"ABC"; | |
NSLog(@"%@",MyClass.someString); |
沒有留言:
張貼留言