#import <Foundation/Foundation.h>
@interface Phone : NSObject
@property (nonatomic,copy,readonly) NSString *name;
@property (nonatomic,assign,readonly) NSInteger price;
- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price;
@end
#import "Phone.h"
//分类中重新声明
@interface Phone()
@property (nonatomic,copy,readwrite) NSString *name;
@property (nonatomic,assign,readwrite) NSInteger price;
@end
@implementation Phone
- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price{
self = [super init];
if (self) {
_name = [name copy];
_price = price;
}
return self;
}
@end
#import <Foundation/Foundation.h>
@interface Phone : NSObject
@property (nonatomic,copy,readonly) NSString *name;
@property (nonatomic,assign,readonly) NSInteger price;
@property (nonatomic,strong,readonly) NSArray *phoneArray;
- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price;
- (void)addPhone:(Phone *)phone;
- (void)removePhone:(Phone *)phone;
@end
#import "Phone.h"
@interface Phone()
@property (nonatomic,copy,readwrite) NSString *name;
@property (nonatomic,assign,readwrite) NSInteger price;
@property (nonatomic,strong,readwrite) NSMutableArray *savePhoneArray;
@end
@implementation Phone
- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price{
self = [super init];
if (self) {
_name = [name copy];
_price = price;
_savePhoneArray = [[NSMutableArray alloc]init];
}
return self;
}
- (instancetype)init{
return [self initWithName:@"iPhone" withPrice:999];
}
-(NSMutableArray *)savePhoneArray{
if (_savePhoneArray == nil) {
_savePhoneArray = [NSMutableArray new];
}
return _savePhoneArray;
}
- (void)addPhone:(Phone *)phone{
[self.savePhoneArray addObject:phone];
}
- (NSArray *)phoneArray{
return [self.savePhoneArray copy];
}
- (void)removePhone:(Phone *)phone{
[self.savePhoneArray removeObject:phone];
}
@end
Effective+Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法