Objective-C syntax for array objects
If the original Yusong Momo article is reprinted, please note: It is reprinted to my independent domain name blogYusong Momo program Research Institute, Original address: http://www.xuanyusong.com/archives/412
The array created using an array object is very powerful. The array defined in Java or C must be of the same type as each element in the array. The objective-C language can put any type of data in the array. It is worth noting that only the pointer pointing to this object can be put. If int, Char, double, etc. are put directly, it cannot be done.
1. Immutable array object
The nsarray keyword is used to create an unchangeable array. After initialization, the elements of this array cannot be dynamically added or deleted.
Create a myclass object and store the pointer to this object in the nsarray array. It can be seen that the nsarray object array can store data of any object.
[Array count]: Get the length of the array of this object.
[Array objectatindex 0]: input an array script ID to obtain the data object.
[Arraywithobjects;...]: assigns a value to the array object initialization. Here, we can write a pointer to any object, and nil must be used at the end. Mark end ~
Use the for loop to traverse this array object.
#import <UIKit / UIKit.h>
#import "MyClass.h"
int main (int argc, char * argv [])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Add our test code
// Create a custom class
MyClass * myClass = [[MyClass alloc] init];
// Create an immutable array
NSArray * array = [NSArray arrayWithObjects: @ "Yu Song", @ "MOMO", @ "Wakaka", myClass, nil];
// Get the length of the array
int count = [array count];
// Iterate through this array
for (int i = 0; i <count; i ++) {
NSLog (@ "Ordinary traversal: the array object when i =% d is:% @", i, [array objectAtIndex: i]);
}
int retVal = UIApplicationMain (argc, argv, nil, nil);
[pool release];
return retVal;
}
The above for loop Traversal method is common, but the efficiency is not good, and the security is not high. Objective-C provides a fast enumeration Traversal method, which is recommended for traversal.
For (nsobject * object in array): traverses the array. Each loop assigns the elements in the array to * object for loop traversal.
#import <UIKit / UIKit.h>
#import "MyClass.h"
int main (int argc, char * argv [])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Add our test code
// Create a custom class
MyClass * myClass = [[MyClass alloc] init];
// Create an immutable array
NSArray * array = [NSArray arrayWithObjects: @ "Yu Song", @ "MOMO", @ "Wakaka", myClass, nil];
// Iterate through this array
for (NSObject * object in array) {
NSLog (@ "Fast traversal of array objects:% @", object);
}
int retVal = UIApplicationMain (argc, argv, nil, nil);
[pool release];
return retVal;
}
2. variable array objects
[Nsmutablearray arraywithcapacity: 10]: initializes the length of a variable array object. If the subsequent Code adds an array that exceeds the length of 10
The length of nsmutablearray is automatically expanded, so you don't have to worry about array out-of-bounds.
[Array addobject:...]: Add a data object to the end of the variable array.
[Array addobjectsfromarray: ..]: adds an array object to the end of a variable array.*) #>];
#import <UIKit / UIKit.h>
#import "MyClass.h"
int main (int argc, char * argv [])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Add our test code
// Create a custom class
MyClass * myClass = [[MyClass alloc] init];
// Create a variable array with length 10
NSMutableArray * array = [NSMutableArray arrayWithCapacity: 10];
// Dynamically add objects to the array
[array addObject: @ "Yu Song"];
[array addObject: @ "MOMO"];
[array addObject: @ "Xiaokeai"];
[array addObject: @ "Wakaka"];
[array addObject: myClass];
for (NSObject * object in array)
{
NSLog (@ "output object array:% @", object);
}
int retVal = UIApplicationMain (argc, argv, nil, nil);
[pool release];
return retVal;
}
[Array insertobject: @ "insert a tiger" atindex: 1]: Insert a String object into the variable array and set its position index to 1.
[Array insertobject :( nsarray *) atindex :( nsindexset *)]:Insert an array object into a variable array. You can set its position index.
#import <UIKit / UIKit.h>
#import "MyClass.h"
int main (int argc, char * argv [])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Add our test code
// Create a custom class
MyClass * myClass = [[MyClass alloc] init];
// Create a variable array with length 10
NSMutableArray * array = [NSMutableArray arrayWithCapacity: 10];
// Dynamically add objects to the array
[array addObject: @ "Yu Song"];
[array addObject: @ "MOMO"];
[array addObject: @ "Xiaokeai"];
[array addObject: @ "wakaka"];
[array addObject: myClass];
// Insert into the array
[array insertObject: @ "Insert a little tiger" atIndex: 1];
for (NSObject * object in array)
{
NSLog (@ "output object array:% @", object);
}
int retVal = UIApplicationMain (argc, argv, nil, nil);
[pool release];
return retVal;
}
Nsange range = nsmakerange (); set a range between 0 and 5.
[Array removeobject: myclass inrange: range]: sets to delete data within a range. If this range does not delete this object, nothing will be deleted. In this example, because the myclass object is in the range of 0 to 5 of the array, myclass is deleted.
[Array removeobject :( ID)]: You do not need to consider the range. This object exists in the main array and will be deleted directly.
[Array removeobjectatindex :( nsuinteger)]: deletes the data of the specified index.
[Array removeobjectidenticalto :( ID)]: deletes the specified Element in the array.
[Array removeobjectidenticalto :( ID) inrange :( nsange)]: deletes the specified element within the specified range.
[Array removeobjectsinarray :( nsarray *)]: deletes an array element.
#import <UIKit / UIKit.h>
#import "MyClass.h"
int main (int argc, char * argv [])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Add our test code
// Create a custom class
MyClass * myClass = [[MyClass alloc] init];
// Create a variable array with length 10
NSMutableArray * array = [NSMutableArray arrayWithCapacity: 10];
// Dynamically add objects to the array
[array addObject: @ "Yusong"];
[array addObject: @ "MOMO"];
[array addObject: @ "Xiaokeai"];
[array addObject: @ "Wakaka"];
[array addObject: myClass];
// Set a delete range
NSRange range = NSMakeRange (0,5);
// delete element
[array removeObject: myClass inRange: range];
for (NSObject * object in array)
{
NSLog (@ "output object array:% @", object);
}
int retVal = UIApplicationMain (argc, argv, nil, nil);
[pool release];
return retVal;
}
[Array replaceobjectatindex 2 withobject: @ "modified"]: Replace the data marked as 2 in the array.
#import <UIKit / UIKit.h>
#import "MyClass.h"
int main (int argc, char * argv [])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Add our test code
// Create a custom class
MyClass * myClass = [[MyClass alloc] init];
// Create a variable array with length 10
NSMutableArray * array = [NSMutableArray arrayWithCapacity: 10];
// Dynamically add objects to the array
[array addObject: @ "Yusong"];
[array addObject: @ "MOMO"];
[array addObject: @ "Xiaokeai"];
[array addObject: @ "wakaka"];
[array addObject: myClass];
// Modify the element whose index is 2
[array replaceObjectAtIndex: 2 withObject: @ "has been modified"];
for (NSObject * object in array)
{
NSLog (@ "output object array:% @", object);
}
int retVal = UIApplicationMain (argc, argv, nil, nil);
[pool release];
return retVal;
}
I feel that the objective-C object array is really awesome. It is also a good choice to use array objects to process data ~~