Objective-C:NSArray的常见操作

NSArray不可变字符串的主要操作有:创建、枚举、排序、与NSString之间的相互转换

注意:

        NSArray可以存对象,不可以存基本数据类型、结构体、数组、指针、nil、NULL

        NSArray用nil作为结束标识符。

        NSNull可以代表一个空对象。

整个操作还是通过代码来体现吧:

 

    .h Person类的声明文件如下:  

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property(nonatomic,copy)NSString* name;

@property(nonatomic,assign)NSInteger age;

+(Person*)personWithName:(NSString*)name andAge:(NSInteger)age;

-(id)initWithName:(NSString*)name andAge:(NSInteger)age;

-(NSComparisonResult)compareByName:(Person *)aPerson;//声明排序方式为通过姓名比较

-(NSComparisonResult)compareByAge:(Person *)aPerson;//声明排序方式为通过年龄比较

@end

 

    .m Person类的实现文件如下:

#import "Person.h"

@implementation Person

+(Person*)personWithName:(NSString*)name andAge:(NSInteger)age

{

    return [[Person alloc]initWithName:name andAge:age];

}

-(id)initWithName:(NSString*)name andAge:(NSInteger)age

{

    self = [super init];

    if(self)

    {

        _name = name;

        _age = age;

    }

    return self;

}

-(NSString*)description//由于数组存储的是自定义的对象Person对象,所以复写description方法,将NSlog重新格式化输出

{

    return  [NSString stringWithFormat:@"name=%@,age=%ld",_name,_age];

}

-(NSComparisonResult)compareByName:(Person *)aPerson //返回一个selector选择器选择的比较方式,字符串比较默认为NSOrderAsecding

{

    return [_name compare:aPerson.name];

}

-(NSComparisonResult)compareByAge:(Person *)aPerson//与上同理

{

    if(_age > aPerson.age)

        return NSOrderedDescending;

    else if (_age < aPerson.age)

        return NSOrderedAscending;

    else

        return NSOrderedSame;

}

@end

 

    主函数测试如下:

#import <Foundation/Foundation.h>

#import "Person.h"

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

{

    @autoreleasepool

    {

        //NSArray测试

        /*

        NSArray可以存对象,不可以存基本数据类型、结构体、数组、指针、nil、NULL

        NSArray用nil作为结束标识符。

        NSNull可以代表一个空对象。

        */

        //创建一个空对象

        NSNull *nul = [NSNull null];

        

        //在一个array中不要求只存相同类型的对象,可以存储任意类型的对象

        NSArray *arr = @[@1,@"one",@"2",nul];

        NSLog(@"%@",arr);

        

        

        //枚举

        NSEnumerator *enumerator = [arr objectEnumerator];

        id obj;

        while(obj = [enumerator nextObject])

        {

            NSLog(@"%@",obj);

        }

        

        

        //保存到文件中(必须是OC内置的对象,自定义的对象会出错)

        [arr writeToFile:@"/Users/mac/Desktop/arr.txt" atomically:YES];

        

        

        //读文件创建NSArray

        NSArray *arr1 = [NSArray arrayWithContentsOfFile:@"/Users/mac/Desktop/arr.txt"];

        NSLog(@"%@",arr1);

        

        

        //排序

        NSArray *arr2 =  @[@2,@5,@1,@4,@3];

        NSArray *sorted = [arr2 sortedArrayUsingSelector:@selector(compare:)];

        NSLog(@"%@",sorted);

        

        NSArray *arr3 = @[@"two",@"three",@"five",@"one",@"four"];

        NSArray *sorted2 = [arr3 sortedArrayUsingSelector:@selector(compare:)];

        NSLog(@"%@",sorted2);

        

        //自定义对象的排序

        NSArray *arrayperson = @[[Person personWithName:@"Tom" andAge:25],

                                 [Person personWithName:@"Jobs" andAge:23],

                                 [Person personWithName:@"Bill" andAge:26],

                                 [Person personWithName:@"John" andAge:21]];

        //按姓名排序

        NSArray *sortedByName = [arrayperson sortedArrayUsingSelector:@selector(compareByName:)];

        NSLog(@"%@",sortedByName);

        

        

        //按年龄排序

        NSArray *sortedByAge = [arrayperson sortedArrayUsingSelector:@selector(compareByAge:)];

        NSLog(@"%@",sortedByAge);

        

        

        //NSArray和NSString

        NSString *str = @"this is a test";

        NSArray *words = [str componentsSeparatedByString:@" "];//分隔符

        NSLog(@"%@",words);

        

        NSString *str2 = [words componentsJoinedByString:@"-"]; //连接符

        NSLog(@"%@",str2);

    }

    return 0;

}

      测试结果如下所示:

2015-08-18 17:37:39.772 01-NSArray[1948:112551] (
    1,
    one,
    2,
    "<null>"
)
2015-08-18 17:37:39.795 01-NSArray[1948:112551] 1
2015-08-18 17:37:39.796 01-NSArray[1948:112551] one
2015-08-18 17:37:39.797 01-NSArray[1948:112551] 2
2015-08-18 17:37:39.797 01-NSArray[1948:112551] <null>
2015-08-18 17:37:39.798 01-NSArray[1948:112551] (null)
2015-08-18 17:37:39.798 01-NSArray[1948:112551] (
    1,
    2,
    3,
    4,
    5
)
2015-08-18 17:37:39.799 01-NSArray[1948:112551] (
    five,
    four,
    one,
    three,
    two
)
2015-08-18 17:37:39.799 01-NSArray[1948:112551] (
    "name=Bill,age=26",
    "name=Jobs,age=23",
    "name=John,age=21",
    "name=Tom,age=25"
)
2015-08-18 17:37:39.800 01-NSArray[1948:112551] (
    "name=John,age=21",
    "name=Jobs,age=23",
    "name=Tom,age=25",
    "name=Bill,age=26"
)
2015-08-18 17:37:39.800 01-NSArray[1948:112551] (
    this,
    is,
    a,
    test
)
2015-08-18 17:37:39.800 01-NSArray[1948:112551] this-is-a-test
Program ended with exit code: 0

 

posted @ 2015-08-18 17:38  XYQ全哥  阅读(283)  评论(0编辑  收藏  举报