Objective-C中数组(NSArray和NSMutableArray)常用用法

NSArray和NSMutableArray常用用法

标签(空格分隔): 常用用法


一、NSArray的常用用法

  • 它是不可变数组。

1、创建数组

1)初始创建

- (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
)

2)使用便利构造器

+ (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
)

3)字面量(结尾不需要加nil)

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作为数组结束的标志,不要手动添加。

2、获取数组中元素个数

@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

3、获取数组中指定下标对应的元素

1)初始获取

- (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

2)字面量获取

NSLog(@"arr3[2] = %@",arr3[2]);

运行结果

2016-02-25 15:17:39.394 OC_NSArray和NSMutableArray[1822:112530] arr3[2] = kitty

4、判断数组中是否包含某个元素

- (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

5、返回一个元素在数组中的索引(即下标)

  • 注意:如果数组中有重复元素,则返回第一个,如果无此数,则返回一个未知数

- (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

6、按照给定的字符串进行分割字符串

- (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
)

7、将数组中的元素按照给定的字符串格式拼接成一个完整的字符串对象

- (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

8、小练习

提取出给定字符串中的所有*.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

二、NSMutableArray的常用用法

  • 它是可变数组

1、创建数组

1)初始创建

- (instancetype)initWithCapacity:(NSUInteger)numItems

NSMutableArray *marr1 = [[NSMutableArray alloc] initWithCapacity:10];

2)便利构造器创建

+ (instancetype)arrayWithCapacity:(NSUInteger)numItems

NSMutableArray *marr2 = [NSMutableArray arrayWithCapacity:10];

4)字面量创建

  • 字面量创建的数组是不可变的,因此在此需要使用.mutableCopy
NSMutableArray *marr3 = @[@"a",@"b",@"c"].mutableCopy;

2、添加元素

- (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
)

3、数组中指定位置插入一个对象

- (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
)

4、数组中删除一个对象

  • (如果数组中有重复对象,则全部删除)

1)按元素删除

- (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
)

2)按位置删除

- (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
)

3)删除最后一个

- (void)removeLastObject

[marr1 removeLastObject];
NSLog(@"marr1 = %@",marr1);

运行结果

2016-02-25 15:17:39.429 OC_NSArray和NSMutableArray[1822:112530] marr1 = (
    b,
    c
)

4)删除全部

- (void)removeAllObjects

[marr1 removeAllObjects];
NSLog(@"marr1 = %@",marr1);

运行结果

2016-02-25 15:17:39.429 OC_NSArray和NSMutableArray[1822:112530] marr1 = (
)

5、替换和交换元素

NSMutableArray *marr4 = @[@"a",@"b",@"c",@"d",@"c"].mutableCopy;

1)使用指定元素替换指定位置上的元素

- (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
)

2)交换两个指定位置的元素

- (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

你可能感兴趣的:(笔记总结)