Skip to content

Effective Objective-C 总结

Published: at 02:21 PM

第一节

第二节

第三节

NSNumber *intNumber = @1;
NSNumber *floatNumber = @1.1f;
NSNumber *doubleNumber = @1.123456;
NSNumber *boolNumber = @YES;
NSNumber *charNumber = @'a';

int numberInt = 5;
float numberFloat = 1.23f;

NSArray *animals = @[@"cat", @"dog", @"mouse"];
NSString *dog = @"shiba";

NSDictionary *personData = @{@"firstName" : @"Stephen",
														 @"lastName" : @"Fang",
														 @"gender" : @"male"};
NSString *dog = animals[1];
NSString *lastName = personData[@"lastName"];

第四节

static const NSTimeInterval kAnimationDuration = 0.3;
// In the header file
extern NSString *const EOCStringConstant;

// In the implementation file 
NSString *const EOCStringConstant = @"DEMO";

第五节

typedef NS_ENUM(NSUInteger, STFFeedPageType) {
    STFFeedPageTypeNone = 0,             // 未知
    STFFeedPageTypeFollow = 1,           // 关注页
    STFFeedPageTypeRecommend = 2,        // 推荐页
    STFFeedPageTypeFeatured = 3,         // 精选页
};

typedef NS_OPTIONS(NSUInteger, AgoraAudioSessionOperationRestriction) {
    /** No restriction, the SDK has full control of the audio session operations. */
    AgoraAudioSessionOperationRestrictionNone              = 0,
    /** The SDK does not change the audio session category. */
    AgoraAudioSessionOperationRestrictionSetCategory       = 1,
    /** The SDK does not change any setting of the audio session (category, mode, categoryOptions). */
    AgoraAudioSessionOperationRestrictionConfigureSession  = 1 << 1,
    /** The SDK keeps the audio session active when leaving a channel. */
    AgoraAudioSessionOperationRestrictionDeactivateSession = 1 << 2,
    /** The SDK does not configure the audio session anymore. */
    AgoraAudioSessionOperationRestrictionAll               = 1 << 7
};

第六节

第七节

- (STFComponent*)component {
		if (!_component) {
					_component = [SFComponent new];
			}
			return _component;
		}

第八节

- (BOOL)isEqual:(id)object {
		if (self == object) return YES; // 指向同一个对象必然相等
		if ([self class] != [object class]) return NO; //不属于同一个类不相等

		// 检测每一个属性是否相等
		STFPerson *someone = (STFPerson*) object;
		if (![_firstName isEqualToString:someone.firstName])
			return NO;
		if (![_lastName isEqualToString:someone.lastName])
			return NO;
		if (![_age != someone.age])
			return NO;
		return YES;
	}

 - (NSUInteger)hash {
		NSUInteger firstNameHash = [_firstName hash];
		NSUInteger lastNameHash = [_lastName hash];
		NSUInteger ageHash = [_ageHash hash];
		return firstNameHash ^ lastNameHash ^ ageHash;
	}
-  (BOOL)isEqual:(id)object {
		if ([self class] == [object class]) 
				return [self isEqualToPerson:(STFPerson*)object];
		} else {
				return [super isEqual: object];
		}

第九节

第十节

第十一节

第十二节

第十三节

第十四节

第十五节

第十六节

第十七节

第十八节

第十九节

如果从其他框架中继承子类务必遵循命名惯例,如从UIView中继承自定义子类则类名末尾词为View,若创建自定义委托协议末尾应跟上Delegate一词。

第二十节

第二十一节

- (BOOL)doSomething:(NSError**) error

- (BOOL)doSomething:(NSError**) error {
	// do something that may cause an error

	if ( /* there was an error */ ) {
			if (error) {
					*error = [NSErrorerrorwithDomain:domain code:code userInfo:userInfo];
			}
			return NO;
		} else {
		return YES;
		}
}

NSError *error = nil;
BOOL ret = [object doSomething: &error];
if (ret) {
     // handle error 
}

传递给方法的是个指针,指针本身指向另一个指向NSError对象的指针,或认为其为一个直接指向NSError对象的指针。在 ARC 中,指针所指向的对象会在方法执行完毕后自动释放。

第二十二节

- (id)copyWithZone:(NSZone*)zone

- (id)copyWithZone:(NSZone*)zone {
		STFPerson *copy = [[self class] allocWithZone:zone]initWithFirstName:_firstName andLastName:_lastName];
		return copy;
}

第二十三节

存放委托对象的属性需要为weak(在对象销毁时自动清空)或unsafe_unretained(不需要自动清空)。

第二十四条

第二十五条

第二十六条

第二十七条

第二十八条

第二十九条

第三十条

第三十一条

第三十二条

第三十三条

第三十四条

系统会自动创建一些线程,如主线程或GCD机制中的线程都有自动释放池,这些线程都有autoreleasepool,每次执行event loop就会将其清空

第三十五条

第三十六条

在 ARC 下调用查询对象当前引用计数的方法会触发崩溃

```objectivec
- (NSUInteger)retainCount
```

事实上该方法不应该调用,方法返回的retainCount是某个给定时间点上的数值,并未考虑系统会稍后清空自动释放池,无法反应对象生命期的全貌。

第三十七条

第三十八条

第三十九条

第四十条

大部分网络通信库写法

为了使得在下载完成后通过以下方法执行调用者指定的Block,需要将completion handler保存至实例变量,一旦运行完completion handler之后没有必要对其进行保留,从而避免出现retain cycle

第四十一条

第四十二条

第四十三条

第四十五条

第四十六条

第四十七条

第四十八条

第四十九条

第五十条

缓存的一般用法

加入 NSPurgeable 的缓存用法

第五十一条

第五十二条