Recently busy with the company's project research and Development learning iOS progress is slow, but still insisted that they learn the foundation of basic learning grammar to share with you to learn. Every day to night in the rest of tomorrow to work physically tired, but I always believe that sometime. This describes the mutable array, immutable lookup of an element in an array, the last element, and the elements you set up, as well as the traversal of an array.
Main.m
Arrary
//
Created by Zhang in 14/11/21.
Copyright (c) 2014 Zyh. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
Create an array
void Arraycreate () {
Nsarray *array = [Nsarray array];
Array = [Nsarray arraywithobject:@ "123"];
Array = [Nsarray arraywithobjects:@ "a", @ "B", @ "C", nil];
unsigned int count= [array count];
NSLog (@ "%zi", count);
}
Simple and practical #pragma maker array
void Arrayuse () {
NSObject *obj = [[NSObject alloc] init];
Nsarray *array1 = [Nsarray arraywithobjects:@ "a", @ "B", @ "C", obj, nil];
if ([Array1 containsobject:@ "a"]) {
NSLog (@ "OK");
}
NSString *last = [Array1 lastobject];
NSLog (@ "%@", last);
NSString *str = [Array1 objectatindex:1];
NSLog (@ "%@", str);
unsigned int index = [array1 indexofobject:@ "B"];
NSLog (@ "%zi", index);
}
void Arraymemory () {
Student *STU1 = [[Student alloc] init];
Student *stu2= [[Student alloc] init];
Student *STU3 = [[Student alloc] init];
Nsarray *array = [[Nsarray alloc] initwithobjects:stu1,stu2,stu3, nil];
NSLog (@ "%zi", Array.count);
}
void Arraymessage () {
Student *STU1 = [Student Student];
Student *STU2 = [Student Student];
Student *STU3 = [Student Student];
Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Nil];
[Array makeobjectsperformselector: @selector (test)];
}
void Arrayfor () {
Nsarray *array = [Nsarray arraywithobjects:@ "1", @ "2", @ "2", nil];
For loop traversal
int count =array.count;
for (int i = 0; i<count; i++) {
ID obj = [array objectatindex:i];
NSLog (@ "%i---%@", i,obj);
// }
Fast traversal
int i=0;
for (ID obj in array) {
NSLog (@ "%i---%@", i,obj);
i++;
// }
#pragma-maker Block Learning
[Array Enumerateobjectsusingblock:
^ (ID obj, Nsuinteger idx, BOOL *stop) {
NSLog (@ "%i---%@", idx,obj);
// }];
//
Use of iterators
Positive sort
Nsenumerator *enu = [array objectenumerator];
Reverse Sort
Nsenumerator *enu1 = [array reverseobjectenumerator];
ID obj = nil;
while (obj = [enu1 nextobject]) {
NSLog (@ "%@", obj);
}
}
int main (int argc, const char * argv[]) {
@autoreleasepool {
Arraycreate ();
Arrayuse ();
Arraymessage ();
Arrayfor ();
}
return 0;
}
Foundation Array Nsarray Learning