标签:
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
#pragma make创建数组
//数组中得元素,必须是对象
//通过实例方法创建
NSArray*array=[[NSArray alloc]initWithObjects:@"zhangsan", @"lisi", @"wangwu" ,nil];
NSLog(@"%@",array);
//表示数字对象
NSNumber*number=[NSNumber numberWithInt:1];
//通过类方法创建
NSArray*array2=[NSArray arrayWithObjects:number,@(2), @(3), nil];
NSLog(@"%@",array2);
//通过已存在的数组,来创建数组
NSArray*array3=[NSArray arrayWithArray:array];
NSLog(@"%@",array3);
//字面量语法,创建出来的数组一定是不可变数组
//数组的字面量语法
NSArray*array4=@[@"haha",array,array2];
NSLog(@"%@",array4);
#pragma make通过下标来获取数组的元素
id obj=[array4 objectAtIndex:0];
//获取第一个元素
id obj2=[array4 firstObject];
//取下标的字面量语法
id obj3=array4[0];
NSLog(@"%@-%@-%@",obj,obj2,obj3);
//取最后一个元素(通过消息)
id obj4=[array4 lastObject];
NSLog(@"%@",obj4);
//获取数组长度
NSUInteger length=[array4 count];
NSLog(@"%d",length);
//取出最后一个元素
id obj5=array4[length-1];
NSLog(@"%@",obj5);
#pragma make子数组
//获取指定范围的子数组
NSArray*subArr=[array4 subarrayWithRange:NSMakeRange(1, 2)];
NSLog(@"%@",subArr);
//下标集合(无序集合)
//NSIndexSet不可变,无法扩充
//NSIndexSet*indeSet=[NSIndexSet indexSetWithIndex:0];
//可变下标集合
NSMutableIndexSet*mSet=[NSMutableIndexSet indexSetWithIndex:0];
[mSet addIndex:2];
NSLog(@"%@",mSet);
//通过下标集合,获取子数组
// NSArray *subArr2=[array4 objectAtIndex:mSet];
// NSLog(@"%@",subArr2);
//oc动态绑定(运行时)
id obj6=array4[2];
NSLog(@"%@",obj6);
#pragma make判断一个对象是否是某个类
//判断一个对象是否是某个类
if ([obj6 isKindOfClass:[NSString class]]) {
NSString*string=(NSString*)obj6;
NSLog(@"是字符串");
}
else{
//NSString *string = (NSString *)obj6;
//[string length];
NSLog(@"不是");
}
#pragma make遍历
Person*p=[[Person alloc]init];
NSArray *tests = @[@"12", @(333), p, @"ss", @(33)];
//数组遍历 方法1
// for (int i = 0; i < [tests count]; i++)
// {
// id obj = tests[i];
// if ([obj isKindOfClass:[NSString class]])
// {
// NSLog(@"该对象为字符串");
// }
// else if ([obj isKindOfClass:[NSNumber class]])
// {
// NSLog(@"该对象为number");
// }
// else if ([obj isKindOfClass:[Person class]])
// {
// NSLog(@"该对象为人");
// }
// }
//快速枚举 方法2 无法逆序遍历数组
//效率高
for (id obj in tests)
{
if ([obj isKindOfClass:[NSString class]])
{
NSLog(@"该对象为字符串");
}
else if ([obj isKindOfClass:[NSNumber class]])
{
NSLog(@"该对象为number");
NSNumber *number = (NSNumber *)obj;
NSLog(@"%d", [number intValue] +1);
}
else if ([obj isKindOfClass:[Person class]])
{
NSLog(@"该对象为人");
}
}
//枚举器遍历数组 方法3 有点类似于链表遍历
//正序枚举器
// NSEnumerator *enumer = [tests objectEnumerator];
//逆序枚举器
NSEnumerator *enumer = [tests reverseObjectEnumerator];
id enumerObj = nil;
while (enumerObj = [enumer nextObject])
{
//打印对象的时候,其实是发消息 description
NSLog(@"%@", enumerObj);
}
//块枚举遍历 方法4 block (匿名的函数)
[tests enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (idx > 2)
{
*stop = YES;
}
NSLog(@"%@", obj);
}];
NSArray *fruits = @[@"apple", @"pear", @"pineApple", @"orange", @"banana", @"lemon", @"coconut"];
NSString *maxStr = nil;
NSString *minStr = [fruits firstObject];
for (NSString *string in fruits)
{
if ([string length] > [maxStr length])
{
maxStr = string;
}
if ([string length] < [minStr length])
{
minStr = string;
}
}
NSLog(@"名字最长单词:%@,名字最短的单词:%@", maxStr, minStr);
}
#pragma make可变数组
//通过实例方法,创建可变数组
NSMutableArray *mArr = [[NSMutableArray alloc] initWithObjects:@"zhangsan", @"lisi", @"wangwu", nil];
//通过类方法创建
NSMutableArray *mArr2 = [NSMutableArray arrayWithObjects:@"zhangsan", @"lisi", @"wangwu", nil];
NSMutableArray *mArr3 = [[NSMutableArray alloc] init];
// NSMutableArray *mArr3;
NSLog(@"mArr3:%@", mArr3);
//只要分配内存,就不等于空
if (mArr3 == nil)
{
NSLog(@"数组为空");
}
//给可变数组添加元素 必须先给数组分配内存空间
[mArr3 addObject:@"zhangsan"];
NSLog(@"mArr3:%@", mArr3);
//提前分配号空间的创建方式 , 效率高一些
NSMutableArray *mArr4 = [[NSMutableArray alloc] initWithCapacity:3];
[mArr4 addObject:@"1"];
[mArr4 addObject:@"2"];
[mArr4 addObject:@"3"];
[mArr4 addObject:@"4"];
NSLog(@"mArr4:%@", mArr4);
//通过数组中的对象,获取其在数组的下标
NSInteger index = [mArr4 indexOfObject:@"2"];
if (index == NSNotFound)
{
NSLog(@"没有找到对应元素");
}
//插入一个元素
[mArr4 insertObject:@"x" atIndex:index];
[mArr4 addObject:@"x"];
NSLog(@"marr4:%@", mArr4);
//删除指定下标
// [mArr4 removeObjectAtIndex:10];
//删除指定对象 删除数组中所有相同的对象
// [mArr4 removeObject:@"x"];
//删除所有元素
// [mArr4 removeAllObjects];
if(mArr4 == nil)
{
NSLog(@"数组为空");
}
NSLog(@"marr4:%@", mArr4);
//交换元素 @"1" , @"3"
[mArr4 exchangeObjectAtIndex:[mArr4 indexOfObject:@"1"] withObjectAtIndex:[mArr4 indexOfObject:@"3"]];
NSLog(@"marr4:%@", mArr4);
//逆序数组
NSMutableArray *mArr5 = [NSMutableArray arrayWithArray:@[@"11", @"1", @"222", @"3333", @"44444"]];
for (int i = 0; i < [mArr5 count]/2; i++)
{
[mArr5 exchangeObjectAtIndex:i withObjectAtIndex:[mArr5 count]-1 -i ];
}
NSLog(@"marr5:%@", mArr5);
#if 0
//按长度从小到大排列数组
for (int i = 0; i < [mArr5 count]; i++)
{
for (int j = 0; j < [mArr5 count]; j++)
{
if ([mArr5[j] length] > [mArr5[i] length])
{
[mArr5 exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
NSLog(@"marr5:%@", mArr5);
#endif
//使用系统方法(compare:)进行排序
NSArray *sortedArr = [mArr5 sortedArrayUsingSelector:@selector(compare:)];
//@selector(方法名) 选择子 -> SEL 方法整型值
//提升调用方法的效率
NSLog(@"marr5:%@", mArr5);
NSLog(@"sortedArr:%@", sortedArr);
[mArr5 sortUsingSelector:@selector(compare:)];
NSLog(@"marr5:%@", mArr5);
NSMutableArray *mArr6 = [NSMutableArray arrayWithArray:@[@"2", @"1",@"15", @"26", @"3", @"6", @"30"]];
// [mArr6 sortUsingSelector:@selector(compare:)];
NSLog(@"mArr6:%@", mArr6);
//块排序
[mArr6 sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSLog(@"%@, %@", obj1, obj2);
return [obj1 compare:obj2];
}];
NSLog(@"mArr6:%@", mArr6);
//直接调用某个方法 动态绑定
// [mArr6 performSelector:@selector(sortUsingComparator:)];
// //延时几秒后执行方法
// [mArr6 performSelector:@selector(sortUsingComparator:) withObject:nil afterDelay:3];
NSString *string = @"123 456-789+000";
NSArray *array = [string componentsSeparatedByString:@"-"];
NSLog(@"array%@", array);
//通过字符串构造字符集合
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@" -+"];
//通过字符集合分割字符串
NSArray *array2 = [string componentsSeparatedByCharactersInSet:set];
NSLog(@"array2:%@", array2);
NSString *newStr = [array2 componentsJoinedByString:@"###"];
NSLog(@"%@", newStr);
//统计这段单词数 (15)
NSString *source = @"I am a student, good good study, day day up. I love programing very much.";
NSArray *words = [source componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" ,."]];
NSMutableArray *mWords = [NSMutableArray arrayWithArray:words];
[mWords removeObject:@""];
NSLog(@"单词个数%ld, %@", [mWords count], words);
return 0;
}
标签:
原文地址:http://www.cnblogs.com/sunbinboke/p/4725893.html