How do I add objects to a two-dimensional array? The array is initiated with a couple items (see below), but I need to add more items to the array.
images_ = [[NSArray alloc] initWithObjects:
[NSArray arrayWithObjects:#"http://farm3.static.flickr.com/2735/4430131154_95212b8e88_o.jpg", #"http://farm3.static.flickr.com/2735/4430131154_17d8a02b8c_s.jpg", nil],
[NSArray arrayWithObjects:#"http://farm5.static.flickr.com/4001/4439826859_19ba9a6cfa_o.jpg", #"http://farm5.static.flickr.com/4001/4439826859_4215c01a16_s.jpg", nil],nil];
Simple answer, add more items ;)
I don't know what's the problem?
EDIT:
Oh, I see, you need a NSMutableArray if you want to add objects later on!
Something like this:
NSMutableArray *firstSubArray = [[NSMutableArray arrayWithObjects:#"http://farm3.static.flickr.com/2735/4430131154_95212b8e88_o.jpg", #"http://farm3.static.flickr.com/2735/4430131154_17d8a02b8c_s.jpg", nil] retain];
NSMutableArray *secondSubArray = [[NSMutableArray arrayWithObjects:#"http://farm5.static.flickr.com/4001/4439826859_19ba9a6cfa_o.jpg", #"http://farm5.static.flickr.com/4001/4439826859_4215c01a16_s.jpg", nil] retain];
images_ = [[NSArray alloc] initWithObjects: firstSubArray, secondSubArray ,nil];
[secondSubArray addObject: #"New string"]
Related
I have an NSMutableArray which has five objects in it. I want to insert an object in the middle of an array, let me say atIndex 1, and move all the objects to index+1 how do I do that?
Can you please help me?
Use insertObject
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr insertObject:#"OBJ" atIndex:1];
With insertObject:AtIndex: method of NSMutableArray.
See docs.
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3",#"4",nil];
[arr insertObject:#"2" atIndex:[arr count]/2];
I'm try to use the functions -setValue:forKey: and get the value using -valueForKey:
Example:
NSArray *rootarr = [[NSArray alloc] init];
NSArray *array = [[NSArray alloc] initWithObjects:#"name", #"address", #"title", nil];
[rootarr setValue:array forKey:#"n"];
NSArray *getArr = [rootarr valueForKey:#"n"];
But the getArr array I got is not equal the array I set (array).
Could you please tell me what's wrong I met. And what's the way to use these functions?
NSArray's setValue:forKey: method is used for Key Value Coding, not for using an array as an associative container. You need to use NSMutableDictionary.
NSMutableDictionary *rootDict = [NSMutableDictionary dictionary];
NSArray *array = [[NSArray alloc] initWithObjects:#"name", #"address", #"title", nil];
[rootDict setValue:array forKey:#"n"];
NSArray *getArr = [rootDict valueForKey:#"n"];
An array isn't a key-value store, which you appear to want to use it as. I think you want an NSDictionary instead (or more precisely NSMutableDictionary if you want to modify it after its created).
According to the Apple documentation setValue:forKey:
Invokes setValue:forKey: on each of the array's items using the specified value and key.
Practical uses are when you want to set the same value to each element of the array
UILabel *label1 = [[UILabel alloc]init];
UILabel *label2 = [[UILabel alloc]init];
NSArray *arr = #[label1, label2];
[arr setValue:#"bye" forKey:#"text"];
NSLog(#"%# %#",label1.text, label2.text); // bye bye
in your example "getArr" is an empty array because your "rootarr" doesn't have elements, otherwise you receive a setValue:forUndefinedKey: into the contained objects that are not compliant for the assigned key
You can't add objects to an NSArray after it is created. You have to use NSMutableArray in order to do that.
I have an array called addArray which I am adding array objects to:
NSMutableArray *addArray = [NSMutableArray array];
[addArray addObjectsFromArray: delegate.arrayobjectOne];
[addArray addObjectsFromArray: delegate.arrayobjectTwo];
// etc...
Now, if I only want to init one of these arrays to display in my table (preferably from another view controller but that's another question), how would I do this? And how would I access a specific property of each array object, e.g. arrayobjectOne.info?
Thanks for your time.
you can say
someObject = [addArray objectAtIndex: someIndex];
use this Example
NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:#"one",#"two", nil];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];
self.list = [[NSMutableArray alloc] init];
[self.list addObjectsFromArray:myArray];
I am using insertObject for NSMutableArray for storing values into two dimensional array like below for example.
[mutableArrayPtr insertObject:[NSMutableArray arrayWithObjects:firstData, secondData,nil] atIndex:index];
I think, it is correct way of storing values in two dimensional array in Obj C.
I want to access it at later point of time. How can i do that?
Thanks!
The way you are doing it is perfectly fine. NSArray's don't have native two dimensional syntax like primitive arrays (int[][], double[][], etc.) in C do. So instead, you must nest them using an array of arrays. Here's an example of how to do that:
NSString *hello = #"Hello World";
NSMutableArray *insideArray = [[NSMutableArray alloc] initWithObjects:hello,nil];
NSMutableArray *outsideArray = [[NSMutableArray alloc] init];
[outsideArray addObject:insideArray];
// Then access it by:
NSString *retrieveString = [[outsideArray objectAtIndex:0] objectAtIndex:0];
To access your array at a later point in time, you would do something like:
NSArray* innerArray = [mutableArrayPtr objectAtIndex:0];
NSObject* someObject = [innerArray objectAtIndex:0];
Of course, change 0 to whatever index you actually need to retrieve.
EDIT:
Q: Is it "initWithObjects" (or) "insertObject:[NSMutableArray arrayWithObjects:imagePtrData,urlInStr,nil]" for two dimension?
A: initWithObjects and insertObject would both allow you to create two dimensional arrays. For example you could do:
NSMutableArray* arrayOne = [[NSMutableArray alloc] initWithObjects:[NSArray initWithObjects:#"One", #"Two", #"Three", nil], [NSArray initWithObjects:[#"Four", #"Five", #"Six", nil], nil];
// NOTE: You need to release the above array somewhere in your code to prevent a memory leak.
or:
NSMutableArray* arrayTwo = [NSMutableArray array];
[arrayTwo insertObject:[NSArray arrayWithObjects:#"One", #"Two", #"Three", nil] atIndex:0];
This method doesn't work...but when I move away from returning a dictionary object and return a single array to the table data source, it works perfectly. so the error of my ways is in how i am creating this dictionary...
Any Help?
- (NSDictionary *) returnDictionary {
self.shopNameArray = [[NSArray alloc] arrayWithObjects: #"Item 1", #"Item 2", #"Item 3", nil];
self.shopLocationArray = [[NSArray alloc] arrayWithObjects: #"Cincinnati, OH", #"Phoenix, AZ", #"Tuscon, AZ", nil];
self.shopImageArray = [[NSArray alloc] arrayWithObjects: #"image1", "image2", #"image3", nil];
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] retain];
[theDictionary setObject:shopNameArray forKey:#"Shop Name"];
[theDictionary setObject:shopLocationArray forKey:#"Shop Location"];
[theDictionary setObject:shopImageArray forKey:#"Shop Image"];
return theMechanicDictionary;
}
Thanks.
Fixed block of code:
- (NSDictionary *) returnDictionary {
self.shopNameArray = [NSArray arrayWithObjects: #"Item 1", #"Item 2", #"Item 3", nil];
self.shopLocationArray = [NSArray arrayWithObjects: #"Cincinnati, OH", #"Phoenix, AZ", #"Tuscon, AZ", nil];
self.shopImageArray = [NSArray arrayWithObjects: #"image1", "image2", #"image3", nil];
NSMutableDictionary *theDictionary = [NSMutableDictionary dictionary];
[theDictionary setObject:self.shopNameArray forKey:#"Shop Name"];
[theDictionary setObject:self.shopLocationArray forKey:#"Shop Location"];
[theDictionary setObject:self.shopImageArray forKey:#"Shop Image"];
return theDictionary;
}
Changes made:
[NSArray arrayWithObjects:… is the format for the particular class method you are trying to call.
[NSMutableDictionary dictionary] is the easiest way to get an autoreleased mutable dictionary. Like #Douwe suggested, you may want to init this object with a starting size of 3, but that's not necessary. It only makes a difference if you're loading a butt-ton of objects into your dictionary and you don't want it to continuously be resizing itself.
When adding the arrays to your dictionary, you shouldn't reference them by the name of the iVar. Since you set the arrays using properties (self.shopNameArray =) the name of the iVar will not necessarily be the same.
You need to call arrayWithObjects: on NSArray directly, not on an instance of NSArray. So use [NSArray arrayWithObjects: #"Item 1", #"Item 2", #"Item 3", nil]; instead of [[NSArray alloc] arrayWithObjects: #"Item 1", #"Item 2", #"Item 3", nil];.
Also change [[NSDictionary alloc] retain] to [NSMutableDictionary arrayWithCapacity:0], so you're returning an autoreleased NSMutableDictionary. Also, you should always call [[SomeClass alloc] init] or [[SomeClass alloc] initWith...], not [[SomeClass alloc] someMethod], that doesn't work.
It looks to me like you don't really understand what methods are supposed to be called on what exactly...
Help Still Needed!
Well, I believe my dictionary is set-up very well now, but yet I continue to stumble when accessing the values of the dictionary...my table view data source just decides to crash...here is the next set of code...in snippets.
SettingsDataObject *myDataObject = [[SettingsDataObject alloc] init];
self.myNewDictionary = [myDataObject returnDictionary];
[myDataObject release];
self.myShopNameArray = [myNewDictionary objectForKey:#"Shop Name"];
self.myShopLocationArray = [myNewDictionary objectForKey:#"Shop Location"];
self.myShopImageArray = [myNewDictionary objectForKey:#"Shop Image"];
self.mySelectedShopArray = [myNewDictionary objectForKey:#"Shop Selected"];
The above occurs first in my cellForRowAtIndexPath: method. Then once I determine if a cell exists and all that other hoopla, I then set the textlabel.text of the cell using the shop name array which is stored within the dictionary at key #"Shop Name".
Now, I am not sure if it is the above code or the below...but when get rid of both and go with a locally defined array and plug that in...everything works fine. Again, something missing here. I tried this and the second line below...error must be in the above.
cell.textLabel.text = [[myNewDictionary objectForKey:#"Shop Name"] objectAtIndex:indexPath.row]
cell.textLabel.text = [myShopNameArray objectAtIndex:indexPath.row];
Change
[[NSDictionary alloc] retain]
to
[[NSMutableDictionary alloc] retain]