标签(空格分隔): 常用用法
- 它是不可变数组。
- (instancetype)initWithObjects:(ObjectType)firstObj, …
NSArray *arr1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil];
NSLog(@"arr1 = %@",arr1);
运行结果
2016-02-25 15:17:39.393 OC_NSArray和NSMutableArray[1822:112530] arr1 = (
a,
b,
c
)
+ (instancetype)arrayWithObjects:(ObjectType)firstObj, …
NSArray *arr2 = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];
NSLog(@"arr2 = %@",arr2);
运行结果
2016-02-25 15:17:39.394 OC_NSArray和NSMutableArray[1822:112530] arr2 = (
1,
2,
3
)
NSArray *arr3 = @[@"mike",@"joe",@"kitty"];
NSLog(@"arr3 = %@",arr3);
运行结果
2016-02-25 15:17:39.394 OC_NSArray和NSMutableArray[1822:112530] arr3 = (
mike,
joe,
kitty
)
- 注意:nil作为数组结束的标志,不要手动添加。
@property (readonly) NSUInteger count
NSUInteger c1 = arr1.count;
NSLog(@"c1 = %lu",c1);
运行结果
2016-02-25 15:17:39.394 OC_NSArray和NSMutableArray[1822:112530] c1 = 3
- (ObjectType)objectAtIndex:(NSUInteger)index
NSString *obj1 = [arr3 objectAtIndex:0];
NSLog(@"obj1 = %@",obj1);
运行结果
2016-02-25 15:17:39.394 OC_NSArray和NSMutableArray[1822:112530] obj1 = mike
NSLog(@"arr3[2] = %@",arr3[2]);
运行结果
2016-02-25 15:17:39.394 OC_NSArray和NSMutableArray[1822:112530] arr3[2] = kitty
- (BOOL)containsObject:(ObjectType)anObject
BOOL b1 = [arr1 containsObject:@"c"];
NSLog(@"b1 = %d",b1);
BOOL b_1 = [arr1 containsObject:@"m"];
NSLog(@"b_1 = %d",b_1);
运行结果
2016-02-25 15:17:39.394 OC_NSArray和NSMutableArray[1822:112530] b1 = 1
2016-02-25 15:17:39.394 OC_NSArray和NSMutableArray[1822:112530] b_1 = 0
- 注意:如果数组中有重复元素,则返回第一个,如果无此数,则返回一个未知数
- (NSUInteger)indexOfObject:(ObjectType)anObject
NSUInteger n1 = [arr1 indexOfObject:@"c"];
NSLog(@"n1 = %lu",n1);
NSUInteger n_1 = [arr1 indexOfObject:@"l"];
NSLog(@"n_1 = %lu",n_1);
运行结果
2016-02-25 15:17:39.394 OC_NSArray和NSMutableArray[1822:112530] n1 = 2
2016-02-25 15:17:39.394 OC_NSArray和NSMutableArray[1822:112530] n_1 = 9223372036854775807
- (NSArray
NSString *str1 = @"www.lanou3g.com";
NSArray *arr4 = [str1 componentsSeparatedByString:@"."];
NSLog(@"arr4 = %@",arr4);
运行结果
2016-02-25 15:17:39.394 OC_NSArray和NSMutableArray[1822:112530] arr4 = (
www,
lanou3g,
com
)
- (NSString )componentsJoinedByString:(NSString )separator
NSString *str2 = [arr4 componentsJoinedByString:@"/"];
NSLog(@"str2 = %@",str2);
运行结果
2016-02-25 15:17:39.395 OC_NSArray和NSMutableArray[1822:112530] str2 = www/lanou3g/com
提取出给定字符串中的所有*.jpg
NSString *string = @"http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg";
NSArray *netAddress = [string componentsSeparatedByString:@"&"];
NSLog(@"%@",netAddress);
for(int i = 0; i < netAddress.count; i++){
NSArray *picArr = [netAddress[i] componentsSeparatedByString:@"/"];
NSLog(@"%@",picArr[picArr.count - 1]); // count-1 指数组最后一个元素
}
运行结果
2016-02-25 15:17:39.395 OC_NSArray和NSMutableArray[1822:112530] (
"http://www.imanhua.com/Cover/2011-10/hyrz.jpg",
"http://www.imanhua.com/Cover/2011-09/op.jpg",
"http://www.imanhua.com/Cover/2012-04/yjdwb.jpg"
)
2016-02-25 15:17:39.395 OC_NSArray和NSMutableArray[1822:112530] hyrz.jpg
2016-02-25 15:17:39.429 OC_NSArray和NSMutableArray[1822:112530] op.jpg
2016-02-25 15:17:39.429 OC_NSArray和NSMutableArray[1822:112530] yjdwb.jpg
- 它是可变数组
- (instancetype)initWithCapacity:(NSUInteger)numItems
NSMutableArray *marr1 = [[NSMutableArray alloc] initWithCapacity:10];
+ (instancetype)arrayWithCapacity:(NSUInteger)numItems
NSMutableArray *marr2 = [NSMutableArray arrayWithCapacity:10];
- 字面量创建的数组是不可变的,因此在此需要使用.mutableCopy
NSMutableArray *marr3 = @[@"a",@"b",@"c"].mutableCopy;
- (void)addObject:(ObjectType)anObject
[marr1 addObject:@"a"];
[marr1 addObject:@"b"];
[marr1 addObject:@"c"];
[marr1 addObject:@"x"];
[marr1 addObject:@"e"];
NSLog(@"marr1 = %@",marr1);
运行结果
2016-02-25 15:17:39.429 OC_NSArray和NSMutableArray[1822:112530] marr1 = (
a,
b,
c,
x,
e
)
- (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index
[marr1 insertObject:@"x" atIndex:2];
NSLog(@"marr1 = %@",marr1);
运行结果
2016-02-25 15:17:39.429 OC_NSArray和NSMutableArray[1822:112530] marr1 = (
a,
b,
x,
c,
x,
e
)
- (如果数组中有重复对象,则全部删除)
- (void)removeObject:(ObjectType)anObject
[marr1 removeObject:@"x"];
NSLog(@"marr1 = %@",marr1);
运行结果
2016-02-25 15:17:39.429 OC_NSArray和NSMutableArray[1822:112530] marr1 = (
a,
b,
c,
e
)
- (void)removeObjectAtIndex:(NSUInteger)index
[marr1 removeObjectAtIndex:0];
NSLog(@"marr1 = %@",marr1);
运行结果
2016-02-25 15:17:39.429 OC_NSArray和NSMutableArray[1822:112530] marr1 = (
b,
c,
e
)
- (void)removeLastObject
[marr1 removeLastObject];
NSLog(@"marr1 = %@",marr1);
运行结果
2016-02-25 15:17:39.429 OC_NSArray和NSMutableArray[1822:112530] marr1 = (
b,
c
)
- (void)removeAllObjects
[marr1 removeAllObjects];
NSLog(@"marr1 = %@",marr1);
运行结果
2016-02-25 15:17:39.429 OC_NSArray和NSMutableArray[1822:112530] marr1 = (
)
NSMutableArray *marr4 = @[@"a",@"b",@"c",@"d",@"c"].mutableCopy;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)anObject
[marr4 replaceObjectAtIndex:marr4.count - 1 withObject:@"e"];
NSLog(@"marr4 = %@",marr4);
运行结果
2016-02-25 15:17:39.429 OC_NSArray和NSMutableArray[1822:112530] marr4 = (
a,
b,
c,
d,
e
)
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2
[marr4 exchangeObjectAtIndex:0 withObjectAtIndex:3];
NSLog(@"marr4 = %@",marr4);
运行结果
2016-02-25 15:17:39.430 OC_NSArray和NSMutableArray[1822:112530] marr4 = (
d,
b,
c,
a,
e
)
Program ended with exit code: 0