Foundation框架---數(shù)組

OC 數(shù)組特點(diǎn): 可以存儲(chǔ)不同類型的對(duì)象,oc的數(shù)組 只能存儲(chǔ)對(duì)象

數(shù)組可以遍歷,占用的內(nèi)存空間是連續(xù)的. oc中的數(shù)組不是把整個(gè)對(duì)象存在數(shù)組中 ,而是把指針存到內(nèi)存中.

-----------------------------------------------------------------------------------------------------------------

? ? ? ? NSArray *nsArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];

? ? ? ? //數(shù)組長度

? ? ? ? NSInteger count = [nsArray count];

? ? ? ? NSLog(@"count = %ld",(long)count);

? ? ? ? //判斷數(shù)組中是否包含對(duì)應(yīng)對(duì)象

? ? ? ? BOOLisHave = [nsArray containsObject:@"3"];

? ? ? ? if(isHave)

? ? ? ? ? ? NSLog(@"包含");

? ? ? ? else

? ? ? ? ? ? NSLog(@"不包含");

? ? ? ? //取出數(shù)組中首個(gè)元素

? ? ? ? NSString*firstStr = [nsArray?firstObject];

? ? ? ? NSLog(@"firstStr =%@",firstStr);

? ? ? ? //取出數(shù)組中最后一個(gè)元素

? ? ? ? NSString*lastStr = [nsArray lastObject];

? ? ? ? NSLog(@"lastStr = %@",lastStr);

? ? ? ? //取出數(shù)組中下標(biāo)為3的元素

?? ? ? ? NSString*threeStr = [nsArray objectAtIndex:3];

? ? ? ? NSLog(@"threeStr = %@",threeStr);

? ? ? ? //打印對(duì)應(yīng)元素的下標(biāo)

? ? ? ? NSInteger index = [nsArray indexOfObject:@"2"];

? ? ? ? NSLog(@"index =%ld",(long)index);

-----------------------------------------------------------------------------------------------------------------

//數(shù)組遍歷(1.基本的for循環(huán),通過下標(biāo)逐一取出查看.for in快速枚舉.3.枚舉器(迭代器))

? ? ? ? Person *p = [[Person alloc] init];

? ? ? ? p.personName=@"公孫離";

? ? ? ? NSArray*personArray = [[NSArray alloc]initWithObjects:@"a",@"b",p,@"c",nil];

? ? ? ? // c語言版本:c89,c95,c99

? ? ? ? // c 89 int i 需要生命到外面

? ? ? ? for(inti =0;i

? ? ? ? ? ? NSString*arrayStr = [nsArray objectAtIndex:i];

? ? ? ? ? ? NSLog(@"arrayStr =%@",arrayStr);

? ? ? ? }


? ? ? ? for(inti =0;i

?? ? ? ? ? id personStr = [personArray objectAtIndex:i];

? ? ? ? ? ? if([personStr isKindOfClass:[Person class]]){

? ? ? ? ? ? ? ? Person*person = (Person*)personStr;

? ? ? ? ? ? ? ? NSLog(@"personName2 =%@",person.personName);

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? NSString*personArray2 = (NSString*)personStr;

? ? ? ? ? ? ? ? NSLog(@"personArray2=%@",personArray2);

? ? ? ? ? ? }

? ? ? ? }

-----------------------------------------------------------------------------------------------------------------

? ? ? ? //快速枚舉

? ? ? ? //迭代相同類型元素的數(shù)組

? ? ? ? for(NSString*personStr2innsArray) {

? ? ? ? ? ? NSLog(@"personStr2 =%@",personStr2);

? ? ? ? }

? ? ? ? //迭代不同類型元素的數(shù)組

? ? ? ? for(id personStr3 in personArray) {

? ? ? ? ? ? if([personStr3 isKindOfClass:[Person class]]) {

? ? ? ? ? ? ? ? Person*person = (Person*)personStr3;

? ? ? ? ? ? ? ? NSLog(@"personName3 =%@",person.personName);

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? NSString*personArray3 = (NSString*)personStr3;

? ? ? ? ? ? ? ? NSLog(@"personArray3 =%@",personArray3);

? ? ? ? ? ? }

? ? ? ? }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 最先接觸編程的知識(shí)是在大學(xué)里面,大學(xué)里面學(xué)了一些基礎(chǔ)的知識(shí),c語言,java語言,單片機(jī)的匯編語言等;大學(xué)畢...
    oceanfive閱讀 3,395評(píng)論 0 7
  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 30,238評(píng)論 8 265
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,654評(píng)論 1 32
  • NSRange : 表示范圍作用的結(jié)構(gòu)體,3種方式創(chuàng)建 // 方式一 NSRange range; locatio...
    路墨閱讀 1,159評(píng)論 1 8
  • 卡農(nóng)是那種可以讓人心情舒暢地聽完一百遍,卻在第一百零一遍的時(shí)候突然聯(lián)想起什么,停滯下當(dāng)前的動(dòng)作,似乎是恍然間觸動(dòng)...
    玫瑰信札閱讀 457評(píng)論 0 1

友情鏈接更多精彩內(nèi)容