美文网首页
ios 数组处理记录

ios 数组处理记录

作者: 船长One | 来源:发表于2017-06-02 14:14 被阅读100次

    //创建一个数组

    NSArray*myArray;

    NSValue*aValue = [NSNumbernumberWithInt:5];

    NSString*aString =@"nihoa";

    // NSString *aString = [NSString stringWithFormat:@"nihao"];

    myArray = [NSArrayarrayWithObjects:aValue, aString,nil];//用便利构造器

    NSLog(@"%@",myArray);

    NSArray*arr = [[NSArrayalloc]initWithObjects:@"one",@"two",nil];//用alloc+init

    NSLog(@"%@",arr);

    //查询数组中是否有某个元素返回值是BOOL类型

    if([arrcontainsObject:@"one"])

    NSLog(@"YES");

    else

    NSLog(@"NO");

    //元素个数

    NSLog(@"%lu",[arrcount]);

    [arrrelease];

    //检索元素 并存放在C语言的数组中

    NSArray*arr1 = [[NSArrayalloc]initWithObjects:@"one",@"two",@"three",@"four",nil];

    id*objects;

    NSUIntegercount = [arr1count];

    objects =malloc(sizeof(id) * count);//objects数组名

    [arr1getObjects:objects];

    for(NSUIntegeri =0; i < count; i++) {

    NSLog(@"object at index %lu: %@", i, objects[i]);

    }

    free(objects);

    //检索某个下标的元素

    NSLog(@"%@",[arr1objectAtIndex:0]);

    //检索某个元素的下标

    NSLog(@"%lu",[arr1indexOfObject:@"two"]);

    NSLog(@"%lu",[arr1indexOfObject:@"two"inRange:NSMakeRange(0,3)]);

    //在数组中增加元素

    NSArray*arr2 = [arr1arrayByAddingObject:@"tiandadida"];

    NSLog(@"%@",arr2);

    [arr1release];

    //排序

    NSArray*arrnumber= [[NSArrayalloc]initWithObjects:@"nihao",@"wo",@"helloword",@"zhonggu",nil];

    NSArray*te = [arrnumbersortedArrayUsingComparator: ^(NSString*s,NSString*s2){

    if(s.length< s2.length){

    returnNSOrderedDescending;

    }

    if(s.length> s2.length){

    returnNSOrderedAscending;

    }

    // NSLog(@"...........................");

    returnNSOrderedSame;

    }];

    NSLog(@"te=%@.",te);

    //firstObjectCommonWithArray的用法

    NSArray*arrnum= [[NSArrayalloc]initWithObjects:@"haoma",@"wo",@"helloword",@"zhongguo",nil];

    NSLog(@"%@",[arrnumberfirstObjectCommonWithArray:arrnum]);

    //快速枚举

    NSArray*ar = [NSArrayarrayWithObjects:@"one",@"two",@"three",nil];

    for(NSString*elementinar) {

    NSLog(@"element: %@",element);

    }

    //lastobject

    NSLog(@"%@",[arlastObject]);

    //把数组元素(字符串),连接起来.

    NSString*string = [arcomponentsJoinedByString:@"->"];

    NSLog(@"%@",string);

    //某个范围的子串

    NSRangetheRange;

    theRange.location=0;//range的起点

    theRange.length= [arcount] /2;//range的长度

    NSArray*halfArray = [arsubarrayWithRange:theRange];

    NSLog(@"%@",halfArray);

    //description Returns a string that represents the contents of the array, formatted as a property list.

    NSString*test = [ardescription];

    NSLog(@"%@",test);

    }

    相关文章

      网友评论

          本文标题:ios 数组处理记录

          本文链接:https://www.haomeiwen.com/subject/nskufxtx.html