类别
2015-08-09 09:42
211 查看
类别 // 如果在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]; } }
相关文章推荐
- IIS部署站点相关经验总结
- Upgrade from Windows 8 to Windows 10
- C语言:数组指针、指针数组、指针的指针、函数指针
- Android开发:shape和selector和layer-list的(详细说明)
- BOX
- webservice之jdk自带wsimport
- Zend Framework教程-入门相关
- 【机房收费系统】 之 收费模式
- canvas移动端常用技巧图片loading
- zendframework1.0框架程手册
- setNeedsDisplay和setNeedsLayout
- hdu 5339 Untitled dfs
- 淘宝全球购发布十年发展报告
- Bootstrap--组件之下拉菜单
- 新浪微博passport登录原理
- 进入公司第五届、六个月
- mysql 执行计划走分区
- 数据仓库基础术语名词一览
- 视频不显示的分析方法
- IE8下console undefined