Main.m#import <Foundation/Foundation.h> #import "Person.h"/* <span style= "White-space:pre" ></ Span>1 creates a collection of 2 by constructing a NSArray3 array of objects that can store different types of object 4 arrays are actually stored in the address of an object, and can also store an array of address 5 stores a custom object 6 array stored in the base data type 7 shortcut for creating an array of 8 Quickly get an element in an array 9 fetch data from the collection 10 get the number of elements in an array 11 determine if an object exists in the array */int main (int argc, const char * argv[]) {@autoreleasepool { 1 Create collection Nsarray * array = [[Nsarray alloc] initwithobjects:@ "One", @ "one", @ "three", nil]; NSLog (@ "Array%@", array); Nsarray * array1 = @[@ "One", @ "one", @ "three"]; NSLog (@ "Array1%@", array1); int float char double bool int i = 10; NSNumber * Number = [[NSNumber alloc] initwithint:i]; 3 arrays can store different types of objects Nsarray * array2 = @[@ "One", @ "one", number]; NSLog (@ "Array2%@", array2); 4 An array of objects that are actually stored in an array of addresses can also be stored in an address Nsarray * a1 = @[@ "one", @ "one", @ "three"]; Nsarray * A2 = @[@ "1", @ "2", @ "3"]; Nsarray * a3 = @[a1,a2]; NSLog (@ "A3%@", A3); NSString * str = nil; The last element in this method must be nil if storing nil elements in an array can easily result in data loss Nsarray * array3 = [[Nsarray alloc] initwithobjects:@ "One", str,a3,@ "thr EE ", nil); NSLog (@ "%@", array3); Create three person objects in the array in the person * p1 = [[Person alloc] initwithname:@ "Jack" andage:15]; Person * P2 = [[Person alloc] initwithname:@ "Tom" andage:18]; person * P3 = [[Person alloc] initwithname:@ "Jim" andage:20]; Nsarray * Array4 = [[Nsarray alloc] initwithobjects:p1,p2,p3, nil]; NSLog (@ "%@", Array4); 6 The base data type is stored in the array//+ (NSNumber *) Numberwithchar: (char) value;//+ (NSNumber *) Numberwithint: (int) value;// + (NSNumber *) Numberwithfloat: (float) value;//+ (NSNumber *) Numberwithdouble: (double) value;//+ (NSNumber *) Numberwithbool: (BOOL) value;//+ (NSNumber *) Numberwithinteger: (Nsinteger) value//Use NSNumber to wrap the basic data type and deposit Array NSNumber * number1 = [NSNumber numberwithfloat:8.9]; NSNumber * number2 = [NSNumber numberwithint:8]; NSNumber * Number3 = [NSNumber numberwithdouble:8.9345]; NSNumber * Number4 = [NSNumber numberwithchar: ' C ']; Nsarray * Array5 = [[Nsarray alloc] initwithobjects:number1,number2,number3,number4, nil]; The char type stores the ASCII code value NSLOG (@ "%@", array5); char c = [[Array5 objectatindex:3] charvalue]; NSLog (@ "%c", c); The array takes the element person * P4 = [Array4 objectatindex:1]; NSLog (@ "%@", p4); Quick way to take elements person * P5 = array[1]; } return 0;
#import <Foundation/Foundation.h> @interface person:nsobject{ nsstring * _name; int _age;} -(ID) Initwithname: (NSString *) name andage: (int) age;-(void) SetName: (NSString *) name;-(NSString *) name;-(void) setage :(int) age;-(int) age; @end
#import "Person.h" @implementation person-(ID) initwithname: (NSString *) name andage: (int) Age { if (self = [super init ]) { _name = name; _age = age; } return self;} -(void) SetName: (NSString *) name { _name = name;} -(NSString *) name { return _name;} -(void) Setage: (int) Age { _age = age;} -(int) Age { return _age;} -(NSString *) Description { return [nsstring stringwithformat:@ "name =%@, age =%d", _name,_age];}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Application creation, storage, customization, quick access to arrays