views:

69

answers:

3

Hello,

I want to ask about the NSArray in objective C.

I want to create a dynamic array as I don't know about the size of the array. And I also want to insert some of the data to the array. However, I don't know how to decl, init and insert the element in to the array.

The following is the C++ version. I want to create a empty array A and put all the array element in array B to A.

// empty array

string arrA[] = new string();

// put the arrB into arrA

for(int i=0; i < arrB.length(); i++)

      arrA[i] = arrB[i];

Thank you very much.

+2  A: 

Use a NSMutableArray. If arrB is a NSArray:

NSMutableArray *arrA = [[NSMutableArray alloc] init];

[arrA addObjectsFromArray: arrB];

Here is the documentation: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableArray/addObjectsFromArray:

Adrian
Not knocking the answer, but just to point out: You can also just write `NSArray *arrA = [arrB copy]`.
Chuck
@Chuck: The question asked for a "dynamic array", and `[arrB copy]` will make another `NSArray` that isn't dynamic.
Adrian
I'm not sure what "dynamic" means here that's leading you to protest. All objects in Objective-C dynamically resolve messages and are dynamically typed. If it just means "mutable," then it's `NSMutableArray *arrA = [arrB mutableCopy]`.
Chuck
I want to a unknown size of the array when I declare the array. By the way, I would like to ask, what is the different between the 'mutable' and 'dynamic' in objective C mean. Thank you very much.
Questions
+2  A: 

You should use the class named NSArray or NSMutableArray. These are similar to std::vector however they are somewhat different in their use due to the way Objective-C works.

// Immutable array (must be created with contents specified)
NSArray *strings = [NSArray arrayWithObjects:@"Hello", @", world!", nil];

// Mutable array (can be modified)
NSMutableArray *strings = [NSMutableArray array];
[strings addObject:@"Hello"];
[strings addObject:@", world!"];

The difference with Objective-C NSArray objects is that they can take any type of object as long as it is a subclass of NSObject.

NSArray *array = [NSArray arrayWithObjects:@"A string", [NSNumber numberWithInt:1337], [NSDictionary dictionary]];

To access values you must use the objectAtIndex: message.

NSString *aString = [array objectAtIndex:10];
NSLog(aString);
Nick Bedford
A: 

Thank you for all of the reply. The information is very useful.

And I found another problems about the NSArray. I check the apple document. I found that I have to declare the array in 'NSMutableArray' if I don't the size of the array. Actually, I found that my program has some problems.

I want to store all the cookies into the array, no matter it is a NSArray or NSMutableArray.

I use the following code to retrieve the cookies and in NSArray form. However, I have to add some cookies to the array in the later part. Do I need to create a 2D arrays, one stores the name, one stores the value of the cookies, or use the whole array to store the cookies.

The following is the code:

// I use the NSArray to store the cookies from the urlCookies
NSArray *cookiesUse = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:urlCookies];

// I use the objectAtIndex to retrieve the element from array to the NSString
NSString *cookesInArrayUse_0 = [cookiesUse objectAtIndex:0];

// get the cookie name and value from the cookesInArrayUse_0
NSString *cookieNameUse  = [cookesInArrayUse_0 name];
NSString *cookieValueUse = [cookesInArrayUse_0 value];

// decl an array to store all the cookies
// and I don't know how many cookies/string I need to store
// so I want to decl a unknown size array (dynamic/mutable)

Can anyone help me. Thank you very much.

Questions
You should ask this as a separate question.
JeremyP