首页 > 代码库 > 【Foundation Frame】NSArray
【Foundation Frame】NSArray
NSArray
代表元素有序、可重复的一个集合,集合中每个元素都有对应的顺序索引。
创建NSArray的常见方法
1、array:创建一个不包含任何元素的空NSARRAY
2、arrayWithObject/initWithObject:创建只包含指定元素的NSArray
3、arrayWithObjects/initWithObjects:创建只包含一个或多个元素的NSArray
4、arrayWithContentsOfFile:读取文件内容来创建NSArray
(以下程序列举了常用的NSArray方法)
//
// main.m
// Array
//
// Created by mac on 14-12-3.
// Copyright (c) 2014年 mac. All rights reserved.
//
#import<Foundation/Foundation.h>
#import "People.h"
//静态创建数组的方法
void staticCreatArray()
{
NSArray * array1 = [NSArrayarrayWithObject:@"NSArray"];
NSLog(@"Way 1: %@",array1);
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3",nil];
NSLog(@"Way 2: %@",array2);
NSArray * array3 = [NSArrayarrayWithArray:array2];
NSLog(@"Way 3: %@",array3);
[array1 release];
[array2 release];
[array3 release];
}
//动态创建数组的方法
void dynamicCreatArray()
{
NSArray * array1 = [[NSArrayalloc]initWithObjects:@"1,2,3,4,5",@"HELLO",nil];
NSLog(@"Way 1: %@",array1);
NSArray * array2 = [[NSArrayalloc]initWithArray:array1];
NSLog(@"Way 2: %@",array2);
}
//查询集合元素在NSArray中的序列
void searchArray()
{
NSArray * array1 = [NSArrayarrayWithObject:@"NSArray"];
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3",nil];
NSArray * array3 = [[NSArrayalloc]initWithObjects:@"1,2,3,4,5",@"HELLO",@"HELLO",nil];
NSLog(@"%lu",[array1count]);
NSLog(@"%hhd",[array3containsObject:@"HELLO"]);
NSLog(@"%@",[array2lastObject]);
NSLog(@"%@",[array2objectAtIndex:1]);
NSLog(@"%lu",[array2indexOfObject:@"NSArray1"]);
NSRange range =NSMakeRange(0,2);
NSLog(@"%lu",[array2indexOfObject:@"NSArray2"inRange:range]);
[array3 release];
}
//对数组进行比较
void compareArray()
{
NSArray * array1 = [NSArrayarrayWithObject:@"NSArray"];
NSArray * array11 = [array1copy];
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3",nil];
NSArray * array3 = [[NSArrayalloc]initWithObjects:@"1,2,3,4,5",@"HELLO",@"HELLO",@"NSArray1",nil];
NSLog(@"%hhd",[array1isEqualToArray:array11]);
NSLog(@"%@",[array2firstObjectCommonWithArray:array3]);
[array3 release];
}
//遍历数组
void ergodicArray()
{
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3",nil];
//C语言方式
for (int i = 0; i<[array2count]; i++)
{
NSLog(@"%@",[array2objectAtIndex:i]);
}
//快速遍历
for (id obj in array2)
{
NSLog(@" %@",obj);
}
//Block方法遍历
[array2 enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop)
{
if (idx<1)
{
* stop = YES ;
}
NSLog(@"%lu,%@",idx,obj);
}];
//迭代器方法遍历
NSEnumerator * enumerator =[array2objectEnumerator];
id obj ;
while (obj= [enumeratornextObject]) {
NSLog(@"%@",obj);
}
}
//其他方法
void otherArray()
{
NSArray * array1 = [NSArrayarrayWithObject:@"NSArray"];
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3",nil];
//快速遍历array
for (id obj in array2)
{
NSLog(@"%@",obj);
}
array2 = [array2 arrayByAddingObject:@"NSArray4"];
for (id obj in array2)
{
NSLog(@" %@",obj);
}
array2 = [array2 arrayByAddingObjectsFromArray:array1];
for (id obj in array2)
{
NSLog(@" %@",obj);
}
NSArray *pathArray = [NSArrayarrayWithObjects:@"here",@"be",@"dragons",nil];
NSLog(@"%@",pathArray);
NSLog(@"%@",[pathArraycomponentsJoinedByString:@"->"]);
NSRange range =NSMakeRange(0,3);
NSLog(@"%@",[array2subarrayWithRange:range]);
}
//排序
void sortArray()
{
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray2",@"NSArray1",@"NSArray3",nil];
NSLog(@"Before Sort:%@",[array2componentsJoinedByString:@" "]);
//选择排序
NSArray * selectSortArray = [array2sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSLog(@"After Sort :%@",[selectSortArraycomponentsJoinedByString:@" "]);
NSArray * array3 = [NSArrayarrayWithObjects:@"NSArray2",@"NSArray1",@"NSArray3",@"NSArray5",@"NSArray4",nil];
NSLog(@"Before Sort:%@",[array3componentsJoinedByString:@" "]);
// main.m
// Array
//
// Created by mac on 14-12-3.
// Copyright (c) 2014年 mac. All rights reserved.
//
#import<Foundation/Foundation.h>
#import "People.h"
//静态创建数组的方法
void staticCreatArray()
{
NSArray * array1 = [NSArrayarrayWithObject:@"NSArray"];
NSLog(@"Way 1: %@",array1);
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3",nil];
NSLog(@"Way 2: %@",array2);
NSArray * array3 = [NSArrayarrayWithArray:array2];
NSLog(@"Way 3: %@",array3);
[array1 release];
[array2 release];
[array3 release];
}
//动态创建数组的方法
void dynamicCreatArray()
{
NSArray * array1 = [[NSArrayalloc]initWithObjects:@"1,2,3,4,5",@"HELLO",nil];
NSLog(@"Way 1: %@",array1);
NSArray * array2 = [[NSArrayalloc]initWithArray:array1];
NSLog(@"Way 2: %@",array2);
}
//查询集合元素在NSArray中的序列
void searchArray()
{
NSArray * array1 = [NSArrayarrayWithObject:@"NSArray"];
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3",nil];
NSArray * array3 = [[NSArrayalloc]initWithObjects:@"1,2,3,4,5",@"HELLO",@"HELLO",nil];
NSLog(@"%lu",[array1count]);
NSLog(@"%hhd",[array3containsObject:@"HELLO"]);
NSLog(@"%@",[array2lastObject]);
NSLog(@"%@",[array2objectAtIndex:1]);
NSLog(@"%lu",[array2indexOfObject:@"NSArray1"]);
NSRange range =NSMakeRange(0,2);
NSLog(@"%lu",[array2indexOfObject:@"NSArray2"inRange:range]);
[array3 release];
}
//对数组进行比较
void compareArray()
{
NSArray * array1 = [NSArrayarrayWithObject:@"NSArray"];
NSArray * array11 = [array1copy];
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3",nil];
NSArray * array3 = [[NSArrayalloc]initWithObjects:@"1,2,3,4,5",@"HELLO",@"HELLO",@"NSArray1",nil];
NSLog(@"%hhd",[array1isEqualToArray:array11]);
NSLog(@"%@",[array2firstObjectCommonWithArray:array3]);
[array3 release];
}
//遍历数组
void ergodicArray()
{
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3",nil];
//C语言方式
for (int i = 0; i<[array2count]; i++)
{
NSLog(@"%@",[array2objectAtIndex:i]);
}
//快速遍历
for (id obj in array2)
{
NSLog(@" %@",obj);
}
//Block方法遍历
[array2 enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop)
{
if (idx<1)
{
* stop = YES ;
}
NSLog(@"%lu,%@",idx,obj);
}];
//迭代器方法遍历
NSEnumerator * enumerator =[array2objectEnumerator];
id obj ;
while (obj= [enumeratornextObject]) {
NSLog(@"%@",obj);
}
}
//其他方法
void otherArray()
{
NSArray * array1 = [NSArrayarrayWithObject:@"NSArray"];
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray1",@"NSArray2",@"NSArray3",nil];
//快速遍历array
for (id obj in array2)
{
NSLog(@"%@",obj);
}
array2 = [array2 arrayByAddingObject:@"NSArray4"];
for (id obj in array2)
{
NSLog(@" %@",obj);
}
array2 = [array2 arrayByAddingObjectsFromArray:array1];
for (id obj in array2)
{
NSLog(@" %@",obj);
}
NSArray *pathArray = [NSArrayarrayWithObjects:@"here",@"be",@"dragons",nil];
NSLog(@"%@",pathArray);
NSLog(@"%@",[pathArraycomponentsJoinedByString:@"->"]);
NSRange range =NSMakeRange(0,3);
NSLog(@"%@",[array2subarrayWithRange:range]);
}
//排序
void sortArray()
{
NSArray * array2 = [NSArrayarrayWithObjects:@"NSArray2",@"NSArray1",@"NSArray3",nil];
NSLog(@"Before Sort:%@",[array2componentsJoinedByString:@" "]);
//选择排序
NSArray * selectSortArray = [array2sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSLog(@"After Sort :%@",[selectSortArraycomponentsJoinedByString:@" "]);
NSArray * array3 = [NSArrayarrayWithObjects:@"NSArray2",@"NSArray1",@"NSArray3",@"NSArray5",@"NSArray4",nil];
NSLog(@"Before Sort:%@",[array3componentsJoinedByString:@" "]);
}
int main(int argc, constchar * argv[]) {
@autoreleasepool {
// insert code here...
// NSLog(@"Hello, World!");
// staticCreatArray();
// dynamicCreatArray();
// searchArray();
// compareArray();
// ergodicArray();
@autoreleasepool {
// insert code here...
// NSLog(@"Hello, World!");
// staticCreatArray();
// dynamicCreatArray();
// searchArray();
// compareArray();
// ergodicArray();
// otherArray();
// sortArray();
}
return0;
}
【Foundation Frame】NSArray
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。