//
// main.m
// NSArray(NSMutableArray)-Foundation
//
// Created by amesty on 15-1-18.
// Copyright (c) 2015年 itcast. All rights reserved.
//
/**
1.NSArray 数组元素不可变。只能装oc对象,数组为空表示数组元素的结束。
2.NSSet
*/
#import <Foundation/Foundation.h>
#import "Student.h"
#pragma mark 数组的创建
void arrayCreate()
{//创建一个空的数组
NSArray *array=[NSArray array];
//创建一个有一个元素的数组
array = [NSArray arrayWithObject:@"123"];
//创建有多个元素的数组
array = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",nil];
int counts=[array count];
NSLog(@"%d",counts);
}
#pragma mark 数组的简单使用
void arrayUse()
{
NSObject *obj=[[NSObject alloc]init];
NSArray *array=[NSArray arrayWithObjects:@"a",@"b",@"c",@"d",obj,nil];
//判断是否包含了某个元素
if ([array containsObject:@"a"]) {
NSLog(@"包含了字串a");
}
//查询最后一个字符串对象
NSString *last=[array lastObject];
NSLog(@"last=%@",last);
//查询下标为1的对象,下标从零开始
NSString *str=[array objectAtIndex:1];
NSLog(@"%@",str);
int index=[array indexOfObject:@"d"];
NSLog(@"%zi",index);
}
#pragma mark 数组的内存管理
void arrayMemory()
{
Student * stu1=[Student student];
Student * stu2=[Student student];
Student * stu3=[Student student];
NSLog(@"stu1的count为: %zi",[stu1 retainCount]);
//当把一个对戏那个塞进数组中时,这个对象的计数器会加1,也就是说数组会对它做一次retain操作。
NSArray *array=[[NSArray alloc]initWithObjects:stu1,stu2,stu3, nil];
NSLog(@"count=%zi\n\n",array.count);
NSLog(@"stu1的count为: %zi",[stu1 retainCount]);
//数组对象销毁的时候,会对内部的所有元素都做一次release操作。
[array release];
[stu1 release];
[stu2 release];
[stu3 release];
}
#pragma mark 给数组里面的元素发送消息
void arrayMessage()
{
Student * stu1=[Student student];
Student * stu2=[Student student];
Student * stu3=[Student student];
NSArray *array=[[NSArray alloc]initWithObjects:stu1,stu2,stu3, nil];
// [array makeObjectsPerformSelector:@selector(test)];
[array makeObjectsPerformSelector:@selector(test) withObject:@"123"];
}
#pragma mark 遍历数组1
void arrayFor1()
{
NSArray *array=[NSArray arrayWithObjects:@"a",@"b",@"c",@"d",nil];
int count=array.count;
for(int i=0;i<count;i++)
{
NSLog(@"%@",[array objectAtIndex:(i)]);
}
}
#pragma mark 遍历数组2
void arrayFor2()
{ Student *stu=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu,@"a",@"b",@"c",@"d",nil];
int i=0;
//快速遍历
for (id obj in array) {
NSLog(@"%i----%@",i,obj);
i++;
}
}
#pragma mark 遍历数组3
void arrayFor3()
{ Student *stu=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu,@"a",@"b",@"c",@"d",nil];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%zi----%@",idx,obj);
//利用指针修改BOOL
if (idx==3) {
*stop=YES;
}
}];
}
#pragma mark 利用迭代器遍历
void arrayFor4()
{
Student *stu=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu,@"a",@"b",@"c",@"d",nil];
//获取数组的迭代器
//NSEnumerator *enumerator=[array objectEnumerator];
//获取逆序的迭代器
NSEnumerator *enumerator=[array reverseObjectEnumerator];
//获取下一个需要遍历的元素
id obj=nil;
//取出没有被遍历的对象
NSArray *array2=[enumerator allObjects];
NSLog(@"array2:%@",array2);
while (obj=[enumerator nextObject]) {
NSLog(@"obj=%@",obj);
}
[array release];
[stu release];
}
#pragma mark 派生出新的数组
void arrayNew()
{NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3", nil];
NSArray *array2=[array arrayByAddingObject:@"4"];
NSLog(@"array2:%@",array2);
NSLog(@"array:%@",array);
NSArray *array3=[array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"4",@"5", nil]];
NSLog(@"array3:%@",array3);
//根据range截取字串
NSArray *array4=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
NSRange range=NSMakeRange(1, 2);
NSArray *array5=[array4 subarrayWithRange:range];
NSLog(@"array5%@",array5);
}
#pragma mark 数组的其他用法
void arrayOther()
{
NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
//利用分隔符凭借所有的数组元素
NSString *str=[array componentsJoinedByString:@"-"];
NSLog(@"%@",str);
//将数组写入一个xml的文件
NSString *path=@"/Users/amesty/Desktop/c语言/array.txt";
[array writeToFile:path atomically:YES];
//从文件中读取数组的内容(文件有严格的要求)
path=@"/Users/amesty/Desktop/c语言/array.txt";
NSArray *array2=[NSArray arrayWithContentsOfFile:path];
NSLog(@"array2:%@",array2);
}
#pragma mark 数组排序1
void arraySort1()
{ NSArray *array=[NSArray arrayWithObjects:@"1",@"3",@"2",@"4", nil];
//返回一个排序好的数组,原来数组的元素顺序不会改变
//指定元素的比较大小:compare;
NSArray *array2=[array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",array2);
}
#pragma mark 数组排序2
void arraySort2()
{ Student *stu1=[Student studentWithFirstname:@"mingjie" lastname:@"li"];
Student *stu2=[Student studentWithFirstname:@"xiaoyan" lastname:@"li"];
Student *stu3=[Student studentWithFirstname:@"lianjie" lastname:@"li"];
Student *stu4=[Student studentWithFirstname:@"jian" lastname:@"xiao"];
NSArray *array =[NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
NSArray *array2=[array sortedArrayUsingSelector:@selector(compareStudent:)];
NSLog(@"%@",array2);
}
#pragma mark 数组的排序3 利用block进行排序
void arraySort3()
{
Student *stu1=[Student studentWithFirstname:@"mingjie" lastname:@"li"];
Student *stu2=[Student studentWithFirstname:@"xiaoyan" lastname:@"li"];
Student *stu3=[Student studentWithFirstname:@"lianjie" lastname:@"li"];
Student *stu4=[Student studentWithFirstname:@"jian" lastname:@"xiao"];
NSArray *array =[NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
// 利用block进行排序
NSArray *array2=[array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
// 先按照姓排序
NSComparisonResult result = [obj1.lastname compare:obj2.lastname];
// 如果有相同的姓,就比较名字
if (result == NSOrderedSame) {
result = [obj1.firstname compare:obj2.firstname];
}
return result;
}];
NSLog(@"array2:%@", array2);
}
#pragma mark 动态添加排序的描述器,之后再排序
void arraySort4()
{ Student *stu1=[Student studentWithFirstname:@"mingjie" lastname:@"li" bookname:@"aabbcc"];
Student *stu2=[Student studentWithFirstname:@"xiaoyan" lastname:@"li" bookname:@"abbbcc"];
Student *stu3=[Student studentWithFirstname:@"lianjie" lastname:@"li" bookname:@"bbccd"];
Student *stu4=[Student studentWithFirstname:@"jian" lastname:@"xiao" bookname:@"bcccd"];
NSArray *array =[NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
// 1.先按照书名进行排序
// 这里的key写的是@property的名称
NSSortDescriptor *bookNameDesc=[NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
// 2.再按照姓进行排序
NSSortDescriptor *firstNameDesc=[NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
// 3.再按照名进行排序
NSSortDescriptor *lastNameDesc=[NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
// 按顺序添加排序描述器
NSArray *desc=[NSArray arrayWithObjects:bookNameDesc,firstNameDesc,lastNameDesc,nil];
NSArray *array2=[array sortedArrayUsingDescriptors:desc];
NSLog(@"%@",array2);
}
//------------------------------可变数组----------------
#pragma mark 可变数组的建立
void mutableArrayCreate()
{
NSMutableArray *array= [NSMutableArray arrayWithObject:@"1"];
[array addObject:@"2"];
[array addObject:@"3"];
[array removeObject:@"3"];
[array removeAllObjects];
NSLog(@"%@",array);
}
#pragma mark 可变数组的内存管理
void mutableArrayMemory()
{ NSMutableArray *array=[[NSMutableArray alloc]init];
Student * stu1=[[Student alloc]init]; // stu1:1
stu1.age=10;
Student *stu2=[[Student alloc]init]; // stu2:1
stu2.age=20;
// 对被添加的元素做一次retain操作,计数器+1
[array addObject:stu1];
[array addObject:stu2];
NSLog(@"add->stu1%zi",[stu1 retainCount]);
// 对被删除的元素做一次release操作,计数器-1
[array removeObject:stu1];
NSLog(@"remove->stu1%zi",[stu1 retainCount]);
[stu1 release];
[stu2 release];
// 当数组被释放的时候,会对所有的元素都做一次release操作
[array release];
}
#pragma mark 可变数组的替换操作
void mutableArrayReplace()
{
NSMutableArray *array =[NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];
[array replaceObjectAtIndex:2 withObject:@"4"];
NSLog(@"%@",array);
}
#pragma mark 可变数组的排序操作
void arraySort()
{
NSMutableArray *array=[NSMutableArray arrayWithObjects:@"1",@"4",@"2",@"3",nil];
[array sortUsingSelector:@selector(compare:)];
NSLog(@"%@",array);
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
//arrayUse();
// arrayMemory();
//arrayMessage();
// insert code here...
//arrayFor1();
//arrayFor2();
//arrayFor3();
//arrayFor4();
//arrayNew();
//arrayOther();
//mutableArrayCreate();
// mutableArrayMemory();
//mutableArrayReplace();
// arraySort();
//arraySort2();
//arraySort3();
arraySort4();
NSLog(@"Hello, World!");
}
return 0;
}
//
// Student.h
// NSArray(NSMutableArray)-Foundation
//
// Created by amesty on 15-1-19.
// 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,assign)int age;
@property(nonatomic,retain)Book *book;
-(void)dealloc;
-(void)test;
-(void)test2:(NSString *)str;
+(Student *) student;
//返回值类型为NSComparisonResult
-(NSComparisonResult)compareStudent:(Student *)stu;
+(id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname;
+(id)studentWithAge:(int)age;
+(id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname bookname:(NSString *)bookname;
@end
//
// Student.m
// NSArray(NSMutableArray)-Foundation
//
// Created by amesty on 15-1-19.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "Student.h"
#import "Book.h"
@implementation Student
-(void)dealloc
{ [_firstname release];
[_lastname release];
[_book release];
NSLog(@"age为%d的%@student对象被销毁了",self.age,self);
[super dealloc];
}
+(Student *) student
{ Student *stu=[[Student alloc]init];
return stu;
}
-(void)test
{ NSLog(@"%@调用了test方法",self);
}
-(void)test2:(NSString *)str
{ NSLog(@"%@->test2->%@",self,str);
}
-(NSComparisonResult)compareStudent:(Student *)stu
{ //先比较姓按照姓排序
NSComparisonResult result=[self.lastname compare:stu.lastname];
if (result==NSOrderedSame) {
result= [self.firstname compare:stu.lastname];
}
return result;
}
+(id)studentWithFirstname:(NSString *)fristname lastname:(NSString *)lastname
{ Student *stu=[[Student alloc]init];
stu.firstname=fristname;
stu.lastname=lastname;
return stu;
}
+(id)studentWithAge:(int)age1
{ Student *stu=[[Student alloc]init];
stu.age=age1;
return stu;
}
+(id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname bookname:(NSString *)bookname
{ Student *stu=[[Student alloc]init];
stu.firstname=firstname;
stu.lastname=lastname;
stu.book=[Book initBookWithName:bookname];
return stu;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"[%@-%@-%@]",self.lastname,self.firstname,self.book.name];
}
@end
//
// Book.h
// NSArray(NSMutableArray)-Foundation
//
// Created by amesty on 15-1-20.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject
@property(nonatomic,retain) NSString *name;
+(id)initBookWithName:(NSString *)name;
@end
//
// Book.m
// NSArray(NSMutableArray)-Foundation
//
// Created by amesty on 15-1-20.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "Book.h"
@implementation Book
+(id)initBookWithName:(NSString *)name
{ Book * book=[[Book alloc]init];
book.name=name;
return book;
}
-(void)dealloc
{ [_name release];
[super dealloc];
}
@end