首页 > 代码库 > OC中的静态成员变量

OC中的静态成员变量

代码:

main.m:

#import <Foundation/Foundation.h>#import "MyObject.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        [MyObject setFlag:YES];        NSLog(@"%@", [MyObject flag] ? @"YES" : @"NO");    }    return 0;}

MyObject.h:

#import <Foundation/Foundation.h>@interface MyObject : NSObject+ (BOOL)flag;+ (void)setFlag:(BOOL)flag;@end

MyObject.m:

#import "MyObject.h"static BOOL _flag;@implementation MyObject+ (BOOL)flag {    return _flag;}+ (void)setFlag:(BOOL)flag {    _flag = flag;}@end

 输出:

YES

 

OC中的静态成员变量