//
// Things.h
// ClassExtension
//
// Created by on 14-9-10.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Things : NSObject
@property (assign) NSInteger thing1;
@property (readonly, assign) NSInteger thing2;
- (void) resetAllValues;
@end
//
// Things.m
// ClassExtension
//
// Created by on 14-9-10.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import "Things.h"
// 类扩展,注意声明方式
@interface Things ()
{
NSInteger thing4;
}
// 这里的特性还有assign:默认,引用计数不增加
// retain:引用计数增加1
// 原子性:atomic默认。
// 非原子性:nonatomic
// atomic是OC中的一种线程保护技术,是防止在未完成的时候被另外一个线程使用,造成数据错误
// 这里将原来的readonly改成了readwrite
@property (readwrite, assign) NSInteger thing2;
@property (assign) NSInteger thing3;
@end
// 这里是Things的实现
@implementation Things
@synthesize thing1;
@synthesize thing2;
@synthesize thing3;
- (void) resetAllValues {
// self.thing1是调用了setter方法
self.thing1 = 200;
self.thing2 = 300;
self.thing3 = 400;
// 这是给thing4变量赋值
thing4 = 5;
}
- (NSString *) description {
NSString *desc = [NSString stringWithFormat: @"%ld %ld %ld",
(long)thing1, (long)thing2, (long)thing3];
return desc;
} // description
@end
//
// main.m
// ClassExtension
//
// Created by 吴家锋 on 14-9-10.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Things.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Things *things = [[Things alloc] init];
things.thing1 = 1;
// 这里调用thing2的setter方法会出错,因为thing2特性为readonly
// readwrite特性是在Things.m文件中的类扩展定义的,只能在Things.m中访问,为私有
// 类扩展都为私有
// things.thing2 = 2;
NSLog(@"%@", things);
[things resetAllValues];
NSLog(@"%@", things);
}
return 0;
}