[NSArray]一 定義1 不可變數組2 oc中數組的元素可以是任何對象 3 數字中裝有元素的地址 二 初始化NSArray *變量 = [[NSArry alloc] initWithObjects:對象...,nil];三 使用場景1 遍歷數組 A 枚舉 NSEnumerator *enums = [array1 objectEnumerator]; while (obj=[enums nextObject]) B 下標 for (i=0;i<count;i++) {C 快速枚舉法for(id obj in array1)2 獲取長度int count = [數組對象 count];[NSMutableArray]一 定義1 可變數組2 可以插入任意對象二 初始化NSMutableArray *變量名 = [[NSMutableArray alloc] init];三 使用場景1 增加對象[mutableArray addObject:@"JACK"];2 下標替換對象[mutableArray exchangeObjectAtIndex:0 withObjectAtIndex:2];3 移除對象 [mutableArray removeObject:person]; [mutableArray removeObjectAtIndex:0];4 順序枚舉遍歷 NSEnumerator *_enums = [mutableArray objectEnumerator];5 倒序枚舉遍歷 NSEnumerator *_reverseEnums = [mutableArray reverSEObjectEnumerator];6 字符串和數組的轉換 NSArray *_strArray = [str componentsSeparatedByString:@" "]; NSString *str = [_strMutableArray componentsJoinedByString:@" "];
//// main.m// OC-數組//// Created by wangtouwang on 15/3/20.// Copyright (c) 2015年 wangtouwang. All rights reserved.//#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * argv[]) { @autoreleasepool { // 不可變數組 NSArry NSLog(@"NSArry"); Person *person = [Person new]; person._name=@"JACK"; //初始化 NSArray *array1 = [[NSArray alloc] initWithObjects:@"jack",@"lucy",person, nil]; NSLog(@"%@",array1); //枚舉 NSLog(@"枚舉器法打印數組結果:"); NSEnumerator *enums = [array1 objectEnumerator]; id obj; while (obj=[enums nextObject]) { NSLog(@"%@",obj); } //快速枚舉 NSLog(@"快速枚舉法打印數組結果:"); for(id obj in array1){ NSLog(@"%@",obj); } //下標便利 NSInteger i = 2; id obj2 = [array1 objectAtIndex:i]; NSLog(@"%@",obj2); //獲取長度 NSInteger count=[array1 count]; NSLog(@"數組長度 %lu",count); i=0; for (i=0;i<count;i++) { NSLog(@"%@",[array1 objectAtIndex:i]); } //可變數組 NSMutableArray NSMutableArray *mutableArray = [[NSMutableArray alloc] init]; //增加 [mutableArray addObject:@"JACK"]; [mutableArray addObject:@"MOBILE"]; [mutableArray addObject:@"TEL"]; [mutableArray addObject:@"BOOK"]; [mutableArray addObject:@"STUDENT"]; [mutableArray addObject:person]; NSLog(@"%@",mutableArray); //替換對象 NSLog(@"替換對象"); [mutableArray exchangeObjectAtIndex:0 withObjectAtIndex:2]; NSLog(@"%@",mutableArray); //移除對象 參數 id類型 NSLog(@"移除對象 參數 id類型"); [mutableArray removeObject:person]; NSLog(@"%@",mutableArray); //移除對象 參數 NSInteger NSLog(@"移除對象 參數 NSInteger"); [mutableArray removeObjectAtIndex:0]; NSLog(@"%@",mutableArray); //正序枚舉遍歷 NSLog(@"正序枚舉遍歷"); NSEnumerator *_enums = [mutableArray objectEnumerator]; id obj3; while (obj3=[_enums nextObject]) { NSLog(@"%@",obj3); } //倒序枚舉遍歷 并且可以遍歷時刪除 NSLog(@"倒序枚舉遍歷 并且可以遍歷時刪除"); NSEnumerator *_reverseEnums = [mutableArray reverseObjectEnumerator]; while (obj3=[_reverseEnums nextObject]) { NSLog(@"%@",obj3); [mutableArray removeObject:obj3]; } NSLog(@"倒序枚舉遍歷結束 查看數組長度 %lu",[mutableArray count]); //字符串和數組的轉換 NSString *str = @"hello world hao are you"; NSArray *_strArray = [str componentsSeparatedByString:@" "]; NSLog(@"lenght = %lu",[_strArray count]); NSMutableArray *_strMutableArray = [[NSMutableArray alloc] init]; NSEnumerator *_strEnums = [_strArray reverseObjectEnumerator]; NSString *objstring ; while (objstring=[_strEnums nextObject]) { [_strMutableArray addObject:objstring]; } NSLog(@"%@",_strMutableArray); str = [_strMutableArray componentsJoinedByString:@" "]; NSLog(@"%@",str); } return 0;}
新聞熱點
疑難解答