I. array in oc: NSArray definition: NSArray * arrayName = [NSArray arrayWithObjects: obj1, obj2, obj3, obj4, nil]; // The array uses nil null as the end flag: for (int I = 0; I <4; I ++) {Object * obj = [arrayName objectAtIndex: I]; [obj functionName];} example: // use the array // define four Student variables and initialize Student * stu1 = [StudentstudentWithName: @ "Tang Seng" andSex: YESandAge: 23 andAddress: @ "Tang"]; student * stu2 = [StudentstudentWithName: @ "Wukong" andSex: YESandAge: 23 andAddress: @ "Huaguo Shan"]; Student * stu3 = [StudentstudentWithName: @ "ba Jie" andSex: YESandAge: 23 andAddress: @ "Gao Lao Zhuang"]; Student * stu4 = [StudentstudentWithName: @ "Sha Seng" andSex: YESandAge: 23 andAddress: @ "liusha River"]; // array definition and initialization (the array is ended with an nil null) NSArray * students = [NSArrayarrayWithObjects: stu1, stu2, stu3, stu4, nil]; // defines an array named students. All element types are 4 Student elements. // use the for loop to use the element for (int I = 0; I <4; I ++) {// retrieves the corresponding object Student * stu = [students objectAtIndex: I] based on the index of the array; // calls to retrieve The sayHi method of the object [stusayHi];} // the elements in the array can be multiple types. // The array elements cannot store basic data types (such as int float ..) // if the basic data type is stored, convert it to an NSNumber object, for example, NSArray * arrays = [NSArrayarrayWithObjects: stu1, @ "Zhang San", [NSDate date]. [NSNumbernumberWithInt: 20], nil]; // defines an array named array, which contains four elements, student, String, Date, and Number respectively. // Note: When printing a reference object, the default type is the print class type and memory address. the description method (equivalent to the toString method in java) can be rewritten in the m file, and its output format can be changed in. description method after rewriting in the m file/** in. descripti in the m file On method [not in. define the interface in the H file] // override the description method is equivalent to the toString method in java-(NSString *) description // After rewriting the description method, let the output name (or other custom strings) {returnname;} * // use for containing arrays of different types (int I = 0; I <4; I ++) {// retrieves the corresponding object id arry = [arraysobjectAtIndex: I] based on the array index; // If id is generic, an object of any type defines id. If a generic object is not prefixed with a variable, * // The NSLog type is determined only when the value is assigned. (@ "% @ ", arry);} NSLog (@ "for-in usage"); // the usage of for-in is the same as that in java, the number of cycles depends on the number of array elements for (id tempin arrays) {NSLog (@ "% @ ", Temp);} // NSArray subclass NSMutableArray can be added, deleted, modified, and queried as a variable array // NSMutableArray * mArray = [[NSMutableArray alloc] init]; NSMutableArray * mArray = [NSMutableArrayarrayWithObjects: stu1, @ "aaa", @ "abc", nil]; // NSMutableArray is a variable array and a subclass of NSArray, you can add, delete, modify, and query NSLog (@ "array length % ld", [mArray count]); // call the count method to output the length of the array (number of elements, use a long integer dashboard operator // Add the element [mArrayaddObject: @ "cccc"] to the array; [mArray addObject: [NSNumbernumberWithBool: YES]; NSLog (@ "Added array element"); NSLog (@ "% @", mArray); // deletes the element [mArray removeObjectAtIndex: 0] in the array. // Delete the element whose subscript is 0, that is, the first element NSLog (@ "the array element after the first element is deleted"); NSLog (@ "% @", mArray ); // Note: Press and hold the windows key and the left mouse button to view the class. h file, so that we can better understand the methods in this class and better use the extension method in this class // NSMutableArray //-(void) addObject :( id) anObject; //-(void) insertObject :( id) anObject atIndex :( NSUInteger) index; //-(void) removeLastObject; //-(void) removeObjectAtIndex :( NSUInteger) index; //-(vo Id) replaceObjectAtIndex :( NSUInteger) index withObject :( id) anObject; 2. Only elements of the object class can be stored in the collection. To store elements of the basic data type, you must convert it to an object class before storing it, the data elements in the collection can be Set sets of different types and arrays. // Set set Set // definition and initialization NSSet * Set = [NSSetsetWithObjects: @ "aa ", @ "bb", @ "cc", nil]; // The elements in the set are unordered and the unique mutually independent lengths are immutable. // NSLog (@ "% @", set) is used for the set ); NSLog (@ "% @", [setanyObject]); // retrieve all the elements in the set. // NSMutableSet, a subclass of the Set, is similar to an Array subclass with a variable length. You can also add and delete elements. // press and hold the windows key and the left mouse button to view the elements. Define the method to use. 3. The string Cocoa has prepared a struct related to string operations: the struct represents the range of related transactions, including two members, and one is a long integer location, it indicates the starting position, and the other is the length of a long integer. For example, to get a range of 10 and 3 starting positions, it can be created in three ways: the first type of callback function can be written as follows: nsange range = NSMakeRange (10, 3); the second type of callback directly assigns the value to the member: NSMakeRange range; range. location = 10; range. length = 3; Explain the third method is to apply the aggregation structure Assignment Mechanism of C language: nsange range = {10, 3}; use of string: (as follows) int main (int argc, const char * argv []) {// 1. Create a string // first, directly create NSString * str = @ "zhangsan "; // Create an empty string NSString * str1 = [[NSStringalloc] init]; NSLog (@" str = % @, str1 = % @ ", str, str1 ); // The dependency to create str2 is equivalent to the str pointer NSString * str2 = [NSStringstringWithString: str]; // After str2 depends on str, after str is released, str2 blank NSLog (@ "str2 = % @", str2); // the fourth key NSString * str3 = [NSStringstringWithFormat: @ "% @ love iPhone % d5.", str, 5]; NSLog (@ "% @", str3); // 2. NSLog (@ "str3 length: % ld", [str3 length]); // 3. Comparison of strings NSString * str4 = [NSStringstringWithForm At: @ "zhangsan"]; if (str = str4) // same as in java, when = is used to determine whether two strings are equal, it determines whether the addresses are equal, instead of determining whether the content is equal {NSLog (@ "= Equal");} if ([strisjavastostring: str4]) {// similar to that in java, determine whether the content of the two strings is equal, use the isEqualToString function NSLog (@ "isEqualToString equal");} // 4. compare the string size // use the compare method to return the enumerated types [-, 1], NSOrderedAscending (1) in ascending order. The former is greater than the latter, and the NSOrderedSame (0) is equal. The former is equal to the latter and NSOrderedDescending (-1) the former is smaller than the latter. // compare are compared from the first letter. The comparison is the ASC code NSString * str5 = @ "abc"; s. Witch ([str4 compare: str5]) {caseNSOrderedAscending: NSLog (@ "greater than"); break; caseNSOrderedSame: NSLog (@ "equal to"); break; caseNSOrderedDescending: NSLog (@ "less than"); break; default: break ;} // 5. Determine the prefix and suffix // use the hasSuffix function to return the bool type NSString * str6 = @ "IOS.doc"; if ([str6hasSuffix :@". doc "]) {// stringstr6records is the NSLog ending with .doc (@" this is a Word document ");} // The prefix is used to determine the return bool type NSString * str7 = @" http://www.baidu.com "with the hasPrefix function "; if ([str 7 hasPrefix: @ "http: //"]) {// judge whether str7 starts with "http: //" NSLog (@ "this is a URL ");} // 6. Find the sub-string rangeOfString function // nsange is a struct. when defining a variable, you do not need to add * // nsange before the variable to contain two members: location and length; localtion indicates the position of the string (generally starting from 0), and length indicates the length of the string. It indicates the range of the string: [str7 rangeOfString: @ "www"]; // define a struct variable and assign it an initial value if (range. location! = NSNotFound) // locate the position of the substring in the parent string range. location {NSLog (@ "location: % ld, length: % ld", range. location, range. length);} else {NSLog (@ "no string found");} // 7. Capture the sub-string NSLog (@ "% @", [str7substringFromIndex: 7]); // NSLog (@ "% @", [str7substringToIndex: 7]) from the position 7 to the end (including the seventh position); // NSLog (@ "% @", [str7substringWithRange: range]) from the beginning to the seventh position (excluding the seventh position); // extract the string from the range. // 8. Convert the string to NSString * str8 = @ "123"; int num = [str8intValue]; // convert the string str8 Int basic type NSLog (@ "% d", num); NSLog (@ "% @", [str7 uppercaseString]); // convert to an NSLog (@ "% @", [str7 lowercaseString]); // convert to a lower-case NSLog (@ "% @", [str7 capitalizedString]); // replace uppercase letters/9, strings // 1, stringByReplacingOccurrencesOfString (replace all) NSString * str9 = @ "2B youth aa, 2B youth dd, 2B youth ff, 2B youth xx "; // Replace" 2B "in str9 with" Literature "NSLog (@" % @ ", [str9 stringByReplacingOccurrencesOfString: @ "2B" withString: @ ""]); // 2. stringByReplacingCharac The tersInRange method (replaced within a certain range) NSLog (@ "% @", [str9 stringByReplacingCharactersInRange: NSMakeRange () withString: @ ""]); // 0 ~ 2. The length of the substring to be replaced by the target string may be different // 10. The string is cut and an array NSArray * arr = [str9 componentsSeparatedByString: @ ","] is returned. // cut the string with "," as the bounds, and put the cut string into the NSLog (@ "% @", arr) in the arr array ); // The new character after string splicing does not change the content of the original string but generates a new string // NSString * str8 = @ "123 "; // first, concatenate the NSLog (@ "% @", [str8stringByAppendingString: @ "abc"]) directly after the original string. // second, concatenate NSLog (@ "% @", [str8stringByAppendingFormat: @ "iPhone % d5", 5]) using string format characters; // 12, variable string NSMutableString NSLog (@ "variable string"); // NSMutableString is a subclass of NString and inherits all NSMutableString * mstr = [[NSMutableString alloc] init]; // define an empty string NSLog (@ "% @", mstr ); // output // 1. Different from NSString, The NSMutableString is spliced on the basis of the original string. No new string is generated, instead, the style of the original string is changed [mstr appendString: @ "iphone5"]; // NSLog (@ "% @", mstr) in normal splicing mode; // The output [mstr appendFormat: @ "ipad % d", 4]; // format: NSLog (@ "% @", mstr ); // output // 2. Delete the character in the string to delete a range of nsange rang = [mst R rangeOfString: @ "ipad"]; // find the range of "ipad" if (rang. location! = NSNotFound) {[mstr deleteCharactersInRange: rang];} else {NSLog (@ "no string found. deletion failed. ");} NSLog (@" % @ ", mstr); return 0;} 4. Exercise int main (int argc, const char * argv []) after class {// Question 1: Replace "World" of "Hello World and Sunshine" with "iBokan" in two ways: NSString * str = @ "Hello World and Sunshine "; NSLog (@ "% @", str); NSLog (@ "% @", [str stringByReplacingOccurrencesOfString: @ "World" withString: @ "iBokan"]); NSLog (@ "% @", str); NSLog (@ "% @", [str stringByReplacingCharactersInRange: NSMakeRange (6, 5) withString: @ "iBokan"]); // Question 2: calculate the difference between the string "158" and "39" in decimal values and output the NSString * num1 = @ "158" in the form of a string "; NSString * num2 = @ "39"; int n1 = [num1 intValue]; int n2 = [num2 intValue]; int result = n1-n2; NSString * jieguo = [NSString stringWithFormat: @ "% d", result]; NSLog (@ "% @", jieguo); // The second question can be abbreviated: NSLog (@ "% d-% d = % @", 158,39, [NSString stringWithFormat: @ "% d ", [@ "158" intValue]-[@ "39" intValue]); return 0 ;}