ios学习笔记--(oc基础4)
2015-08-05 18:41
393 查看
NSArray 和NSMutableArray
NSArray 和NSMutableArray 的一些基本用法根据一个题目:通过所学知识对西游记师徒进行情景编码,按照收徒的先后顺序依次加入取经团队,可以通过姓名,年龄,本领进行排序。例如,猪悟能可以离开团队回高老庄。唐僧因误会可以赶走孙悟空等等。
main.m // // main.m // oc_8.5_1 // // Created by dq on 15/8/5. // Copyright (c) 2015年 dq. All rights reserved. // #import <Foundation/Foundation.h> #import "Prentice.h" #import "Tangceng.h" int main(int argc, const char * argv[]) { @autoreleasepool { // 作业:通过所学知识对西游记师徒进行情景编码,按照收徒的先后顺序依次加入取经团队,可以通过姓名,年龄,本领进行排序。例如,猪悟能可以离开团队回高老庄。唐僧因误会可以赶走孙悟空等等。 Tangceng *tangceng = [Tangceng personWithName:@"tangceng" andAge:30 andAbility:1]; NSMutableArray *qujingTeam = [NSMutableArray arrayWithObject:tangceng]; Prentice *sunwukong = [Prentice personWithName:@"sunkukong" andAge:1500 andAbility:100]; Prentice *zhupajie = [Prentice personWithName:@"zhupajie" andAge:1800 andAbility:70]; Prentice *shaheshang = [Prentice personWithName:@"shaheshang" andAge:1600 andAbility:60]; //孙悟空 猪八戒 沙和尚 分别加入取经队伍 [qujingTeam addObject:sunwukong]; [qujingTeam addObject:zhupajie]; [qujingTeam addObject:shaheshang]; //排序 //1利用选择器对年龄排序 NSLog(@"按照姓名升序输出(利用选择器)"); [qujingTeam sortUsingSelector:@selector(compareWithname:)]; for (Person *per in qujingTeam) { NSLog(@"%@",per); } //利用特征对年龄排序 NSSortDescriptor *sortdes = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]; [qujingTeam sortUsingDescriptors:@[sortdes]]; NSLog(@"按照年龄特征升序(利用特征)"); for (Person *per in qujingTeam) { NSLog(@"%@",per); } //利用代码块排序 [qujingTeam sortUsingComparator:^NSComparisonResult(Person * obj1, Person * obj2) { if (obj1.ability > obj2.ability) { return NSOrderedDescending; } else if (obj1.ability < obj2.ability) { return NSOrderedAscending; } else { return NSOrderedSame; } }]; NSLog(@"按照代码块生序排序(利用代码块)"); //利用数组转枚举 快速便利枚举 NSEnumerator *enumerator = [qujingTeam objectEnumerator]; id per; while (per = [enumerator nextObject]) { NSLog(@"%@",per); } //悟空被赶走 [qujingTeam removeObject:sunwukong]; NSLog(@"悟空被赶走了 队伍只剩下:"); for (Person *per in qujingTeam) { NSLog(@"%@",per.name); } //八戒回高老庄了 [qujingTeam removeObject:zhupajie]; NSLog(@"八戒回高老庄 队伍只剩下:"); for (Person *per in qujingTeam) { NSLog(@"%@",per.name); } } return 0; } person.h // Person.h // oc_8.5_1 // // Created by dq on 15/8/5. // Copyright (c) 2015年 dq. All rights reserved. // #import <Foundation/Foundation.h> @interface Person : NSObject { NSString *_name; int _age; int _ability; } @property(nonatomic,strong) NSString *name; @property(nonatomic,assign) int age; @property(nonatomic,assign) int ability; /** * 自定义初始化参数 * * @param aName 姓名 * @param aAge 年龄 * @param aAbility 能力值 满分�� * * @return 对象 */ -(instancetype)initWithName:(NSString *)aName andAge:(int)aAge andAbility:(int)aAbility; /** * 便利构造器 * */ +(instancetype)personWithName:(NSString *)aName andAge:(int)aAge andAbility:(int)aAbility; /** * 选择器 排序 姓名的比较方法 * * @param other 比较的对象 * * @return 返回比较的结果 */ -(NSComparisonResult)compareWithname:(Person *)other; @end person.m // // Person.m // oc_8.5_1 // // Created by dq on 15/8/5. // Copyright (c) 2015年 dq. All rights reserved. // #import "Person.h" @implementation Person -(instancetype)initWithName:(NSString *)aName andAge:(int)aAge andAbility:(int)aAbility { if (self == [super init]) { _name = aName; _age = aAge; _ability = aAbility; } return self; } +(instancetype)personWithName:(NSString *)aName andAge:(int)aAge andAbility:(int)aAbility { Person *person = [[Person alloc]initWithName:aName andAge:aAge andAbility:aAbility]; return person; } -(NSComparisonResult)compareWithname:(Person *)other { return [self.name compare:other.name]; } - (NSString *)description { return [NSString stringWithFormat:@"我是%@ 年龄%d 能力值%d",_name,_age,_ability]; } @end Tanceng.h // // Tangceng.h // oc_8.5_1 // // Created by dq on 15/8/5. // Copyright (c) 2015年 dq. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" @interface Tangceng : Person @end Tangceng.m // // Tangceng.m // oc_8.5_1 // // Created by dq on 15/8/5. // Copyright (c) 2015年 dq. All rights reserved. // #import "Tangceng.h" @implementation Tangceng @end Prentice.h // // Prentice.h // oc_8.5_1 // // Created by dq on 15/8/5. // Copyright (c) 2015年 dq. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" @interface Prentice : Person //@property(nonatomic,strong) NSString *name; //@property(nonatomic,assign) int age; //@property(nonatomic,assign) int ability; //-(instancetype)initWithName:(NSString *)aName andAge:(int)aAge andAbility:(int)aAbility; //+(instancetype)prenticeWithName:(NSString *)aName andAge:(int)aAge andAbility:(int)aAbility; @end Prentice.m // // Prentice.m // oc_8.5_1 // // Created by dq on 15/8/5. // Copyright (c) 2015年 dq. All rights reserved. // #import "Prentice.h" @implementation Prentice //-(instancetype)initWithName:(NSString *)aName andAge:(int)aAge andAbility:(int)aAbility //{ // if (self == [super init]) { // _name = aName; // _age = aAge; // _ability = aAbility; // } // return self; //} //+(instancetype)prenticeWithName:(NSString *)aName andAge:(int)aAge andAbility:(int)aAbility //{ // Prentice *prentice = [[Prentice alloc]initWithName:aName andAge:aAge andAbility:aAbility]; // return prentice; //} @end
相关文章推荐
- iOS 8 Xcode6 设置Launch Image 启动图片<转>
- iOS Core Animation
- 《iOS 高级编程》之Tableview进阶指南
- 通过WireShark抓取iOS联网数据实例分析
- ios js call ios
- iOS开发中常用到的宏
- 快快快!27个提升效率的iOS开源库推荐
- iOS 动画系列之动画解释
- IOS给图片添加水印(两种方式)
- IOS7开发~Images.xcassets
- iOS 开发 解决EXC_BAD_ACCESS错误的一种方法--NSZombieEnabled(转)
- IOS 实现下拉菜单
- iOS dSYM文件结构剖析(下)
- iOS开发中常用的颜色及其对应的颜色值
- IOS状态栏
- 全面理解iOS开发中的Scroll View[转]
- iOS中几种定时器
- iOS 百度地图开发集成使用 (转)
- iOS培训-培训成果:成都生活展示
- Ios userDefaultes本地存储