I am new to Objective-C programming and I am trying to access object properties from a 2 dimensional array.
First I created two arrays, each of those arrays contains objects, then I made a 2 dimensional array that contains those arrays of objects by using NSMutableArray
NSMutableArray *team1 = [[NSMutableArray alloc] init];
[team1 addObject:tank1];
[team1 addObject:btr1];
[team1 addObject:ambulance1];
NSMutableArray *team2 = [[NSMutableArray alloc] init];
[team2 addObject:tank2];
[team2 addObject:btr2];
[team2 addObject:ambulance2];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:2];
[array addObject:team1];
[array addObject:team2];
What I want to do now is to access the properties of these objects by referring them from my 2d array and print them by using NSLog. Is this possible?
Please excuse me my question looks complicated, this is something new for me.
Use the above reference code to access the object like this.
1. To access btr 1
index for this will be : item 0, object 1 for array so can access by this code.
[[array objectAtIndex:0] objectAtIndex:1]
2. To access ambulance2
index for this will be : item 0, object 1 for array so can access by this code.
[[array objectAtIndex:1] objectAtIndex:2]
You can print them with same code given here as
NSlog(Item at object: %d index of 'array' and item in 'array' at index : %d is == %#,outerarray(array) index, innerarray(item array) index, [[array objectAtIndex:outerarrayindex] objectAtIndex:innerarrayindex]);
Or simply NSlog(#"%#",[[array objectAtIndex:outerarrayindex] objectAtIndex:innerarrayindex]);
Related
I like to copy the array A elements to Array B elements with specific
example :
array A=[0123]
array b=[1111111111111111111]
i want `b=[1111111101231111111]
int ip=0;
[b addObjectsFromArray:[A objectsAtIndexes:[NSIndexSetindexSetWithIndexesInRange:NSMakeRange(ip, 10)]]];
i know how to copy the array element , i want to know how to replace the object start from 9 to 13 in array b to replaced by array a element , can any give me hint
NSArray *a = #[#0,#1,#2,#3];
NSArray *b = #[#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1];
NSMutableArray *c = [b mutableCopy];
// The range here is index->8 (9th object) and length->4
[c replaceObjectsInRange:NSMakeRange(8,4) withObjectsFromArray:a];
You need to create mutable copy of your array and modify them:
NSMutableArray* mutableArray = [yourArray mutableCopy];
Then you will access to this methods: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html
Here I am getting the cityName1 with the city names like Piscataway, Iselin, Broklyn etc fetched from the tgpList1 array and I need to put the values into an array called item5.
There are 133 records fetched by the above iteration. The following code stores only the last record's cityName1 and not the entire list of city names though inside the loop.
I tried many ways but I am missing something.
tgpList1 is an array.
tgpDAO is an NSObject with two objects NSString *airportCode and NSString *cityName
NSArray *item5 = [[NSArray alloc]init];
for (int currentIndex=0; currentIndex<[tgpList1 count]; currentIndex++)
{
tgpDAO *tgpTable = (tgpDAO *)[self.tgpList1 objectAtIndex:currentIndex];
NSLog(#"The array values are %#",tgpList1);
NSString *cityName1 = tgpTable.cityName;
item5 =[NSArray arrayWithObjects:cityName1, nil];
}
Use mutable array.
{
NSMutableArray *item5 = [[NSMutableArray alloc]initWithArray:nil];
for (int currentIndex=0; currentIndex<[tgpList1 count]; currentIndex++) {
tgpDAO *tgpTable = (tgpDAO *)[self.tgpList1 objectAtIndex:currentIndex];
NSLog(#"The array values are %#",tgpList1);
NSString *cityName1 = tgpTable.cityName;
[item5 addObject:cityName1];
}
}
Instead of
item5 =[NSArray arrayWithObjects:cityName1, nil];
use
[item5 addObject:cityName1];
There are more ways of achieving that. However, this is the one that is designed for that purpose and the most "readable" from my pont of view.
If you need to clear the contents of item5 before then call
[item5 removeAllObjects];
right before the for loop.
What you were doing: arrayWithObjects allways creates a new array that ist made of the objects that are passed to it as aguments. If you do not use ARC, then you would create some serious memory leak with your code because arrayWithObjects creates and retains an object on every loop and on the next loop all references to the array object, that was just created, are lost without being released. If you do ARC then you do not have to worry about in this case.
NSMutableArray *myCities = [NSMutableArray arrayWithCapacity:2]; // will grow if needed.
for( some loop conditions )
{
NSString* someCity = getCity();
[myCities addObject:someCity];
}
NSLog(#"number of cities in array: %#",[myCities count]);
Once an Array is initialized, in order to set value of desired position, I am using
[self.appName replaceObjectAtIndex:x withObject:[self.appCell objectAtIndex: 0]];
Problem is that if I initialize appName array without objects, this array keeps empty and I must initialize it using initWithObjects and then works. Problem is that I do not know the size of an array and if I set it like:
NSMutableArray *nameArray = [[ NSMutableArray alloc] initWithObjects:#"test",#"test",#"test",#"test", nil];
self.appName = nameArray;
[nameArray release];
For example, works from 0 to 3 but from position 4 to following ones, after replaceObjectAtIndex, position has a nil value. How to solve it? Thank you
It's a mutable array, you can add objects (to the end) and insert objects (in the middle) as well, whenever you like:
NSMutableArray *nameArray = [[NSMutableArray alloc] init];
[nameArray addObject:#"test"];
[nameArray insertObject:#"another test" atIndex:0];
[nameArray removeObjectAtIndex:1];
You can do this anywhere you have a pointer to the mutable array.
Look at the count methon on NSArray it gives you the number of elements in the array. So check that your index x is less than the count then you can remove the element.
This question already has an answer here:
Getting an array of a property from an object array
(1 answer)
Closed 7 years ago.
I have an array of Objects.Object contains three values, id,name,value.I want to parse name from this object array and want to create a different array of names only....Can anyone help Plz.....
int i;
NSInteger *namesCount=[eCategories count]; //eCategories is an object array
SubCategories *subCategoriesList=[[SubCategories alloc] init];
//SubCategories is a NSObject class containing cat_name,cat_id,cat_value.
NSMutableArray *nameArray=[[NSMutableArray alloc]init];
for(i=0;i<namesCount;i++)
{
subCategoriesList=[eCategories objectAtIndex:i];
nameArray=subCategoriesList.cat_name;
}
NSArray has a method -valueForKey: which does exactly what you want in one message
Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.
NSArray* nameArray = [eCategories valueForKey: #"cat_name"];
How about
[objects valueForKeyPath: #"#distinctUnionOfArrays.name"]
Unless, of course, the objects inside the array are not NSDictionaries with "name" as the key for the names. You're not telling us enough.
int i;
NSInteger *namesCount=[eCategories count]; //eCategories is an object array
SubCategories *subCategoriesList=[[SubCategories alloc] init];
//SubCategories is a NSObject class containing cat_name,cat_id,cat_value.
NSMutableArray *nameArray=[[NSMutableArray alloc]init];
for(i=0;i
[nameArray addobject:subCategoriesList.cat_name];
}
I want to ask about the objective C question. I want to create a 2D NSArray or NSMutableArray in objective C. What should I do? The object stored in the array is NSString *. Thank you very mcuh.
This is certainly possible, but i think it's worthy to note that NSArrays can only hold objects, not primitive types.
The way to get around this is to use the primitive wrapper type NSNumber.
NSMutableArray *outer = [[NSMutableArray alloc] init];
NSMutableArray *inner = [[NSMutableArray alloc] init];
[inner addObject:[NSNumber numberWithInt:someInt]];
[outer addObject:inner];
[inner release];
//do something with outer here...
//clean up
[outer release];
Try NSMutableDictionary with NSNumbers as keys and arrays as objects. One dimension will be the keys, the other one will be the objects.
To create the specific "2D array"
NSMutableDictionary *twoDArray = [NSMutableDictionary dictionary];
for (int i = 0; i < 10; i++){
[twoDArray setObject:arrayOfStrings forKey:[NSNumber numberWithInt:i]];
}
To pull the data
NSString *string = [[twoDArray objectForKey:[NSNumber numberWithInt:3]] objectAtIndex:5];
//will pull string from row 3 column 5 -> as an example
Edited to make my answer more applicable to the question. Initially I didn't notice that you were looking for a 2D array. If you know how many by how many you need up front you can interleave the data and have a stride. I know that there are probably other (more objective standard) ways of having arrays inside of an array but to me that gets confusing. An array inside of an array is not a 2 dimensional array. It's just a second dimension in ONE of the objects. You'd have to add an array to each object, and that's not what I think of as a 2 dimensional array. Right or wrong I usually do things in a way that makes sense to me.
So lets say you need a 6x6 array:
int arrayStride=6;
int arrayDepth=6;
NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:arrayStride*arrayDepth];
I prefer to initialize the array by filling it up with objects;
for(int i=0; i<arrayStride*arrayDepth; i++) [newArray addObject #"whatever"];
Then after that you can access objects by firstDim + secondDim*6
int firstDim = 4;
int secondDim = 2;
NSString *nextString = [newArray objectAtIndex:firstDim+secondDim*6];