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.