Object-C, array NSArray

Source: Internet
Author: User

Object-C, array NSArray

Back in the evening, I wrote two iOS applications.

Displays tags on the interface. One is to manually construct the interface and then bind the event. Another method is to use the built-in interface as the container, but manually add other interface elements to it.

 

The point in the book is that it is better to use a graphical interface to construct the interface.

 

Then, an example of Object-C array is written.

 

Object-C is relatively simple. It is output on the black screen console, while there are many programs and codes on the iOS visual interface, which is hard to describe.

The "context" of iOS programs is more complicated, and the Object-C language is similar to Java.

 

 

/// Main. m // NSArrayTest /// Created by fansunion on 15/12/1. // Copyright (c) 2015 demo. All rights reserved. // # import
 
  
// Demonstrate immutable array int main (int argc, const char * argv []) {@ autoreleasepool {// immutable array, use the class method to construct the array NSArray * array = [NSArray arrayWithObjects: @ "A", @ "B", @ "C", nil]; // There are two access elements: NSLog (@ "The first element is % @", array [0]); NSLog (@ "The second element is % @", [array objectAtIndex: 1]); // immutable array, adds an element D to the original array NSArray * newArray = [array arrayByAddingObject: @ "D"]; // use the for loop to print the new array for (int index = 0; index
  
   

 

Program output

 

21:16:55. 768 NSArrayTest [5346: 358824] The first element is

21:16:55. 769 NSArrayTest [5346: 358824] The second element is B

21:16:55. 769 NSArrayTest [5346: 358824] The 0 element is

21:16:55. 769 NSArrayTest [5346: 358824] The 1 element is B

21:16:55. 770 NSArrayTest [5346: 358824] The 2 element is C

21:16:55. 770 NSArrayTest [5346: 358824] The 3 element is D

21:16:55. 774 NSArrayTest [5346: 358824] The element is

21:16:55. 774 NSArrayTest [5346: 358824] The element is B

21:16:55. 774 NSArrayTest [5346: 358824] The element is C

21:16:55. 774 NSArrayTest [5346: 358824] The element is D

Program ended with exit code: 0

 

Note that NSArray is immutable, just like a String object in java.

NSMutableArray is a variable array.

 

This is the opposite of java: ArrayList in Java is variable. If you want to be immutable, third-party implementations such as Apache can be implemented.

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.