NSArray
1.1数组副本中的数据元素与原中数据元素的深浅拷贝
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TRStudent *stu1 = [TRStudent studentWithName:@"张三" andAge:18];
TRStudent *stu2 = [TRStudent studentWithName:@"李四" andAge:20];
TRStudent *stu3 = [TRStudent studentWithName:@"王五" andAge:19];
NSArray *stu = @[stu1,stu2,stu3];
NSArray *copied = [NSArray arrayWithArray:stu];//生成的副本中数据元素与原本中数据元素的浅(引用计数)拷贝
self.outputLabel.text = [NSString stringWithFormat:@"%p\n%p",stu,copied];
self.outputLabel.text = [NSString stringWithFormat:@"%p\n%p",stu[0],copied[0]];
NSArray *copiedDeeply = [[NSArray alloc]initWithArray:stu copyItems:YES];//生成的副本中数据元素与原本中数据元素是深拷贝
self.outputLabel.text = [NSString stringWithFormat:@"%p\n%p",stu[0],copiedDeeply[0]];
}
1.2排序
1.2.1数组中的所有元素必须是同一个类的对象
1.2.2在该类的.h文件中,添加排序规则方法的声明。方法名为-(NSComparisonResult)compare...(TRInteger*)other;
1.2.3在该类的.m文件中,实现排序规则方法
1.2.4在需要排序处,用待排序的数组对象调用sortedArrayUsingSelector方法进行排序
TRStudent.h
@interface TRStudent : NSObject
@property NSString *name;
@property int age;
-(instancetype)initWithName:(NSString*)name andAge:(int)age;
+(id)studentWithName:(NSString*)name andAge:(int)age;
-(NSComparisonResult)compareAge:(TRStudent*)other;
@end
TRStudent.m
@implementation TRStudent
-(instancetype)initWithName:(NSString *)name andAge:(int)age
{
self = [super init];
if(self)
{
self.name = name;
self.age = age;
}
return self;
}
+(id)studentWithName:(NSString *)name andAge:(int)age
{
__autoreleasing TRStudent *s = [[TRStudent alloc]initWithName:name andAge:age];
return s;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"姓名:%@,年龄:%d",self.name,self.age];
}
-(NSComparisonResult)compareAge:(TRStudent *)other
{
NSNumber *selfAge = [NSNumber numberWithInt:self.age];
NSNumber *otherAge = [NSNumber numberWithInt:other.age];
return [selfAge compare:otherAge];
}
@end
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TRInteger *i1 = [TRInteger integertWithInteger:10];
TRInteger *i2 = [TRInteger integertWithInteger:3];
TRInteger *i3 = [TRInteger integertWithInteger:8];
TRInteger *i4 = [TRInteger integertWithInteger:17];
TRInteger *i5 = [TRInteger integertWithInteger:5];
NSArray *array = @[i1,i2,i3,i4,i5];
NSArray *sorted = [array sortedArrayUsingSelector:@selector(compare:)];
self.outputLabel.text = [NSString stringWithFormat:@"%@",sorted];
TRStudent *stu1 = [TRStudent studentWithName:@"风清" andAge:22];
TRStudent *stu2 = [TRStudent studentWithName:@"李沁" andAge:12];
TRStudent *stu3 = [TRStudent studentWithName:@"彭于晏" andAge:28];
TRStudent *stu4 = [TRStudent studentWithName:@"冯德伦" andAge:45];
TRStudent *stu5 = [TRStudent studentWithName:@"郭富城" andAge:36];
TRStudent *stu6 = [TRStudent studentWithName:@"刘德华" andAge:55];
TRStudent *stu7 = [TRStudent studentWithName:@"张学友" andAge:35];
TRStudent *stu8 = [TRStudent studentWithName:@"周星驰" andAge:25];
NSArray *stu = @[stu1,stu2,stu3,stu4,stu5,stu6,stu7,stu8];
NSArray *sorted1 = [stu sortedArrayUsingSelector:@selector(compareAge:)];
// self.outputLabel.text = [NSString stringWithFormat:@"%@",sorted1];
NSMutableString *str = [NSMutableString stringWithCapacity:15 *8];
for (TRStudent*s in sorted1)
{
[str appendFormat:@"%@\n",s];
}
self.outputLabel.text = str;
}
网友评论