Dark Horse programmer------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
Nsarray and Nsmutablearray
Nsarray represents an ordered, repeatable set of elements, with each element in the collection having its corresponding sequential index, Nsarray can only save the object and cannot hold the base type. Nsarray represents an immutable collection of collection elements, and once Nsarray is created successfully, the program cannot add new elements to the collection, cannot delete existing elements in the collection, and cannot replace the collection elements. Nsmutablearray is a subclass of Nsarray that represents a mutable collection of collection elements, so you can add, remove, and replace elements to the collection
void use () {///OC array cannot hold nil value//OC array can only hold OC object, cannot hold non-OC object type, such as int, struct, enum, etc.//This array is always an empty array Nsarray *array = [Nsarray array]; /* 1.NSArray created */nsarray *array2 = [Nsarray arraywithobject:@ "Jack"]; Nil is the tag of the end of the array element nsarray *array3 = [Nsarray arraywithobjects:@ "Jack", @ "Rose", nil Nil]; [Array2 Count]; Nsarray *array4 = [Nsarray arraywithobjects:@ "Jack", @ "Rose", @ "4324324", nil]; Quickly create a Nsarray object Nsarray *array4 = @[@ "Jack" @ "Rose" @ "4324324"]; Nsmutablearray *arr = @[@ "Jack" @ "Rose"]; /* Number of elements in 2.NSArray */NSLog (@ "%ld", Array3.count); /* 3.NSArray access to elements */NSLog (@ "%@", [Array3 objectatindex:1]); ARRAY3[1]; NSLog (@ "%@", array3[0]); }//traversal array void Use2 () {Person *p = [[Person alloc] init]; Nsarray *array = @[p, @ "Rose", @ "Jack"]; Iterating over arrays, customizingMethod//for (int i = 0; i<array.count; i++)//{//NSLog (@ "%@", Array[i]); }//id obj represents each element in an array//int i = 0; for (ID obj in array)//{////Find the position of the obj element in the array//Nsuinteger i = [array indexofobje Ct:obj]; NSLog (@ "%ld-%@", I, obj); i++; if (i==1)//{//break; }////The way the system iterates through an array [array Enumerateobjectsusingblock://each traversal to an element will be called once Bloc K//and the current element and index position are passed as parameters to block ^ (ID obj, Nsuinteger idx, Boolbool *stop) {NSLog (@ "%ld-%@", IDX , obj); if (idx = = 0) {//stop traversing *stop = YES; } }]; }//numutablearray uses void Use3 () {Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "Rose", @ "Jim", Ni L Nil]; @[] Create only immutable groups Nsarray/* ERROR Nsmutablearray *array = @[@ "Jack" @ "Rose"]; [Array addobject:@ "Jim"]; *//add element [array Addobject:[[person alloc] init]; [Array addobject:@ "Jack"]; Delete element//[array removeallobjects]; Deletes the specified object//[array removeobject:@ "Jack"]; [Array removeobjectatindex:0]; Error notation//[array addobject:nil]; NSLog (@ "%@", array); NSLog (@ "%ld", Array.count); }
The
nsstring and nsmutablestring
NSString represent immutable strings of character sequences, while nsmutablestring represent strings that are variable in character sequences. Nsmutablestring is a subclass of NSString. The NSString generally includes the following functions.
1, create string: You can use an instance method beginning with Init, you can also use a class method beginning with string, or you can give a string constant in the form of @ ""
2, read the network URL or file initialization string
3, write the string contents to a file or URL
4, gets the string length, including the number of characters and the number of bytes
5, joins, separates, replaces, compares strings
Create string void Stringcreate () {/* 1. String creation */NSString *S1 = @ "Jack"; NSString *s2 = [[NSString alloc] initwithstring:@ "Jack"]; NSString *S3 = [[NSString alloc] initwithformat:@ "Age is%d", 10]; C String--and OC string NSString *s4 = [[NSString alloc] initwithutf8string: "Jack"]; OC string--C string const Charchar *CS = [S4 utf8string]; Read the file creation string, nsutf8stringencoding use the Chinese can use this encoding nsstring *S5 = [[NSString alloc] initwithcontentsoffile:@ "/users/apple /desktop/1.txt "Encoding:nsutf8stringencoding Error:nil]; URL: Resource Path//protocol header://path//file:////ftp:////http://weibo.com/a.png//HTTP://WW w.baidu.com//Nsurl *url = [[Nsurl alloc] initwithstring:@ "File:///Users/apple/Desktop/1.txt"]; Nsurl *url = [Nsurl fileurlwithpath:@ "/users/apple/desktop/1.txt"]; NSString *S6 = [[NSString alloc] Initwithcontentsofurl:url Encoding:nsutf8StringEncoding Error:nil]; NSLog (@ "s6=\n%@", S6); /* There is usually a class method paired with the object method [Nsurl urlwithstring:<# (NSString *) #>]; [NSString stringwithformat:@ ""]; [NSString stringwithcontentsoffile:<# (NSString *) #> encoding:<# (nsstringencoding) #> error:<# ( Nserror *__autoreleasing *) #>]; */} void Stringexport () {///string Export [@ ' jack\njack ' writetofile:@ '/users/apple/desktop/my.txt ' atomically : YES encoding:nsutf8stringencoding Error:nil]; NSString *str = @ "4234234"; Nsurl *url = [Nsurl fileurlwithpath:@ "/users/apple/desktop/my2.txt"]; [Str writetourl:url atomically:yes encoding:nsutf8stringencoding Error:nil]; } int main () {nsmutablestring *s1 = [nsmutablestring stringwithformat:@ "My age is 10"]; Stitching the contents to the back of the S1 [S1 appendstring:@ "11 12"]; Get the is scope nsrange range = [S1 rangeofstring:@ "is"]; [S1 Deletecharactersinrange:range]; NssTring *S2 = [NSString stringwithformat:@ "Age is 10"]; NSString *S3 = [S2 stringbyappendingstring:@ "11 12"]; NSLog (@ "s1=%@, s2=%@", S1, S2); return 0; }
27-Dark Horse programmer------OC Language Learning Note---Foundation02