iOS入门小知识-数组

2021-11-16  本文已影响0人  BiBiMan

C数组主要用于存储基本数据类型 (int, char, float, double...),以及复合数据类型(int *, int[6]...)
OC数组是一个对象,主要用于存储“对象”,可以存储不同类型的对象,数组分不可变数组NSArray和可变数组NSMutableArray,数组中存储的其实是每个对象的地址,并不是真的存储对象,所以通常一个对象会被存储到多个数组中,但所存储的对象地址指向的都是同一个对象的内存空间,也就是同一个对象

//基础数据类型
char c = 'z';
char cc[] = "1";
int i = 10;
bool d = YES;
NSNumber *obj1 = [NSNumber numberWithChar:c];
NSString *obj2 = [NSString stringWithUTF8String:cc];
NSString *obj3 = [[NSString alloc] initWithBytes:&c length:sizeof(c) encoding:NSUTF8StringEncoding];
NSNumber *obj4 = [NSNumber numberWithInt:i];
NSNumber *obj5 =[NSNumber numberWithBool:d];
        
//任意类对象
NSObject *obj6 = [[NSObject alloc] init];
        
NSArray *array = @[obj1, obj2, obj3, obj4, obj5, obj6];
NSLog(@"%@\n",array);
~
打印日志:
(
122,
1,
z,
10,
1,
"<NSObject: 0x1038305f0>"
)

NSArray

NSArray *arr = [[NSArray alloc] init];
[NSArray arrayWithArray:arr];
[NSArray arrayWithObject:@""];
[NSArray arrayWithObjects:@"", nil];
NSString *objs[] = {@""};
[NSArray arrayWithObjects:objs count:1];
//字符串分割
NSString *originStr = @"current string,contain one#two and three,other four";
//以字符串分割
NSArray *separatArr = [originStr componentsSeparatedByString:@" "];
//以字符集合中的字符分割
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@", #"];
NSArray *setArr = [originStr componentsSeparatedByCharactersInSet:set];
NSLog(@"separatArr = %@\nsetArr = %@",separatArr,setArr);
~
打印日志:
separatArr = (
    current,
    "string,contain",
    "one#two",
    and,
    "three,other",
    four
)
setArr = (
    current,
    string,
    contain,
    one,
    two,
    and,
    three,
    other,
    four
)

NSMutableArray

NSMutableArray继承自NSArray,所以基本可以使用NSArray构造方法来创建NSMutableArray对象,不同的是NSMutableArray可以在原数组内进行元素的

NSMutableArray *arr = [NSMutableArray array];
[NSMutableArray arrayWithArray:arr];
[NSMutableArray arrayWithObject:@""];
[NSMutableArray arrayWithObjects:@"", nil];
NSString *objs[] = {@""};
[NSMutableArray arrayWithObjects:objs count:1];
NSMutableArray *arr = [NSMutableArray array];
//增
[arr insertObject:@"1" atIndex:0];
[arr addObject:@"2"];
[arr addObjectsFromArray:@[@"3",@"4"]];
//删
[arr removeObject:@"4"];
[arr removeLastObject];
//改
[arr exchangeObjectAtIndex:0 withObjectAtIndex:1];
[arr replaceObjectAtIndex:0 withObject:@"5"];
//查
[arr objectAtIndex:0];
[arr firstObject];
[arr lastObject];
//重置数组
[arr setArray:@[@"1",@"2",@"3",@"4"]];

深浅拷贝

Person *obj1 = [[Person alloc] init];
Person *obj2 = [[Person alloc] init];
//当把对象加入到数组里时候,对象的引用计数默认+1
NSArray *arr = [NSArray arrayWithObjects:obj1, obj2, nil];
//flag = NO时为浅拷贝,数组中的元素(对象)引用计数+1
NSArray *newArr1 = [[NSArray alloc] initWithArray:arr copyItems:NO];
//flag = YES时为深拷贝,数组中的元素(对象)生成副本
NSArray *newArr2 = [[NSArray alloc] initWithArray:arr copyItems:YES];
NSLog(@"arr = %@\nnewArr1 = %@\nnewArr2 = %@",arr,newArr1,newArr2);
~
打印日志:
arr = (
    "<Person: 0x100648710>",
    "<Person: 0x100646430>"
)
newArr1 = (
    "<Person: 0x100648710>",
    "<Person: 0x100646430>"
)
newArr2 = (
    "<Person: 0x100646600>",
    "<Person: 0x100646710>"
)
上一篇下一篇

猜你喜欢

热点阅读