NSArray(不可变数组)、NSMutableArray(可变数组)、数组排序、NSNumber(多态:数值和对象互转)、NSValue(将结构体转换成对象)

来源:互联网 发布:江苏大学网络教学平台 编辑:程序博客网 时间:2024/04/30 23:21

1. NSArray(不可变数组

数组是存储在UI中可显示的内容的集合

1.1 创建不可变数组 

// + (instancetype)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;NSArray *arr1 = [NSArray arrayWithObjects:@"iphone", @"sanung", @"miui", nil];

1.2 复制数组

<span style="font-size:14px;">// + (instancetype)arrayWithArray:(NSArray *)array;NSArray *arr2 = [NSArray arrayWithArray:aar1];NSLog(@"%@", arr2);</span>

将arr1中的元素一一复制给arr2

打印结果:

(

    iphone,

    sanung,

    miui

)

1.3 获取元素的个数

// - (NSUInteger)count;[arr1 count];NSLog(@"count = %lu", [arr1 count]);

打印结果:

count = 3

1.4 根据index 值获取对象 

// - (id)objectAtIndex:(NSUInteger)index;NSLog(@"%@", [arr1 objectAtIndex:2]);// - (NSUInteger)indexOfObject:(id)anObject;NSLog(@"%lu", [arr1 indexOfObject:@"iphone"]);
打印结果:

miui

0

1.5 遍历数组元素

for (int i = 0; i < [arr1 count]; i++) {     NSLog(@"%@", [arr1 objectAtIndex:i]);}
打印结果:

iphone

sanung

miui

2. NSMutableArray(可变数组)

可变数组,数组必须先初始化才能使用

2.1 创建可变数组 

// + (instancetype)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;NSMutableArray *mArr1 = [NSMutableArray arrayWithObjects:@"Window", @"Unix", nil];

2.2 添加元素

// - (void)addObject:(id)anObject;[mArr1 addObject:@"Linux"];for (int i = 0; i < [mArr1 count]; i++) {    NSLog(@"%@", [mArr1 objectAtIndex:i]);}
打印结果:

Window

Unix

Linux

2.3 添加数组元素 

// - (void)addObjectsFromArray:(NSArray *)otherArray;NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];NSMutableArray *mArr2 = [NSMutableArray arrayWithObjects:@"1", @"2", nil];[mArr2 addObjectsFromArray:arr];NSLog(@"%@", mArr2);
打印结果:

(

    1,

    2,

    a,

    b,

    c

)

2.4 插入元素

// - (void)insertObject:(id)anObject atIndex:(NSUInteger)index;[mArr1 insertObject:@"Mac" atIndex:0];for (int i = 0; i < [mArr1 count]; i++) {     NSLog(@"%@", [mArr1 objectAtIndex:i]);}
打印结果:

Mac

Window

Unix

Linux

2.5 扩展: 数组嵌套数组 

NSMutableArray *mArr2 = [NSMutableArray arrayWithObjects:arr1, mArr1, nil];for (int i = 0; i < [mArr2 count]; i++) {     for (int j = 0; j < [mArr2[i] count]; j++) {          NSLog(@"%@", [[mArr2 objectAtIndex:i] objectAtIndex:j]);     }}
打印结果:

iphone

sanung

miui

Mac

Window

Unix

Linux

2.6 删除元素

// - (void)removeLastObject;[mArr1 removeLastObject];NSLog(@"%@", mArr1);

打印结果:

(

    Mac,

    Window,

    Unix

)

2.7 替换元素

// - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;[mArr1 replaceObjectAtIndex:0 withObject:@"BSD"];NSLog(@"%@", mArr1);
打印结果:

(

    BSD,

    Window,

    Unix

)    

2.8 交换两个指定的元素 

// - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;[mArr1 exchangeObjectAtIndex:0 withObjectAtIndex:1];NSLog(@"%@", mArr1);

打印结果:

(

    Window,

    BSD,

    Unix

)


3. 数组排序

3.1 不可变数组 

NSArray *arr3 = [NSArray arrayWithObjects:@"2", @"1", @"3", @"5", nil]NSArray *sortArray1 = [arr3 sortedArrayUsingSelector:@selector(compare:)];for (NSString *str in sortArray1) {     NSLog(@"%@", str);}
打印结果:

1

2

3

5

3.2 可变数组 

NSMutableArray *mArr3 = [NSMutableArray arrayWithArray:arr3];NSArray *sortArray2 = [mArr3 sortedArrayUsingSelector:@selector(compare:)];for (NSString *str in sortArray2) {     NSLog(@"%@", str);}

打印结果:

1

2

3

5


4. NSNumber(数值和对象互转)

所有的基础变量不能当对象存储到容器中(数组,字典等),因此,基础类型变量想要存储的话需要转化成对象进行存储

id == void *

4.1 数值转对象

// + (NSNumber *)numberWithInt:(int)value;int c = 1;NSNumber *num = [NSNumber numberWithInt:c];NSLog(@"%@", num);

打印结果:

1

4.2.对象数值
// @property (readonly) int intValue;NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];NSNumber *num2 = [array lastObject];int d = [num2 intValue];NSLog(@"%d", d);

打印结果:

3

4.3 基础类型变量想要存储的话需要转化成对象进行存储

// + (NSNumber *)numberWithInt:(int)value;int e = 100;NSNumber *e1 = [NSNumber numberWithInt:c];// + (NSNumber *)numberWithFloat:(float)value;float f = 1.2;NSNumber *f1 = [NSNumber numberWithFloat:f];NSMutableArray *mArr3 = [NSMutableArray arrayWithObjects:e1, f1, nil];NSLog(@"%@", mArr3);
打印结果:

(

    100,

    "1.2"

)


5. NSValue(将结构体转换成对象)
NSRange ran = NSMakeRange(0, 0);NSValue *v = [NSValue valueWithRange:ran];NSLog(@"%@", v);
打印结果:
NSRange: {0, 0}


0 0
原创粉丝点击