NSArray 派生出新的集合
-(NSArray *)arrayByAddingObject:(id)anObject
//add a new array,and return a new NSArray
-(NSArray *)arrayByAddingObjectsFromArray:(NSArray *)anotherarray
//add all the elements inanother array and return a new nsarray original array not change
-(NSArray *)subarrayWithRange:(NSArray) range
//load elements within range
#pragma 派生出新的数组
void arrayNew(){
NSArray *array=[NSArray arrayWithObjects:@"1",@"2",nil];
NSArray *array2=[arrayarrayByAddingObject:@"3"];//合并数组
//原来的array没有变,只是返回的array2变化了
NSArray *array3=[arrayarrayByAddingObjectsFromArray:[NSArray arrayWithObject:@"4",@"5",nil]];//4 elements
NSArray *array4=[array arrayWithObjects:@"1",@"2",@"3",@"4",nil];
NSRange range= NSMakeRange(1,2);
NSArray *array5=[array4 subarrayWithRange:range];
}
Other method.
-(NSString)componentsJoinedByString:(NSString *)seperator
//Use a seperator to joint all the elements as a string
//用一个分隔符将数组中的所有元素拼接成一个字符串
-(BOOL)write To File:(NSString *)path atomically:(BOOL)use AuxiliaryFile
//将一个NSArray持久化到文件中去
#pregma mark other method
void arrayOther(){
NSArray *array=[array arrayWithObjects:@"1",@"2",@"3",@"4",nil];
NSString *string=[array componentsJoinedByString:@","];// 1,2,3,4
NSString *path=@"/Users/apple/Desktop/array.xml";
[arry writeToFile:path atomically:YES];//create a xml file
//从文件中读取数组内容(文件有严格的格式要求)
NSString *path=@"/Users/apple/Desktop/array.txt";
NSArray *array2=[NSArray arrayWithContentsOfFile:path];
}
NSArray 排序1
-(NSArray *)sortedArrayUsingSelector:(SEL)comparator
//return a new sorted array ,original array will not change
#pragma mark arraysort
void arraySort1(){
NSArray *array=[array arrayWithObjects:@"2",@"3",@"1",@"4",nil];
NSArray *array2=[array sortedArrayUsingSelector:@selector(compare:)];
// [@"1" compare:@"2"]; 按照传入的比较方法进行排序
//Default order is from smaller to bigger
}
NSArray 排序2
//例如这样调用(假设array里面装着Student对象);
-(NSArray *)sortedArrayUsingComparator:(comparator)cmptr
//typedef NSComoarisionResult (^NSComparator)(id obj1,id obj2);
//[array sortedArrayUsingComparatorResult:^(Student *s1,Student *s2){
return [s1.name compare:s2.name;]
}];
//按照name排序
Student.h
@interface Student:NSObject
@property(nonatomic,retain)NSString *firstname;
@property(nonatomic,retain)NSString *lastname;
+(id)studentWithFirstName:(NSString *)firstname lastname:(*NSString *)lastname;
//Pass to @selector and return value must be NSComparisionResult
-(NSComparisionResult)compareStudent:(Student *)stu;
@end
Student.m
@implementation
+(id)studentWithFiestName:(NSString *)firstname lastname:(*NSString *)lastname{
Student *stu=[[[Student alloc]init]autorelease];
//because of static method + so we need to add autorelease.
stu.lastname=lastname;
stu.firstname=firstname;
return stu;
}
//We can store lastname reult first
-(NSComparisionResult)compareStudent:(Student *)stu{
//first sort with lastname then firstname
NSComparisionResult result=[self.lastname compare:stu.lastname];
//if has same lastname then compare the firstname
if(result==NSOrderSame){
result=[self.firstname compare:stu.firstname];
}
return result;
}
}
-(void)dealloc{
[_firstname release];
[_lastname release];
[super dealloc];
}
//Rewrite description method
//Yesi
-(NSString *)description{
return [NSString stringWithFormat:@"[%@ %@]",self.lastname,self.firstname];
}
@end
-----------------------
#progma mark 数组排序2
#import "Student.h"
void arraySort2(){
Student *stu1=[Student studentWithFirstname::@"Yesi" lastname:@“Huang”];
Student *stu2=[Student studentWithFirstname::@"jujubee" lastname:@“Sharma”];
Student *stu3=[Student studentWithFirstname::@"hitu" lastname:@”Sharma”];
Student *stu4=[Student studentWithFirstname::@"madani" lastname:@”Green”];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4,nil];
//now need to sort with firstname then lastname
NSArray *array2=[array sortedArrayUsingSelector:@selctor(compareStudent)];
}
//NSArray排序3 利用block (block完全没懂好么,roleback to see again)
#pragma mark Using block to sort
#import "Student.h"
void arraySort3(){
Student *stu1=[Student studentWithFirstname::@"Yesi" lastname:@“Huang”];
Student *stu2=[Student studentWithFirstname::@"jujubee" lastname:@“Sharma”];
Student *stu3=[Student studentWithFirstname::@"hitu" lastname:@”Sharma”];
Student *stu4=[Student studentWithFirstname::@"madani" lastname:@”Green”];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4,nil];
//Using a block to compare
NSArray *araray2=[array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1,Student *obj2){
//copy algorithm here...double click can add a block quickly
//first sort with lastname then firstname
NSComparisionResult result=[obj1.lastname compare:obj2.lastname];
//if has same lastname then compare the firstname
if(result==NSOrderSame){
result=[obj1.firstname compare:obj2.firstname];
}
return result;
}];
NSLog(@”array2:%@”,array2);
}
//NSArray排序4 Student has a subclass book
//sort first with bookname then lastname then firstname
#pragma mark Using block to sort Student has a book
Book.h
@interface Book:NSObject
@property (nonatomic,retain) NSString *name;
+(id)bookWithName:(NSString *)name;
@end
Book.m
@implementation Book
+(id)bookWithName:(NSString *name){
Book *book=[[[Book alloc]init]autorelease];
book.name=name;
return book;
}
-(void)dealloc{
[_name release];
[super dealloc];
}
@end
----------------------
Student.h //add
@class Book;
@property (nonatomic,retain) Book *book;
+(id)studentWithFirstName:(NSString *)firstname lastname:(NSString *)lastname bookName:(NString *)bookname;
Student.m//add
+(id)studentWithFirstName:(NSString *)firstname lastname:(NSString *)lastname bookName:(NString *)bookname{
Student *stu=[Student studentWithFirstname:firstname lastname:lastname];
stu.book=[Book bookWithName:bookname]; //no memory leak
return stu;
}
Student.m//add
#import "Book.h"
-(void)dealloc{
[_firstname release];
[_lastname release];
[_book release];
[super dealloc];
}
---------------------
void arraySort4(){
Student *stu1=[Student studentWithFirstname::@"Yesi" bookName:@"book1"];
Student *stu2=[Student studentWithFirstname::@"jujubee" bookName:@"book2"];
Student *stu3=[Student studentWithFirstname::@"hitu" bookName:@"book2"];
Student *stu4=[Student studentWithFirstname::@"madani" bookName:@"book1"];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4,nil];
//1先按照书名进行排序
NSSortDescriptor *bookNameDesc=[NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
//2再按照姓
NSSortDescriptor *lastameDesc=[NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
//3最后按照名字
NSSortDescriptor *firstnameDesc=[NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
NSArray array2=[array sortedArrayUsingDescriptors:]
}
NSArray 派生出新的集合
-(NSArray *)arrayByAddingObject:(id)anObject
//add a new array,and return a new NSArray
-(NSArray *)arrayByAddingObjectsFromArray:(NSArray *)anotherarray
//add all the elements inanother array and return a new nsarray original array not change
-(NSArray *)subarrayWithRange:(NSArray) range
//load elements within range
#pragma 派生出新的数组
void arrayNew(){
NSArray *array=[NSArray arrayWithObjects:@"1",@"2",nil];
NSArray *array2=[array arrayByAddingObject:@"3"];//合并数组
//原来的array没有变,只是返回的array2变化了
NSArray *array3=[array arrayByAddingObjectsFromArray:[NSArray arrayWithObject:@"4",@"5",nil]];//4 elements
NSArray *array4=[array arrayWithObjects:@"1",@"2",@"3",@"4",nil];
NSRange range= NSMakeRange(1,2);
NSArray *array5=[array4 subarrayWithRange:range];
}
Other method.
-(NSString)componentsJoinedByString:(NSString *)seperator
//Use a seperator to joint all the elements as a string
-(BOOL)write To File:(NSString *)path atomically:(BOOL)use AuxiliaryFile
//讲一个NSArray持久化到文件中去
#pregma mark other method
void arrayOther(){
NSArray *array=[array arrayWithObjects:@"1",@"2",@"3",@"4",nil];
NSString *string=[array componentsJoinedByString:@","];// 1,2,3,4
NSString *path=@"/Users/apple/Desktop/array.xml";
[arry writeToFile:path atomically:YES];//create a xml file
//从文件中读取数组内容(文件有严格的格式要求)
NSString *path=@"/Users/apple/Desktop/array.txt";
NSArray *array2=[NSArray arrayWithContentsOfFile:path];
}
NSArray 排序1
-(NSArray *)sortedArrayUsingSelector:(SEL)comparator
//return a new sorted array ,original array will not change
#pragma mark arraysort
void arraySort1(){
NSArray *array=[array arrayWithObjects:@"2",@"3",@"1",@"4",nil];
NSArray *array2=[array sortedArrayUsingSelector:@selector(compare:)];
// [@"1" compare:@"2"]; 按照传入的比较方法进行排序
//Default order is from smaller to bigger
}
NSArray 排序2
//例如这样调用(假设array里面装着Student对象);
-(NSArray *)sortedArrayUsingComparator:(comparator)cmptr
//typedef NSComoarisionResult (^NSComparator)(id obj1,id obj2);
//[array sortedArrayUsingComparatorResult:^(Student *s1,Student *s2){
return [s1.name compare:s2.name;]
}];
//按照name排序
Student.h
@interface Student:NSObject
@property(nonatomic,retain)NSString *firstname;
@property(nonatomic,retain)NSString *lastname;
+(id)studentWithFiestName:(NSString *)firstname lastname:(*NSString *)lastname;
//Pass to @selector and return value must be NSComparisionResult
-(NSComparisionResult)compareStudent:(Student *)stu;
@end
Student.m
@implementation
+(id)studentWithFiestName:(NSString *)firstname lastname:(*NSString *)lastname{
Student *stu=[[[Student alloc]init]autorelease];
//because of static method + so we need to add autorelease.
stu.lastname=lastname;
stu.firstname=firstname;
return stu;
}
//We can store lastname reult first
-(NSComparisionResult)compareStudent:(Student *)stu{
//first sort with lastname then firstname
NSComparisionResult result=[self.lastname compare:stu.lastname];
//if has same lastname then compare the firstname
if(result==NSOrderSame){
result=[self.firstname compare:stu.firstname];
}
return result;
}
}
-(void)dealloc{
[_firstname release];
[_lastname release];
[super dealloc];
}
//Rewrite description method
//Yesi
-(NSString *)description{
return [NSString stringWithFormat:@"%@ %@",self.lastname,self.firstname];
}
@end
-----------------------
#progma mark 数组排序2
#import "Student.h"
void arraySort2(){
Student *stu1=[Student studentWithFirstname::@"Yesi"];
Student *stu2=[Student studentWithFirstname::@"jujubee"];
Student *stu3=[Student studentWithFirstname::@"hitu"];
Student *stu4=[Student studentWithFirstname::@"madani"];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4,nil];
//now need to sort with firstname then lastname
NSArray *array2=[array sortedArrayUsingSelector:@selctor(compareStudent)];
}
//NSArray排序3 利用block (block完全没懂好么,roleback to see again)
#pragma mark Using block to sort
#import "Student.h"
void arraySort3(){
Student *stu1=[Student studentWithFirstname::@"Yesi"];
Student *stu2=[Student studentWithFirstname::@"jujubee"];
Student *stu3=[Student studentWithFirstname::@"hitu"];
Student *stu4=[Student studentWithFirstname::@"madani"];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4,nil];
//Using a block to compare
[array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1,Student *obj2){//copy algorithm here...double click can add a block quickly
//first sort with lastname then firstname
NSComparisionResult result=[obj1.lastname compare:obj2.lastname];
//if has same lastname then compare the firstname
if(result==NSOrderSame){
result=[obj1.firstname compare:obj2.firstname];
}
return result;
}];
}
//NSArray排序4 Student has a subclass book
//Using sort descriptor 使用排序描述器,z指定优先级
//sort first with bookname then lastname then firstname
#pragma mark Using block to sort
Book.h
@interface Book:NSObject
@property (nonatomic,retain) NSString *name;
+(id)bookWithName:(NSString *)name;
@end
Book.m
@implementation Book
+(id)bookWithName:(NSString *name){
Book *book=[[[Book alloc]init]autorelease];
book.name=name;
return book;
}
-(void)dealloc{
[_name release];
[super dealloc];
}
@end
----------------------
Student.h //add
@class Book;
@property (nonatomic,retain) Book *book;
+(id)studentWithFirstName:(NSString *)firstname lastname:(NSString *)lastname bookName:(NString *)bookname;
Student.m//add
+(id)studentWithFirstName:(NSString *)firstname lastname:(NSString *)lastname bookName:(NString *)bookname{
Student *stu=[Student studentWithFirstname:firstname lastname:lastname];
stu.book=[Book bookWithName:bookname];//no memory leak
return stu;
}
Student.m//add
#import "Book.h"
-(void)dealloc{
[_firstname release];
[_lastname release];
[_book release];
[super dealloc];
}
-(NSString *)description{
return [NSString stringWithFormat:@”[%@ %@-%@]”,self.lastname, sel.firstname, _book.name]; //self.book.name
}
---------------------
void arrarSort4(){
Student *stu1=[Student studentWithFirstname::@"Yesi" lastname:@“Huang”bookName:@”book1”];
Student *stu2=[Student studentWithFirstname::@"jujubee" lastname:@“Sharma”bookName:@”book1”];
Student *stu3=[Student studentWithFirstname::@"hitu" lastname:@”Sharma”bookName:@”book2”];
Student *stu4=[Student studentWithFirstname::@"madani" lastname:@”Green”bookName:@”book2”];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4,nil];
//1先按照书名进行排序 ascending 升序
NSSortDescriptor *bookNameDesc=[NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
//2再按照姓
NSSortDescriptor *lastameDesc=[NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
//3最后按照名字
NSSortDescriptor *firstnameDesc=[NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
//注意以上需用Student.m里面@property后面的一致的名字
//按顺序添加排序描述器,决定排序的优先级
NSArray *descs=[NSArray arrayWithObjects:bookNameDesc,lastameDesc,firstnameDesc,nil];
NSArray array2=[array sortedArrayUsingDescriptors:descs];
NSLog(@”array2:%@”,array2);
}