#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *array = [NSArray arrayWithObjects:@"你好",@"ios",@"安卓",@"HTML",@"YINGYU",@"迅速", nil];
NSLog(@"第一个元素为%@",[array objectAtIndex:0]);
NSLog(@"索引为1的元素%@",[array objectAtIndex:1]);
NSLog(@"最后一个元素:%@",[array lastObject]);
//索引从3-5的元素组成的新的集合
NSArray *array1 = [array objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(3, 3)]];
NSLog(@"%@",array1);
//获取元素在集合中的位置
NSLog(@"HTML的位置为:%ld",[array indexOfObject:@"HTML"]);
//获取元素在集合指定范围中的位置
NSLog(@"在2~4范围内HTML的位置为:%ld",[array indexOfObject:@"HTML" inRange:NSMakeRange(2, 3)]);
//向数组增加元素
array = [array arrayByAddingObject:@"我是"];
array = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"PHP",@"雄鹰",@"小鸟",@"跳跃", nil]];
for (int i = 0; i
输出为:
2016-02-29 09:07:49.742 数组[2626:155312] 第一个元素为你好
2016-02-29 09:07:49.743 数组[2626:155312] 索引为1的元素ios
2016-02-29 09:07:49.743 数组[2626:155312] 最后一个元素:迅速
2016-02-29 09:07:49.743 数组[2626:155312] (
HTML,
YINGYU,
"\U8fc5\U901f"
)
2016-02-29 09:07:49.743 数组[2626:155312] HTML的位置为:3
2016-02-29 09:07:49.743 数组[2626:155312] 在2~4范围内HTML的位置为:3
2016-02-29 09:07:49.743 数组[2626:155312] 你好
2016-02-29 09:07:49.743 数组[2626:155312] ios
2016-02-29 09:07:49.743 数组[2626:155312] 安卓
2016-02-29 09:07:49.743 数组[2626:155312] HTML
2016-02-29 09:07:49.743 数组[2626:155312] YINGYU
2016-02-29 09:07:49.743 数组[2626:155312] 迅速
2016-02-29 09:07:49.743 数组[2626:155312] 我是
2016-02-29 09:07:49.744 数组[2626:155312] PHP
2016-02-29 09:07:49.744 数组[2626:155312] 雄鹰
2016-02-29 09:07:49.744 数组[2626:155312] 小鸟
2016-02-29 09:07:49.744 数组[2626:155312] 跳跃
Program ended with exit code: 0
//对数组进行排序
#import
//定义一个比较函数
NSInteger intSort (id num1,id num2, void *context)
{
int v1 = [num1 intValue];
int v2 = [num2 intValue];
if (v1v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
//初始化一个数组对象
NSArray *arr1 = [NSArray arrayWithObjects:@"OC",@"JAVA",@"HTML",@"Orcal",@"JSP",@"PUSH",@"POP", nil];
//使用集合元素的compare:方法进行排序
arr1 = [arr1 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"1、%@",arr1);
//继续初始化一个数组对象
NSArray *arr2 = [NSArray arrayWithObjects:[NSNumber numberWithInt:20],[NSNumber numberWithInt:29],[NSNumber numberWithInt:40],[NSNumber numberWithInt:-4],[NSNumber numberWithInt:59] ,nil];
NSLog(@"2、%@",[arr2 sortedArrayUsingSelector:@selector(compare:)]);
//使用sort方法进行排序
arr2 = [arr2 sortedArrayUsingFunction:intSort context:nil];
NSLog(@"3、%@",arr2);
//使用代码块对集合进行元素排序
NSArray *arr3 = [arr2 sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
//该代码块就是根据集合元素的intValue进行比较
if ([obj1 intValue] > [obj2 intValue]) {
return NSOrderedDescending;
}
if ([obj1 intValue] < [obj2 intValue]) {
return NSOrderedAscending;
}
return NSOrderedSame;
}];
NSLog(@"4、%@",arr3);
}
return 0;
}
数组的KVC KVO
程序清单:LYXUser.h
#import
@interface LYXUser : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *pass;
-(id)initWithName:(NSString *)name pass:(NSString *)pass;
@end
程序清单:LYXUser.m
#import "LYXUser.h"
@implementation LYXUser
-(id)initWithName:(NSString *)aname pass:(NSString *)apass
{
if (self = [super init]) {
_name = aname;
_pass = apass;
}
return self;
}
@end
#import
#import "LYXUser.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *array = [NSArray arrayWithObjects:
[[LYXUser alloc]initWithName:@"xiaohua" pass:@"afd"],
[[LYXUser alloc]initWithName:@"xiaolie" pass:@"afd"],
[[LYXUser alloc]initWithName:@"lili" pass:@"hahah"],
[[LYXUser alloc]initWithName:@"zhende" pass:@"haoxiang"],nil];
id arr = [array valueForKey:@"name"];
NSLog(@"%@",arr);
[array setValue:@"新名字" forKey:@"name"];
NSLog(@"%@",array);
id arr1 = [array valueForKey:@"name"];
NSLog(@"%@",arr1);
}
return 0;
}
2016-02-29 17:13:51.767 数组[23070:2218035] (
xiaohua,
xiaolie,
lili,
zhende
)
2016-02-29 17:13:51.767 数组[23070:2218035] (
"\U65b0\U540d\U5b57",
"\U65b0\U540d\U5b57",
"\U65b0\U540d\U5b57",
"\U65b0\U540d\U5b57"
)
2016-02-29 17:13:51.767 数组[23070:2218035] (
"\U65b0\U540d\U5b57",
"\U65b0\U540d\U5b57",
"\U65b0\U540d\U5b57",
"\U65b0\U540d\U5b57"
)
Program ended with exit code: 0