Nsarray in iOS

Source: Internet
Author: User

//

#import <Foundation/Foundation.h>

#import "Animal.h"

int main (int argc, const char * argv[]) {
@autoreleasepool {
1. Create a method
2. Sort
3. Take elements
4. Connect, separate
5. Strong references
6. For...in

Animal *ani = [[Animal alloc] init];
Ani.age = 12;



Create an OC immutable group with three elements
Elements of an immutable array can no longer be changed
Nsarray *arr1 = @[@12, @ "", ANI];
NSLog (@ "%lu", Arr1.count);
NSLog (@ "%@", arr1);

Not modify array
Ani.age = 20;

Animal *ani10 = [[Animal alloc] init];
Ani10.age = 5;

Animal *ani11 = [[Animal alloc] init];
Ani11.age = 48;

Nsarray *arr1 = @[ani, ani10, ani11];

NSLog (@ "%@", arr1);

Sort, just to implement the method of comparison between 22
SEL
Nsarray *arr10 = [arr1 sortedarrayusingselector: @selector (mycompare:)];
NSLog (@ "%@", arr10);

Execute print in sequence
[Arr10 makeobjectsperformselector: @selector (print)];
Can pass a parameter
[Arr10 makeobjectsperformselector: @selector (call:) withobject:@ "Zhangsan"];

Creates a new array, and the argument as the last element
Nsarray *arr30 = [arr10 arraybyaddingobject:@ "AA"];
NSLog (@ "%p,%p", Arr10, Arr30);

NSString *str10 = @ "A.B.C";
Split string
Nsarray *arr40 = [Str10 componentsseparatedbystring:@ ".];
NSLog (@ "%@", Arr40);

Connection string
NSString *str11 = [Arr40 componentsjoinedbystring:@ "<+>"];
NSLog (@ "%@", Str11);

Nsarray *ARR20 = @[@123, @ "234", @345];
[Arr20 writetofile:@ "/users/apple/desktop/test.plist" atomically:yes];
//
Nsarray *arr21 = [Nsarray arraywithcontentsoffile:@ "/users/apple/desktop/test"];
NSLog (@ "%@", Arr21);


return 0;

NSNumber *num1 = arr1[0];
NSLog (@ "%@", NUM1);

Animal *ani2 = [[Animal alloc] init];
Ani2.age = 20;
NSNumber *num2 = @34;

Check if an object is included
if ([arr1 containsobject:num2]) {
NSLog (@ "in arr1");

Get the location of an object
Nsuinteger index = [arr1 indexofobject:ani2];
NSLog (@ "%lu", index);
if (Nsnotfound = = index) {
NSLog (@ "no Zhaodao");
}
}
else {
NSLog (@ "not");
}

return 0;

NSLog (@ "%lu", Arr1.count);

ani = nil;

Nil means end of argument
Cannot fit nil object in array
ARR1 = [[Nsarray alloc] initwithobjects:@90, ANI, @80, nil];
NSLog (@ "%@", arr1);

ID Carr[4];
Carr[0] = @ "abc";
CARR[1] = @ "EFG";
CARR[2] = ANI;
CARR[3] = @ "xxx";

ARR1 = [[Nsarray alloc] Initwithobjects:carr Count:4];
NSLog (@ "%@", arr1);

Creates an array and initializes it with the elements in arr1.
In fact, the array contains a pointer to the object, and the array produces a strong reference to the object it is loaded in.
Nsarray *ARR2 = [Nsarray arraywitharray:arr1];
NSLog (@ "%lu", Arr2.count);

Get Objects by location
NSLog (@ "%@", arr2[1]);
NSLog (@ "%@", [arr2 objectatindex:arr2.count-1]);

Gets the No. 0 element
NSLog (@ "%@", [arr2 Firstobject]);
Gets the last element
NSLog (@ "%@", [arr2 Lastobject]);

NSLog (@ "------");
for (int i = 0; i < Arr2.count; ++i) {
NSLog (@ "%@", Arr2[i]);
}

Two types
1. Compile-time type, type of pointer
2. Run-time type, type of object
NSLog (@ "+++++");

ARR2 = @[@12, ANI, @34];
Quick Enumeration
for (id obj in arr2) {
Checks if an object is of type NSNumber
if ([obj Iskindofclass:[nsnumber class]]) {
NSNumber *num = (nsnumber *) obj;
NSLog (@ "%d", num.intvalue);
}
else if ([obj Iskindofclass:[animal class]]) {
Animal *animal = (Animal *) obj;
NSLog (@ "Age:%d", animal.age);
}

Obj.age = 20;
}

ani = nil;
arr1 = nil;
ARR2 = nil;

NSLog (@ "xxxxxxx");
}

NSLog (@ "-----");
return 0;
}

Nsarray in iOS

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.