您的位置:首页 > 其它

类别

2015-08-09 09:42 197 查看
类别
// 如果在Category和类中有相同的方法名
// Category 优先
// 如果在两个Category中有两个重名的方法,那么在complie source中谁在后面,就会执行哪一个

在ARC中,strong,weak用于对象的内存管理
strong考虑对象的内存管理 相当于 MRC 中的retain
weak不考虑内存管理,相当于assign,但是用于对象的内存管理
assign 主要用于基本数据类型 在 ARC 和 MRC 中基本数据类型都用assign

Category的应用

NSString *string = @"Hello world123";
NSLog(@"%d",[NSString numberCountOfString:string]);
NSLog(@"%d",[string numberOfCount]);

// 类方法
+ (int)numberCountOfString:(NSString *)string  {

return [string numberOfCount];
}

// 实例方法
- (int)numberOfCount {

int count = 0;
for (NSUInteger i = 0; i < self.length; i++) {
unichar ch = [self characterAtIndex:i ];
if (ch < '9' && ch > '0') {
count++;
}
}
return count;
}

- (void)sortBySelector:(SEL)cmp;
// isOlderThanDog:(dog)
- (void)sortBySelector:(SEL)cmp {
for (NSUInteger i = 0; i < self.count-1; i++) {
for (NSUInteger j = i+1; j < self.count; j++)
// if selfi > selfj 那么交换
if ([self[i] performSelector:cmp withObject:self[j]])
[self exchangeObjectAtIndex:i withObjectAtIndex:j];

}
}

类别
// 如果在Category和类中有相同的方法名
// Category 优先
// 如果在两个Category中有两个重名的方法,那么在complie source中谁在后面,就会执行哪一个

在ARC中,strong,weak用于对象的内存管理
strong考虑对象的内存管理 相当于 MRC 中的retain
weak不考虑内存管理,相当于assign,但是用于对象的内存管理
assign 主要用于基本数据类型 在 ARC 和 MRC 中基本数据类型都用assign

Category的应用

NSString *string = @"Hello world123";
NSLog(@"%d",[NSString numberCountOfString:string]);
NSLog(@"%d",[string numberOfCount]);

// 类方法
+ (int)numberCountOfString:(NSString *)string  {

return [string numberOfCount];
}

// 实例方法
- (int)numberOfCount {

int count = 0;
for (NSUInteger i = 0; i < self.length; i++) {
unichar ch = [self characterAtIndex:i ];
if (ch < '9' && ch > '0') {
count++;
}
}
return count;
}

- (void)sortBySelector:(SEL)cmp;
// isOlderThanDog:(dog)
- (void)sortBySelector:(SEL)cmp {
for (NSUInteger i = 0; i < self.count-1; i++) {
for (NSUInteger j = i+1; j < self.count; j++)
// if selfi > selfj 那么交换
if ([self[i] performSelector:cmp withObject:self[j]])
[self exchangeObjectAtIndex:i withObjectAtIndex:j];

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: