NSMutableArray (可變陣列增、刪、改、查)
//1.1 初始化 NSMutableArray *muArray = [NSMutableArray arrayWithObjects:@"123",@"345",@"2354", nil]; NSMutableArray *mutAry = [NSMutableArray array]; //從其它陣列中新增元素 [mutAry addObjectsFromArray:muArray]; //1.2 初始化劃好幾個元素容量的記憶體空間,當不超過元素個數時不需要重新請求記憶體,實際用得較少 NSMutableArray *mutableAry = [[NSMutableArray alloc]initWithCapacity:4]; [mutableAry addObject:@"1"]; [mutableAry addObject:@"2"]; [mutableAry addObject:@"3"]; [mutableAry addObject:@"4"]; [mutableAry addObject:@"1"]; [mutableAry addObject:@"5"]; [mutableAry addObject:@"6"]; //=======增 [muArray insertObject:@"456" atIndex:2]; [muArray addObject:@"d"]; NSLog(@"%@",muArray); //=======刪 [muArray removeObject:@"123"]; //刪除所有 // [muArray removeAllObjects]; //刪除陣列最後一個元素 [muArray removeLastObject]; //刪除指定範圍的@"1"值 NSRange range = {2,5}; // [mutableAry removeObject:@"1" inRange:range]; //1234156 --> 123456 // [muarray removeObjectAtIndex:4]; NSLog(@"%@",muArray); //刪除相同元素 及 刪除指定範圍內相同元素 // [mutableAry removeObject:@"1"]; // [mutableAry removeObjectIdenticalTo:@"1"]; // [mutableAry removeObjectIdenticalTo:<#(nonnull id)#> inRange:<#(NSRange)#>] //根據另一陣列元素找出所對應的刪除 // [mutableAry removeObjectsInArray:@[@"1",@"4",@"6"]]; //========改 // [muArray replaceObjectAtIndex:3 withObject:@"23"]; NSLog(@"%@",muArray); //========查 NSString *str = [muArray objectAtIndex:2]; NSLog(@"%@",muArray[1]); NSLog(@"%@",str); //陣列中加入Number [muArray addObject:[NSNumber numberWithInt:3]]; int i = [[muArray lastObject] intValue]; [muArray addObject:[NSNumber numberWithFloat:2]]; float y = [[muArray lastObject] floatValue]; //元素調換位置 123456 3和5調換==》125436 [mutableAry exchangeObjectAtIndex:2 withObjectAtIndex:4];
陣列寫入本地檔案儲存
//- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; //陣列寫入檔案儲存 //從檔案轉成陣列 生成本地一個data.txt檔案拖到工程 //沙盒根目錄路徑 NSString *doucumonPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *fileName = @"data.plist"; //data.txt //根目錄下新建立資料夾路徑 NSString *folderPath = [doucumonPath stringByAppendingPathComponent:@"myFolder"]; //完整檔案路徑 NSString *path = [folderPath stringByAppendingPathComponent:fileName]; //建立檔案管理器 NSFileManager *file=[NSFileManager defaultManager]; if (![file fileExistsAtPath:folderPath]) { [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil]; } BOOL b = [mutableAry writeToFile:path atomically:YES]; //獲取檔案陣列 //專案路徑讀取 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"txt"]; //沙盒路徑讀取 NSMutableArray *fileAry = [NSMutableArray arrayWithContentsOfFile:path]; //==================================================
</pre></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 13.5px; line-height: normal; font-family: Menlo; color: rgb(0, 175, 202);">arrayWithContentsOfURL</p><pre name="code" class="objc">//從URL轉為陣列 //URL可以是 http://xxx.plist http://.xxx.xml http://.xxx.txt 等下載地址 NSURL *url = [NSURL fileURLWithPath:path]; //本地URL:file:///var/mobile/Containers/Data/Application/6856FD5B-C8F1-4282-8DF3-D49B97355680/Library/Documentation/myFolder/data.xml BOOL bb = [mutableAry writeToURL:url atomically:YES]; NSMutableArray *urlAry = [NSMutableArray arrayWithContentsOfURL:url];
//把非物件型別存入陣列
想把一些view frame儲存到NSArray中,但是CGRect其實是結構體,必須封裝成物件才可以存入Array,Dictionary中,蘋果自帶NSValue封裝這些結構體,具體如下。
因為CGRect,CGPoint等不是NSObject的子類,因此不能夠直接新增到NSMutableArray中,所以要先將其轉換成NSObject的子類。NSValue類可以解決這個問題,見NSValue類的宣告:
如下,可以將CGRect,CGPoint等的數值轉換成NSValue類的數值。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lockButton.png"]];
imageView.frame = CGRectMake(10 * (l + 1) + imageView.frame.size.width * l, 120 + 50 * r, imageView.frame.size.width, imageView.frame.size.height);
[self.lockImageRectArray addObject:[NSValue valueWithCGRect:imageView.frame]];
[self addSubview:imageView];
通過下面的程式碼,又可以將NSValue轉換成CGRect,CGPoint等型別的數值。
CGRect imageRect = [[self.lockImageRectArray objectAtIndex:l] CGRectValue];
通過NSValue就可以實現CGxxx型別的數值儲存在NSMutable,NSDictionary型別的陣列中。