copy elements from one array to another array - objective-c

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

Related

access object properties from a multidimensional array in objective-c

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]);

Objective C. NSMutable Dictionary adding value to existing key

Is there any way to add a value to an existing key on a NSMutableDictionary?
Here is snippet of my code
NSMutableArray *mainFeedList = [NSMutableArray array];
[mainFeedList addObjectsFromArray:feedList];
for(int i = 0; i < mainFeedList.count; i++){
NSMutableArray *allFeed = [NSMutableArray array];
NSString *categoryId = [mainFeedList[i] valueForKey: #"categoryId"];
[allFeed addObject:mainFeedList[i]];
if(allFeed != nil && allFeed.count > 0) {
[feedContent setObject:allFeed
forKey:[combinedCategories[(int)[categoryId integerValue]] valueForKey: #"name"]];
}
Sample scenario:
NSMutableDictionary *mDict = #{#"key1":#"value1",#"key2": #"value2"};
I know that
[mDict setObject:mArray forKey:#"key1"];
will set an object to key1 but what I need is
add another object to key1 without replacing existing object (i need it both)
A structure of any NSDictionary is "one key to one object". If you would like to build a structure which maps one key multiple objects, you need an NSDictionary that maps keys to collections, such as NSArray or NSMutableArray:
NSMutableDictionary *mDict = #{
#"key1": [#[ #"value1" ] mutableCopy]
, #"key2": [#[ #"value2" ] mutableCopy]
};
Now you can add values to keys without replacing the existing ones:
[mDict[#"key1"] addObject:#"value3"];
NSDictionary only allows a single object corresponding to a single key. If you would like to add multiple objects corresponding to a single key, if you have string type of object then you can use separators also to combine strings like:
[mDict setObject:[NSString stringWithFormat:#"%#,%#", [mDict objectforKey:#"key1"], #"value2"] forKey:#"key1"];
Otherwise, you have to take collections, which you have already defined in your question.
add another object to key1 without replacing existing object...
why not set an dict to key1?
before:
[dict setObject:#"a" forKey:#"key1"];
U wanna:
add #"b" to "key1", in dict;
why not like:
[dict setObject:#{#"a":#"subKey1", #"b":#"subKey2"} forKey:#"key1"];
I would suggest storing an array as a key in your dictionary like I do below :
// Setting the value for "key1" to an array holding your first value
NSMutableDictionary *mDict = #{#"key1":#["value1"],#"key2": #"value2"};
Now when I want to add a new value I would do this:
// Create temp array
NSMutableArray *temp = mDict[#"key1"];
// Add new object
[temp addObject:#"value3"];
// Convert temp NSMutableArray to an NSArray so you can store it in your dict
NSArray *newArray = [[NSArray alloc] initWithArray:temp];
// Replace old array stored in dict with new array
mDict[#"key1"] = newArray;
Furthermore, if you are not sure if an array is already stored for that key you can run a check and populate with an empty dictionary like below:
if (mDict[#"key1"] == nil) {
mDict[#"key1"] = #[];
}

Load an element value of an array to another array Xcode Objective-C

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]);

object array to NSMutable array [duplicate]

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];
}

How to create a 2D NSArray or NSMutableArray in objective C?

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];