Class Defines
@interface SimpleClass : NSObject
{
NSString *_myNonPropertyInstanceVariable;
}
@end
Properties
@interface Person : NSObject
atomic原子,多线程保护
@property (readonly is readwrite getter=isFinished atomic nonatomic strong(默认) weak copy) NSString *firstName; (内部用_firstName存储)
@property NSString *lastName;
@end
@implement Person
{
NSString *_anotherCustomInstanceVariable;//不想公开
}
//没有下面这句,就默认是@synthesize propertyName = _propertyName
@synthesize propertyName = instanceVariableName;(指定存储值)
@end
init方法
- (id)init {
self = [super init];
if (self) {
// initialize instance variables here
}
return self;
}
实例方法
- (XYZObject *)someImportantObject {
if (!_someImportantObject) {
_someImportantObject = [[XYZObject alloc] init];
}
return _someImportantObject;
}
类方法
+ (XYZObject *)someImportantObject {
if (!_someImportantObject) {
_someImportantObject = [[XYZObject alloc] init];
}
return _someImportantObject;
}
strong 修饰的属性会在赋值时调用被指向对象的 retain 方法,导致其引用计数加1 。weak 则不会。另外还有个 unsafe_unretained,跟 weak 类似,区别是被指向对象消失时不会“自动“变成 nil 。
NSString *someString = @"Hello, World!";
NSNumber *myBOOL = @YES;
NSNumber *myFloat = @3.14f;
NSNumber *myInt = @42;
NSNumber *myLong = @42L;
不需要强类型
运行时报错
id someObject = @”Hello, World!”;
[someObject removeAllObjects];
编译时报错
NSString *someObject = @”Hello, World!”;
[someObject removeAllObjects];
if ([firstPerson isEqual:secondPerson]) {
// firstPerson is identical to secondPerson
}
if ([someDate compare:anotherDate] == NSOrderedAscending) {
// someDate is earlier than anotherDate
}
XYZPerson *somePerson;(会自动=nil)
// somePerson is automatically set to nil
属性自动会synthesize,需要定制才需要显式synthesize
get方法同属性名
set方法在属性名前面加set大写
[object valueForKey:(NSString*)]可以通过属性名来访问属性值
属性对应的变量名前面加了下划线_
@synthesize firstName; 变量同属性名
//下面是默认实习,有没有都一样
@synthesize firstName = _firstName
-(id)init
{
self = [super init];
}
- (void)dealloc {
NSLog(@”XYZPerson is being deallocated”);
}
//分类,主要用来实现私有。可以在.m中用category
@interface ClassName (CategoryName)
@end
@implementation XYZPerson (XYZPersonNameDisplayAdditions)
- (NSString *)lastNameFirstNameString {
return [NSString stringWithFormat:@”%@, %@”, self.lastName, self.firstName];
}
@end
Category
用于给class及其subclass添加新的方法
有自己单独的 .h 和 .m 文件
用于添加新方法,而不能添加新属性(property)
Extension
Extension常被称为是匿名的Category
用于给类添加新方法,但只作用于原始类,不作用于subclass
只能对有implementation源代码的类写Extension,对于没有implementation源代码的类,比如framework class,是不可以的
Extension可以给原始类添加新方法,以及新属性
@interface XYZPerson : NSObject
…
@property (readonly) NSString *uniqueIdentifier;
- (void)assignUniqueIdentifier;
@end
//下面隐藏信息,只有调用着才能调用、看到。需要源码实现。
@interface XYZPerson ()
@property (readwrite) NSString *uniqueIdentifier;
@end
@implementation XYZPerson
…
@end
Protocols有点像接口声明
@protocol XYZPieChartViewDataSource
@require
- (NSUInteger)numberOfSegments;
- (CGFloat)sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;
@optional
- (NSString *)titleForSegmentAtIndex:(NSUInteger)segmentIndex;
@end
@protocol XYZPieChartViewDataSource
- (NSUInteger)numberOfSegments;
- (CGFloat)sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;
@optional
- (NSString *)titleForSegmentAtIndex:(NSUInteger)segmentIndex;
@end
if ([self.dataSource respondsToSelector:@selector(titleForSegmentAtIndex:)]) {
thisSegmentTitle = [self.dataSource titleForSegmentAtIndex:index];
}
//接口(协议)继承
@protocol MyProtocol
…
@end
//实现借口
@interface MyClass : NSObject
…
@end
@interface MyClass : NSObject