1.建立陣列
// 建立乙個空的陣列
nsarray *array = [nsarray array];
// 建立有1個元素的陣列
array = [nsarray arraywithobject:@"123"];
// 建立有多個元素的陣列
array = [nsarray arraywithobjects:@"a", @"b", @"c", nil nil];
nsarray *array3 = [array arraybyaddingobjectsfromarray:[nsarray arraywithobjects:@"4", @"5", nil nil]];
nsarray *array4 = [nsarray arraywithobjects:@"1", @"2", @"3", @"4", nil nil];
nsrange range = nsmakerange(1, 2);
nsarray *array5 = [array4 subarraywithrange:range];
2.陣列的一些基本方法
int count = [array count];//個數
// 判斷是否包含了某個元素
if ([array containsobject:@"a"])
nsstring *last = [array lastobject];最後乙個元素
nsstring *str = [array objectatindex:1];根據索引獲取陣列中的元素
int index = [array indexofobject:@"c"];獲取指定元素的索引
// 讓陣列裡面的所有物件都呼叫test方法,123為引數
nsarray *array = [nsarray arraywithobjects:stu1, stu2, stu3, nil nil];
[array makeobjectsperformselector:@selector(test2:) withobject:@"123"];
nsarray *array = [nsarray arraywithobjects:@"1", @"2", @"3", @"4", nil nil];
// 1-2-3-4
// 利用分隔符-拼接所有的陣列元素
nsstring *str = [array componentsjoinedbystring:@"-"];
// 將乙個陣列寫入檔案(生成的是乙個xml檔案)
[array writetofile:path atomically:yes];
// 從檔案中讀取陣列內容(檔案有嚴格的格式要求)
nsarray *array2 = [nsarray arraywithcontentsoffile:path];
3.遍歷陣列
#pragma mark 遍歷陣列1
void arrayfor1()
} #pragma mark 遍歷陣列2 快速遍歷
void arrayfor2()
} #pragma mark 遍歷陣列3
void arrayfor3()
}];
} #pragma mark 遍歷陣列4
void arrayfor4()
}
4.陣列排序
#pragma mark 陣列排序1
void arraysort1()
#pragma mark 陣列排序2
void arraysort2()
- (nscomparisonresult)comparestudent:(student *)stu
return result;
} #pragma mark 陣列排序3
void arraysort3()
return result;
}];
nslog(@"array2:%@", array2);
} #pragma mark 陣列排序4-高階排序
void arraysort4() {
student *stu1 = [student studentwithfirstname:@"mingjie" lastname:@"li" bookname:@"book1"];
student *stu2 = [student studentwithfirstname:@"longhu" lastname:@"huang" bookname:@"book2"];
student *stu3 = [student studentwithfirstname:@"lianjie" lastname:@"li" bookname:@"book2"];
student *stu4 = [student studentwithfirstname:@"jian" lastname:@"xiao" bookname:@"book1"];
nsarray *array = [nsarray arraywithobjects:stu1,stu2,stu3, stu4, nil nil];
// 1.先按照書名進行排序
// 這裡的key寫的是@property的名稱
nssortdescriptor *booknamedesc = [nssortdescriptor sortdescriptorwithkey:@"book.name" ascending:yes];
// 2.再按照姓進行排序
nssortdescriptor *lastnamedesc = [nssortdescriptor sortdescriptorwithkey:@"lastname" ascending:yes];
// 3.再按照名進行排序
nssortdescriptor *firstnamedesc = [nssortdescriptor sortdescriptorwithkey:@"firstname" ascending:yes];
// 按順序新增排序描述器
nsarray *descs = [nsarray arraywithobjects:booknamedesc, lastnamedesc, firstnamedesc, nil nil];
nsarray *array2 = [array sortedarrayusingdescriptors:descs];
nslog(@"array2:%@", array2);
ios陣列基本用法和排序
1 建立陣列 objc view plain copy 建立乙個空的陣列 nsarray array nsarray array 建立有1個元素的陣列 array nsarrayarraywithobject 123 建立有多個元素的陣列 array nsarrayarraywithobjects ...
ios陣列基本用法和排序
1.建立陣列 objc view plain copy 建立乙個空的陣列 nsarray array nsarrayarray 建立有1個元素的陣列 array nsarrayarraywithobject 123 建立有多個元素的陣列 array nsarrayarraywithobjects a...
iOS陣列用法以及排序
1.建立陣列 objc view plain copy 建立乙個空的陣列 nsarray array nsarray array 建立有1個元素的陣列 array nsarrayarraywithobject 123 建立有多個元素的陣列 array nsarrayarraywithobjects ...