Summary of the NSString method in OC language (all)

Source: Internet
Author: User

Summary of the NSString method in OC language (all)

// Value of Arrays: (1) storing multiple elements (2) storage elements can only be objects and objects can be of different types. (3) array is an Ordered Set * (4) elements in the array can be repeated and repeated objects will become array elements. // 1 create an array object // (1) Use the constructor // nil as the ending sign for storing array elements. elements after nil are encountered are not put into the element; NSArray * arr1 = [NSArray arrayWithObjects: @ "5", @ "bb", @ "cc", @ "dd ", @ "ee", @ "ff", nil]; NSLog (@ "% @", arr1); NSArray * arr2 = [[NSArray alloc] initWithObjects: @ "5 ", @ "bb", @ "cc", @ "dd", @ "ee", @ "ff", @ "dd", @ "5", @ "bb ", @ "cc", @ "dd", @ "ee", @ "ff", @ "dd", nil]; NSLog (@ "% @", arr2 ); // (2) use the initialization method // 2. obtain the number of elements NSUInteger count = [arr1 count]; NSLog (@ "% lu", count); // 3. obtain the object based on the index value // (1) obtain the element NSString * str = [arr2 objectAtIndex: 0]; NSLog (@ "% @", str ); // (2) obtain the first element in the element // The firstObject and objectAtIndex: 0 Methods: Fortunately, when the array element is empty, nil is returned, while objectAtIndex: 0 will cause the program crash. index 0 beyond bound of empty array. array subscript out of bounds. NSString * str2 = [arr2 firstObject]; NSString * str3 = [arr2 lastObject]; NSLog (@ "% @", str2); NSLog (@ "% @", str3 ); // (3) obtain the second element in the array // 4. obtain the index value of the object in the array NSUInteger index = [arr2 indexOfObject: @ "dd"]; NSLog (@ "% lu", index); // 5. determines whether an object BOOL isExist = [arr2 containsObject: @ "dd"]; NSLog (@ "% d", isExist); // 6. array sorting (Children's edition) NSArray * sortArr = [arr2 sortedArrayUsingSelector: @ selector (compare :)]; NSLog (@ "% @", sortArr); // 7. fast traversal (quick enumeration) // for (int I = 0; I <[arr2 count]; I ++) {// NSString * str = [arr2 objectAtIndex: I]; /// NSLog (@ "% @", str); //}/*** forin fast enumeration (fast traversal) is used to traverse the elements in the set, type * object // The type of the element in the type array. // name of an object (set by yourself) collection: collection (that is, a large container, array, Dictionary, or set) // * // for (NSString * str in arr2) {// NSLog (@ "% @", str );//}

Related Article

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.