首页 > 白话Objective-C > 白话Objective-C之NSArray

白话Objective-C之NSArray

NSArray是不可变的数组,而且只能储存Objective-C对象。另外,数组的最后一个元素一定是nil,表示结束。注:这些集合类只能收集cocoa对象(NSOjbect对象),如果想保存一些原始的C数据(例如,int, float, double, BOOL等),则需要将这些原始的C数据封装成NSNumber类型的,NSNumber对象是cocoa对象,可以被保存在集合类中。

如上所述,使用NSArray的时候需要注意:

  • NSArray只能存储Objective-C对象,不能存储一些原始的C类型
  • NSArray以nil标志数组结尾,所以我们不能存储nil(0或者NULL值)

重要的方法如下:

Creating an Array
+ arrayWithObjects:

Initializing an Array
– initWithObjects:

Querying an Array
– count
– containsObject:
– lastObject
– objectAtIndex:

Finding Objects in an Array
– indexOfObject:

Sorting
– sortedArrayUsingDescriptors:
– sortedArrayUsingSelector:
– sortedArrayUsingComparator:

Working with String Elements
– componentsJoinedByString:

实例代码:

//
//  main.m
//  NSArray
//
//  Created by sam on 13-8-17.
//  Copyright (c) 2013年 http://helloitworks.com/. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Task : NSObject
@property (assign)int order;
@property (copy)NSString *name;
@end

@implementation Task

@end

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        //Creating an Array
        NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];

        //arrayWithObjects是类方法,另外可以用实例方法initWithObjects创建。
        //NSArray *array = [[NSArray alloc]initWithObjects:@"one", @"two", @"three", nil];

        //Querying an Array
        for (NSInteger i = 0; i < [array count]; i++){
            NSLog (@"index %lu has %@.",i, [array objectAtIndex:i]);
        }

        NSString *object = @"one";
        if ([array containsObject:object]) {
            NSLog(@"array contains object %@",object);
        } else {
            NSLog(@"array do not contain object %@",object);
        }

        NSLog(@"last object is %@",[array lastObject]);

        //Finding Objects in an Array
        NSInteger index = [array indexOfObject:object];
        NSLog (@"indexofObject %@ is %lu",object,index);

        //sort menthod 1:sortedArrayUsingDescriptors。若是mutableArray,则使用sortUsingDescriptors
        Task *task1 = [[Task alloc]init];
        task1.order = 1;
        task1.name = @"one";
        Task *task2 = [[Task alloc]init];
        task2.order = 2;
        task2.name = @"two";
        NSArray *tasksArray = [NSArray arrayWithObjects:task1,task2,nil];
        //根据task对象order属性的值,从大到小排序。 
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"order" ascending:NO];
        NSArray *sortedTasksArray = [tasksArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor,nil]];
        for (NSInteger i = 0; i < [sortedTasksArray count]; i++) {
            Task *task  = [sortedTasksArray objectAtIndex:i];
            NSLog (@"index %lu has %@.",i,[NSString stringWithFormat:@"order = %d,name = %@",task.order,task.name]);
        }

        //sort menthod 2:sortedArrayUsingSelector。若是mutableArray,则使用sortUsingSelector
        NSArray *numbersArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],nil];
        //根据number从小到大排序,compare是NSNumber默认的比较函数,我们也可以自定义自己的比较方法
        NSArray *sortedNumbersArray = [numbersArray sortedArrayUsingSelector:@selector(compare:)];
        for (NSInteger i = 0; i < [sortedNumbersArray count]; i++) {
            NSNumber *number = [sortedNumbersArray objectAtIndex:i];
            NSLog (@"index %lu has %@.",i,number);
        }

        ////Working with String Elements
        //split string to array, array will has three objects: @"red" @green" @"blue"
        NSString *string = @"red:green:blue";
        NSArray *chunks = [string componentsSeparatedByString: @":"];
        for (NSInteger i = 0; i < [chunks count]; i++){
            NSLog (@"index %lu has %@.",i,[chunks objectAtIndex:i]);
        }

        //join array to string, string will be red-green-blue
        string = [chunks componentsJoinedByString: @"-"];
        NSLog(@"string after componentsJoinedByString is %@",string);

    }

    return 0;
}

参考资料:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html

(转载本站文章请注明出处 www.helloitworks.com ,请勿用于任何商业用途)

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.