//
// main.m
// foundation-NSArray2
//
// Created by apple on 15/6/29.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#pragma mark 派生新的数组
void arrayNew(){
NSArray *array = [NSArray arrayWithObjects:@"1",@"2", nil];
NSLog(@"array:%@",array);
NSArray *array2 = [array arrayByAddingObject:@"3"];
NSLog(@"array:%@",array2);
NSArray *array3 =[array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"a",@"a",@"a",@"a",@"a", nil]];
NSLog(@"array:%@",array3);
NSArray *array4 = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8", nil];
NSRange range = NSMakeRange(3, 5);
NSArray *array5 = [array4 subarrayWithRange:range];
NSLog(@"array:%@",array5);
}
#pragma mark 数组的其他用法
void arrayOther(){
NSArray *array = [NSArray arrayWithObjects:@"1",@"2", nil];
NSString *str = [array componentsJoinedByString:@"*"];
NSLog(@"产生的字符串:%@",str);
NSString *path = @"/Users/apple/Desktop/test.xml";
//将一个数组写入一个文件(xml格式)
//[array writeToFile:path atomically:YES];
//从文件中读取数组,有严格的格式要求
NSArray *arr1 = [NSArray arrayWithContentsOfFile:path];
NSLog(@"产生的数组是:%@",arr1);
}
#pragma mark 数组排序1
void arraySort1(){
NSArray *array = [NSArray arrayWithObjects:@"13",@"25",@"15",@"2",@"1",@"25",@"21",@"62",@"41",@"82",@"19",@"122", nil];
NSLog(@"排序前的数组是:%@",array);
NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"排序后的数组是:%@",array2);
}
#pragma mark 数组排序2
void arraySort2(){
Student *stu1 = [Student studentWithFirstname:@"Xiaofeng1" Lastname:@"Zhou2"];
Student *stu2 = [Student studentWithFirstname:@"Xiaofeng2" Lastname:@"Zhou1"];
Student *stu3 = [Student studentWithFirstname:@"Xiaofeng3" Lastname:@"Zhou9"];
Student *stu4 = [Student studentWithFirstname:@"Xiaofeng4" Lastname:@"Zhou5"];
Student *stu5 = [Student studentWithFirstname:@"Xiaofeng5" Lastname:@"Zhou4"];
NSArray *array1 = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4,stu5, nil];
NSLog(@"排序前的数组是:%@",array1);
NSArray *array2 = [array1 sortedArrayUsingSelector:@selector(compareStudent:)];
NSLog(@"排序后的数组是:%@",array2);
}
#pragma mark 数组排序3
void arraySort3(){
Student *stu1 = [Student studentWithFirstname:@"Xiaofeng1" Lastname:@"Zhou2"];
Student *stu2 = [Student studentWithFirstname:@"Xiaofeng2" Lastname:@"Zhou1"];
Student *stu3 = [Student studentWithFirstname:@"Xiaofeng3" Lastname:@"Zhou9"];
Student *stu4 = [Student studentWithFirstname:@"Xiaofeng4" Lastname:@"Zhou5"];
Student *stu5 = [Student studentWithFirstname:@"Xiaofeng5" Lastname:@"Zhou4"];
NSArray *array1 = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4,stu5, nil];
NSLog(@"排序前的数组是:%@",array1);
NSArray *array2 = [array1 sortedArrayUsingComparator:
^NSComparisonResult(Student *stu1, Student *stu2) {
if (stu1.lastName == stu2.lastName) {
return [stu1.firstName compare:stu2.firstName];
}else{
return [stu1.lastName compare:stu2.lastName];
}
}];
NSLog(@"排序后的数组是:%@",array2);
}
#pragma mark 数组排序4
void arraySort4(){
Student *stu1 = [Student studentWithFirstname:@"Xiaofeng12" Lastname:@"Zhou2" BookName:@"book1"];
Student *stu2 = [Student studentWithFirstname:@"Xiaofeng11" Lastname:@"Zhou2" BookName:@"book2"];
Student *stu3 = [Student studentWithFirstname:@"Xiaofeng14" Lastname:@"Zhou1" BookName:@"book3"];
Student *stu4 = [Student studentWithFirstname:@"Xiaofeng1123" Lastname:@"Zhou4" BookName:@"book4"];
NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
//1.先按书名排序
NSSortDescriptor *bookDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
//2.再按照姓排序
NSSortDescriptor *lastNameDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
//3.再按照名字排序
NSSortDescriptor *firstNameDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
//按顺序添加排序描述器
NSArray *desc = [NSArray arrayWithObjects:bookDescriptor,lastNameDescriptor,firstNameDescriptor,nil];
NSArray *array2 = [array sortedArrayUsingDescriptors:desc];
NSLog(@"%@",array2);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
arraySort4();
}
return 0;
}
//
// Student.h
// foundation-NSArray2
//
// Created by apple on 15/6/29.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
@class Book;
@interface Student : NSObject
@property (nonatomic,retain) NSString *firstName;
@property (nonatomic,retain) NSString *lastName;
@property (nonatomic,retain) Book *book;
+(id)studentWithFirstname:(NSString *)firstname Lastname:(NSString *)lastname;
+(id)studentWithFirstname:(NSString *)firstname Lastname:(NSString *)lastname BookName:(NSString *)bookName;
//返回值为NSComparisonResult
-(NSComparisonResult)compareStudent:(Student *)stu;
@end
//
// Student.m
// foundation-NSArray2
//
// Created by apple on 15/6/29.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "Student.h"
#import "Book.h"
@implementation Student
+(id)studentWithFirstname:(NSString *)firstname Lastname:(NSString *)lastname{
Student *stu = [[Student alloc]init];
stu.firstName = firstname;
stu.lastName = lastname;
return stu;
}
+(id)studentWithFirstname:(NSString *)firstname Lastname:(NSString *)lastname BookName:(NSString *)bookName{
Student *stu = [Student studentWithFirstname:firstname Lastname:lastname];
stu.book = [Book bookWithName:bookName];
return stu;
}
-(NSComparisonResult)compareStudent:(Student *)stu{
if (self.lastName == stu.lastName) {
return [self.firstName compare:stu.firstName];
}else{
return [self.lastName compare:stu.lastName];
}
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@ %@-%@", self.lastName,self.firstName,_book.name];
}
@end
//
// Book.h
// foundation-NSArray2
//
// Created by apple on 15/6/29.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject
@property (nonatomic,retain) NSString *name;
+(id)bookWithName:(NSString*)name;
@end
//
// Book.m
// foundation-NSArray2
//
// Created by apple on 15/6/29.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "Book.h"
@implementation Book
+(id)bookWithName:(NSString *)name{
Book *book = [[Book alloc]init];
book.name = name;
return book;
}
@end