首页 > 其他分享 >OC之【NSArray使用】

OC之【NSArray使用】

时间:2022-12-09 15:05:44浏览次数:42  
标签:stu1 obj 数组 NSArray OC Student 使用 array

#import <Foundation/Foundation.h>
#import "Student.h"

创建一个数组
void
// 创建一个空的数组
NSArray *array = [NSArray array];

// 创建有1个元素的数组
NSArray arrayWithObject:@"123"];

// 创建有多个元素的数组
NSArray arrayWithObjects:@"a", @"b", @"c", nil];

int count = [array count];
// count = array.count;
NSLog(@"%i", count);
}

数组的简单使用
void
NSObject *obj = [[NSObject alloc] init];
NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c" , obj, nil];
// 判断是否包含了某个元素
if ([array containsObject:@"a"]) {
NSLog(@"包含了字符串a");
}

NSString *last = [array lastObject];
NSLog(@"last=%@", last);

NSString *str = [array objectAtIndex:1];
NSLog(@"%@", str);

int index = [array indexOfObject:@"c"];
NSLog(@"index=%i", index);

release];
}

数组的内存管理
void
// 1
Student *stu1 = [[Student alloc] init];
Student *stu2 = [[Student alloc] init];
Student *stu3 = [[Student alloc] init];

NSLog(@"stu1:%zi", [stu1 retainCount]);

// 当把一个对象塞进数组中时,这个对象的计数器会加1,也就是说数组会对它做一次retain操作
// 2
NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil];

NSLog(@"stu1:%zi", [stu1 retainCount]);

NSLog(@"count=%zi", array.count);

// 1
release];
release];
release];

// 数组被销毁的时候,会对内部的所有元素都做一次release操作
// 0
release];
}

给数组里面的元素发送消息
void
Student *stu1 = [Student student];
Student *stu2 = [Student student];
Student *stu3 = [Student student];

NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil];
// 让数组里面的所有对象都调用test方法
// [array makeObjectsPerformSelector:@selector(test)];
[array makeObjectsPerformSelector:@selector(test2:) withObject:@"123"];
}

遍历数组1
void
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
int count = array.count;
for (int i = 0; i<count; i++) {
// id == void *
id obj = [array objectAtIndex:i];
NSLog(@"%i-%@", i, obj);
}
}

遍历数组2
void
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
// 快速遍历
int i =0;
for (id obj in
NSLog(@"%i-%@", i, obj);
i++;
}
}

遍历数组3
void
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
[array enumerateObjectsUsingBlock:
id obj, NSUInteger idx, BOOL
NSLog(@"%i-%@", idx, obj);

// 如果索引为1,就停止遍历
if (idx == 1) {
// 利用指针修改外面BOOL变量的值
YES;
}
}];
}

遍历数组4
void
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];

// 获取数组的迭代器
// NSEnumerator *enumerator = [array objectEnumerator];
// 反序迭代器(从尾部开始遍历元素)
NSEnumerator *enumerator = [array reverseObjectEnumerator];

// allObjects是取出没有被遍历过的对象
NSArray *array2 = [enumerator allObjects];
NSLog(@"array2:%@", array2);

// 获取下一个需要遍历的元素
id obj = nil;
while (obj = [enumerator nextObject]) {
NSLog(@"obj=%@", obj);
}
}

int main(int argc, const char
{

@autoreleasepool {
arrayFor4();
}
return 0;
}

标签:stu1,obj,数组,NSArray,OC,Student,使用,array
From: https://blog.51cto.com/u_15907570/5925306

相关文章

  • OC之【NSMutableArray的使用】
    #import<Foundation/Foundation.h>#import"Student.h"voidNSMutableArray*array=[NSMutableArrayarrayWithObject:@"1"];//添加元素addObject:@"2"];addObj......
  • OC之【@protocol协议】
    #import<Foundation/Foundation.h>@protocolStudy<NSObject>默认就是@required-(void)test3;表示必须实现的方法//虽然字面上说是必须实现,但是编译器并不强求某个类......
  • OC之【@class】
    如果是继承某个类,就要导入类的头文件@classbook.h文件:#import<Foundation/Foundation.h>Book:NSObjectintprice;@end文件:"Book.h"Book-(void)dealloc{NSL......
  • OC之【深拷贝(mutableCopy)和浅拷贝(copy)】
    文件#import<Foundation/Foundation.h>#import"Student.h"#import"GoodStudent.h"//copy语法的目的:改变副本的时候,不会影响到源对象字符串的拷贝(深拷贝)//深拷贝:内容......
  • OC之【NSNumber的使用】
    #import<Foundation/Foundation.h>void//将int类型的10包装成一个NSNumber对象NSNumber*number=[NSNumbernumberWithInt:10];NSLog(@"number=%@",number);......
  • OC之【NSDictionary详解】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"字典的初始化void//NSDictionary是不可变的NSDictionary*dict=[NSDictionarydictionaryWit......
  • OC之【c语言结构体】
    <stdio.h>void//这个机构只能在函数内部使用//定义一个名为Student的结构体类型structintage;//年龄char*name;//姓名floatheight;//身高};......
  • OC之【NSMutableDictionary的使用】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"可变字典的使用void//创建一个空的字典NSMutableDictionary*dict=[NSMutableDictionarydicti......
  • OC之【NSValue的使用】
    #import<Foundation/Foundation.h>voidCGPointpoint=CGPointMake(10,10);//将结构体变量包装成一个对象NSValue*value=[NSValuevalueWithPoint:point];......
  • OC之【enum枚举】
    void//定义一种枚举类型enum//定义一个枚举变量senumSeasons=winter;}void//定义枚举类型的同时定义一个枚举变量senumSeason{spring,summer,......