Common operations for Objective-c:nsarray

Source: Internet
Author: User

The main operations of Nsarray immutable strings are: Create, enumerate, sort, and convert to NSString

Attention:

Nsarray can save objects, cannot save basic data type, struct, array, pointer, nil, NULL

Nsarray with nil as the end identifier.

Nsnull can represent an empty object.

The entire operation is represented by code:

    The declaration file for the. h Person class is as follows:  

#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; //Declaration Sort by name comparison

-(Nscomparisonresult) Comparebyage: (person *) Aperson; //Declaration Sort by age comparison

@end

   The implementation file for the. M person class is as follows:

#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 //Because the array stores the custom object Person object, the replication description method will nslog reformat the output

{

return [NSString stringwithformat:@ "Name=%@,age=%ld", _name,_age];

}

-(Nscomparisonresult) Comparebyname: (person *) Aperson //Returns a comparison of selector selector selection, string comparison defaults to nsorderasecding

{

return [_name Compare:aPerson.name];

}

-(Nscomparisonresult) Comparebyage: (person *) Aperson//with the same

{

if (_age > Aperson.age)

return nsordereddescending;

else if (_age < aperson.age)

return nsorderedascending;

Else

return nsorderedsame;

}

@end

    The main function tests are as follows:

#import <Foundation/Foundation.h>

#import "Person.h"

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

{

@autoreleasepool

{

Nsarray test

/*

Nsarray can save objects, cannot save basic data type, struct, array, pointer, nil, NULL

Nsarray with nil as the end identifier.

Nsnull can represent an empty object.

*/

Create an empty object

NSNull *nul = [NSNull null];

Objects of the same type are not required in an array and can be stored in any type of object

Nsarray *arr = @[@1,@ "One", @ "2", nul];

NSLog (@ "%@", arr);

Enumeration

Nsenumerator *enumerator = [arr objectenumerator];

ID obj;

while (obj = [enumerator nextobject])

{

NSLog (@ "%@", obj);

}

Save to file (must be OC built-in object, custom object will error)

[Arr writetofile:@ "/users/mac/desktop/arr.txt" atomically:yes];

Read file creation Nsarray

Nsarray *arr1 = [Nsarray arraywithcontentsoffile:@ "/users/mac/desktop/arr.txt"];

NSLog (@ "%@", arr1);

Sort

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

Nsarray *sorted = [arr2 sortedarrayusingselector: @selector (compare:)];

NSLog (@ "%@", sorted);

Nsarray *ARR3 = @[@ "One", @ "three", @ "five", @ "one", @ "four"];

Nsarray *sorted2 = [Arr3 sortedarrayusingselector: @selector (compare:)];

NSLog (@ "%@", sorted2);

Customizing the sorting of objects

Nsarray *arrayperson = @[[person personwithname:@ "Tom" andage:25],

[Person personwithname:@ "Jobs" andage:23],

[Person personwithname:@ "Bill" andage:26],

[Person personwithname:@ "John" andage:21]];

Sort by name

Nsarray *sortedbyname = [Arrayperson sortedarrayusingselector: @selector (comparebyname:)];

NSLog (@ "%@", sortedbyname);

Sort by age

Nsarray *sortedbyage = [Arrayperson sortedarrayusingselector: @selector (comparebyage:)];

NSLog (@ "%@", sortedbyage);

Nsarray and NSString

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

Nsarray *words = [str componentsseparatedbystring:@ ""];//delimiter

NSLog (@ "%@", words);

NSString *STR2 = [Words componentsjoinedbystring:@ "-"]; Connector characters

NSLog (@ "%@", str2);

}

return 0;

}

     The test results are as follows:

 -- ,- -  -:Panax Notoginseng:39.772  on-nsarray[1948:112551] (    1, one,2,    "<null>") -- ,- -  -:Panax Notoginseng:39.795  on-nsarray[1948:112551]1 -- ,- -  -:Panax Notoginseng:39.796  on-nsarray[1948:112551] One -- ,- -  -:Panax Notoginseng:39.797  on-nsarray[1948:112551]2 -- ,- -  -:Panax Notoginseng:39.797  on-nsarray[1948:112551] <NULL> -- ,- -  -:Panax Notoginseng:39.798  on-nsarray[1948:112551] (NULL) -- ,- -  -:Panax Notoginseng:39.798  on-nsarray[1948:112551] (    1,    2,    3,    4,    5) -- ,- -  -:Panax Notoginseng:39.799  on-nsarray[1948:112551] (five, four, one, three, both) -- ,- -  -:Panax Notoginseng:39.799  on-nsarray[1948:112551] (    "name=bill,age=26",    "name=jobs,age=23",    "name=john,age=21",    "name=tom,age=25") -- ,- -  -:Panax Notoginseng:39.800  on-nsarray[1948:112551] (    "name=john,age=21",    "name=jobs,age=23",    "name=tom,age=25",    "name=bill,age=26") -- ,- -  -:Panax Notoginseng:39.800  on-nsarray[1948:112551] (     This,     is, A, test) -- ,- -  -:Panax Notoginseng:39.800  on-nsarray[1948:112551] This- is-a-Testprogram ended with exit code:0

Common operations for Objective-c:nsarray

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.