Objective-C:NSArray的常見操作

來源:互聯網
上載者:User

標籤:

NSArray不可變字串的主要操作有:建立、枚舉、排序、與NSString之間的相互轉換

注意:

        NSArray可以存對象,不可以存基礎資料型別 (Elementary Data Type)、結構體、數組、指標、nil、NULL

        NSArray用nil作為結束標識符。

        NSNull可以代表一個Null 物件。

整個操作還是通過代碼來體現吧:

 

    .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可以存對象,不可以存基礎資料型別 (Elementary Data Type)、結構體、數組、指標、nil、NULL

        NSArray用nil作為結束標識符。

        NSNull可以代表一個Null 物件。

        */

        //建立一個Null 物件

        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] 12015-08-18 17:37:39.796 01-NSArray[1948:112551] one2015-08-18 17:37:39.797 01-NSArray[1948:112551] 22015-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-testProgram ended with exit code: 0

 

Objective-C:NSArray的常見操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.