OBJECTIVE-C Foundation Framework Practice--nsarray (i) __nsarray

Source: Internet
Author: User
Tags array length

Nsarray is an immutable group in the Foundation framework, let's implement these methods:

#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) {@autoreleasepool {//oc array can be Storing objects of different types; the array of OC can only store objects;//But memory space is still contiguous; Nsarray *arr1 = [[Nsarray alloc] initwithobjects:@ "1", @ "2", @ "3", nil];/
    
    /array finally must end with nil, indicating the end of the array; NSLog (@ "%@", arr1);
    Computes the array length; int count = (int) arr1.count;
    
    NSLog (@ "Count =%d", count);
    Detects whether there is a value in the array; BOOL iscontainer = [arr1 containsobject:@ "5"];
    if (Iscontainer) {NSLog (@ "exists this object");
    }else{NSLog (@ "There is no such object");
    //Get the last object; NSString *lastobj = [arr1 lastobject];
    
    NSLog (@ "lastobj =%@", lastobj);
    Get the first object; NSString *firstobj = [arr1 firstobject];
    
    NSLog (@ "firstobj =%@", firstobj);
    Gets the object at the specified position; nsstring *indexobj = [arr1 objectatindex:2];
    
    NSLog (@ "indexobj =%@", indexobj);
    Gets the location of a specified object;//If the element does not exist, print-1; int index = (int) [arr1 indexofobject:@ "2"];
    
  NSLog (@ "index =%d", index); } RETurn 0; }

The output results are as follows:

.


GitHub home: https://github.com/chenyufeng1991. You are welcome to visit.

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.