OBJECTIVE-C (Foundation framework an array (Nsarray))

Source: Internet
Author: User



OC Array



Nsarray is a static array, that is, it points to the content is immutable, it points to a memory area, once initialized, it can not be used to modify the memory area of the data, but it is able to read the data.



Nsmutablearray is a subclass of Nsarray that can make changes to the contents of the memory area that it points to, and can increase the array contents



The subscript for the first data for Nsarray and Nsmutablearray is 0.






1, nsarray non-variable group



[Array1 Count]: The length of the array.


// Create OC array object
     NSArray * array = [NSArray arrayWithObject: @ "jack"];

     // nil is the end of the array element
     NSArray * array1 = [NSArray arrayWithObjects: @ "jack", @ "haha", nil];
     NSLog (@ "% ld", array1.count); // print the length of the array


[Array1 objectatindex:0]: Accessing elements in an array



ARRAY1[0];


NSArray * array1 = [NSArray arrayWithObjects: @ "jack", @ "haha", nil];
     // access the output array element
     NSLog (@ "% @", [array1 objectAtIndex: 1]);
     NSLog (@ "% @", array1 [0]);





To create an OC array object quickly:



[Nsarray arraywithobjects;@ "Jack" @ "haha" ..., nil]: Initialize the assignment to the array object. You can write pointers to arbitrary objects, and you must use nil at the end.


     // Quickly create NSArray array object
      NSArray * array = @ [@ "jack", @ "rose‘, @ "haha"];
NSLog (@ "% @", array1); 





Fast traversal of nsarray arrays


NSArray * array = @ [@ "jack", @ "haha"];
    
     // id obj represents every element in the array
     for (id obj in array) {
        
         NSLog (@ "% @", obj);
     }





Find the position of an element in an array


NSArray * array = @ [@ "jack", @ "haha"];
    
     // id obj represents every element in the array
     for (id obj in array) {
         // find the position of the obj element in the array
         NSUInteger i = [array indexOfObject: obj];
        
         NSLog (@ "% ld-% @", i, obj);
     }





Use block


NSArray * array = @ [@ "jack", @ "haha"];
     
     // Every time an element is traversed, block is called once
     // And the current element and index position are passed to the block as a reference
     [array enumerateObjectsUsingBlock: ^ (id obj, NSUInteger idx, BOOL * stop) {
         NSLog (@ "---------");
     }];
/ *
Bool * stop
If (idx == 0) {
Stop traversing
* Stop = YES;
}
* / 





OBJECTIVE-C (Foundation framework an array (Nsarray))


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.