NSArray add objects from other array - ios

I have three NSArray objects. I need to add all objects for this array to NSArray that is called allMyObjects.
Have NSArray standard solution to make it for example via initialization method or do I need make custom method to retrieve all objects from other arrays and put all retrieved objects to my allMyObjects array?

Don't know if this counts as a sufficiently simple solution to your problem, but this is the straight forward way to do it (as alluded to by other answerers, too):
NSMutableArray *allMyObjects = [NSMutableArray arrayWithArray: array1];
[allMyObjects addObjectsFromArray: array2];
[allMyObjects addObjectsFromArray: array3];

once see this one ,
NSArray *newArray=[[NSArray alloc]initWithObjects:#"hi",#"how",#"are",#"you",nil];
NSArray *newArray1=[[NSArray alloc]initWithObjects:#"hello",nil];
NSArray *newArray2=[[NSArray alloc]initWithObjects:newArray,newArray1,nil];
NSString *str=[newArray2 componentsJoinedByString:#","];
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"()\n "];
str = [[str componentsSeparatedByCharactersInSet:doNotWant] componentsJoinedByString: #""];
NSArray *resultArray=[str componentsSeparatedByString:#","];
NSLog(#"%#",resultArray);
O/P:-
(
hi,
how,
are,
you,
hello
)

You can call addObjectsFromArray: method on your allMyObjects array.

Here am adding code for store and get datas from array to array.
To store array to array
NSMutableArray rowOneRoundData = [NSMutableArray arrayWithObjects: #"45",#"29",#"12",nil];
NSMutableArray rowTwoRoundData = [NSMutableArray arrayWithObjects: #"41",#"45",#"45",nil];
NSMutableArray rowThreeRoundData = [NSMutableArray arrayWithObjects: #"12",#"45",#"22",nil];
NSMutableArray rowFourRoundData = [NSMutableArray arrayWithObjects: #"45",#"12",#"61",nil];
NSMutableArray rowFiveRoundData = [NSMutableArray arrayWithObjects: #"12",#"14",#"14",nil];
NSMutableArray rowSixRoundData = [NSMutableArray arrayWithObjects: #"12",#"12",#"12",nil];
NSMutableArray rowSevenRoundData = [NSMutableArray arrayWithObjects: #"12",#"36",#"83",nil];
NSMutableArray rowEightRoundData = [NSMutableArray arrayWithObjects: #"37",#"57",#"45",nil];
NSMutableArray rowNineRoundData = [NSMutableArray arrayWithObjects: #"12",#"93",#"83",nil];
NSMutableArray rowTenRoundData = [NSMutableArray arrayWithObjects: #"16",#"16",#"16",nil];
NSArray circleArray = [[NSArray alloc]initWithObjects:rowOneRoundData,rowTwoRoundData,rowThreeRoundData,rowFourRoundData,rowFiveRoundData,rowSixRoundData,rowSevenRoundData,rowEightRoundData,rowNineRoundData,rowTenRoundData, nil];
Get data from Circle Array
for (int i= 0; i<10;i++)
{
NSArray *retriveArrar = [[circleArray objectAtIndex:i] mutableCopy];
}

Related

Sorting an NSArray keeping original indexes rather than values

I have an array of values, and I'd like to sort it from highest to lowest values. Here's my array:
#[#0.985517248005697843460382, #0.000103821940243745174581, #0.002930049254083499140483, #0.006089428685598983863325, #0.000169959081717878927225 #0.038708805305937427077012, #0.005785644142951103588435, #0.003949420720490224266663 #0.003306789895982742942537, #0.005520713777168946394258];
What's the most efficient way to create a new array, but instead of containing the sorted values, it would contain the indexes from the original array?
Currently this is what I'm doing:
NSArray *array = #[#3, #5, #7, #2, #4, #4.1, #1, #10];
NSArray *sortedArray = [array sortedArrayUsingSelector: #selector(compare:)];
NSMutableArray *arraySortedByOriginalIndexes = [[NSMutableArray alloc] init];
for (int i = 0; i < sortedArray.count; i++) {
NSUInteger index = [array indexOfObject:sortedArray[i]];
[arraySortedByOriginalIndexes addObject:#(index)];
}
Here is the simple solution if no numbers in array are equal.
NSArray *sortedArray = [array sortedArrayUsingSelector: #selector(compare:)];
NSMutableArray *mutableIndexes = [NSMutableArray array];
for (NSNumber *number in sortedArray) {
[mutableIndexes addObject:#([array indexOfObject:number])];
}
NSArray *indexes = [mutableIndexes copy];
Again, it only works when no numbers are equal.

How to sort a NSArray with another NSArray?

NSArray A = #[[[#"id":#"3"]], [[#"id":#"4"]] ,[[#"id":#"c"]],[[#"id":#"f"]]];
NSArray idArray = #[#"c", #"3", #"4",#"f"];
Just a example I assumed.
How can I sort A by its id with idArray?
That is, I want A to become:
NSArray A= #[[[#"id":#"c"]], [[#"id":#"3"]] ,[[#"id":#"4"]],[[#"id":#"f"]]];
Now, I want to ask for an algorithm to sort array A to get the desired result.
---I get my answer when I search in google:
NSArray *sorter = #[#"B", #"C", #"E"];
NSMutableArray *sortee = [#[
#[#"B", #"abc"],
#[#"E", #"pqr"],
#[#"C", #"xyz"]
] mutableCopy];
[sortee sortUsingComparator:^(id o1, id o2) {
NSString *s1 = [o1 objectAtIndex:0];
NSString *s2 = [o2 objectAtIndex:0];
NSInteger idx1 = [sorter indexOfObject:s1];
NSInteger idx2 = [sorter indexOfObject:s2];
return idx1 - idx2;
}];
If you want to compare both array you can use
NSArray *array1=#[#"3",#"4",#"c","f"];
NSArray *array2=#[#"c",#"3",#"4","f"];
array1=[array1 sortedArrayUsingSelector:#selector(compare:)];
array2=[array2 sortedArrayUsingSelector:#selector(compare:)];
if ([array1 isEqualToArray:array2]) {
NSLog(#"both are same");
}
else{
NSLog(#"both are differnt");
}
or If you want to get common elements from 2 array use
NSMutableSet* set1 = [NSMutableSet setWithArray:array1];
NSMutableSet* set2 = [NSMutableSet setWithArray:array2];
[set1 intersectSet:set2]; //this will give you only the obejcts that are in both sets
NSArray* result = [set1 allObjects];
This would be a better way to make a dictionary for A. And then sorting based on their specific values like IQ, Name etc.
NSArray A = #[[[#"id":#"3"]], [[#"id":#"4"]] ,[[#"id":#"c"]],[[#"id":#"f"]]];
NSArray idArray = #[#"c", #"3", #"4",#"f"];
NSMutableArray *array = [NSMutableArray array];
for (int id = 0;idx<[A count];id++) {
NSDictionary *dict = #{#"Name": A[id],#"IQ":idArray[id]};
[array addObject:dict];
}
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:#"IQ" ascending:NO];
[array sortUsingDescriptors:#[descriptor]]

addObject to NSArray in Objective-C

How to addObject to NSArray using this code? I got this error message when trying to do it.
NSArray *shoppingList = #[#"Eggs", #"Milk"];
NSString *flour = #"Flour";
[shoppingList addObject:flour];
shoppingList += #["Baking Powder"]
Error message
/Users/xxxxx/Documents/iOS/xxxxx/main.m:54:23: No visible #interface for 'NSArray' declares the selector 'addObject:'
addObject works on NSMutableArray, not on NSArray, which is immutable.
If you have control over the array that you create, make shoppingList NSMutableArray:
NSMutableArray *shoppingList = [#[#"Eggs", #"Milk"] mutableCopy];
[shoppingList addObject:flour]; // Works with NSMutableArray
Otherwise, use less efficient
shoppingList = [shoppingList arrayByAddingObject:flour]; // Makes a copy
You can't add objects into NSArray. Use NSMutableArray instead :)
Your array cant be changed because is defined as NSArray which is inmutable (you can't add or remove elements) Convert it to a NSMutableArray using this
NSMutableArray *mutableShoppingList = [NSMutableArray arrayWithArray:shoppingList];
Then you can do
[mutableShoppingList addObject:flour];
NSArray does not have addObject: method, for this you have to use
NSMutableArray. NSMutableArray is used to create dynamic array.
NSArray *shoppingList = #[#"Eggs", #"Milk"];
NSString *flour = #"Flour";
NSMutableArray *mutableShoppingList = [NSMutableArray arrayWithArray: shoppingList];
[mutableShoppingList addObject:flour];
Or
NSMutableArray *shoppingList = [NSMutableArray arrayWithObjects:#"Eggs", #"Milk",nil];
NSString *flour = #"Flour";
[shoppingList addObject:flour];

Adding NSMutableDictionary's in NSMutableArray

I'm scanning an NSMutableArray of dictionary's and request to a WebService a info from each data.
The problem is after get the data, i need to update 2 keys of the same array of dictionary's and when i added an object, the array continues empty.
What is wrong?!
PS: using ARC
NSMutableArray* arrayOfDicts = [NSMutableArray arrayWithObjects:
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"2013",#"year",
#"a1beb511-7fe1-434b-ab87-a0d02fb47713",#"yearTB",
#"",#"receita_arrecadada",
#"",#"receita_prevista_atualizada",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"2012",#"year",
#"5d20f841-d790-4671-ae26-505c9a8c7f76",#"yearTB",
#"",#"receita_arrecadada",
#"",#"receita_prevista_atualizada",
nil],
nil];
__block NSMutableArray* arrayTemp = [[NSMutableArray alloc] init];;
__block NSMutableDictionary* dicTemp;
for(dicTemp in arrayOfDicts){
NSString* year = [dicTemp objectForKey:#"year"];
NSString* yearTB = [dicTemp objectForKey:#"yearTB"];
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
[RequestFacade getReceiptsTotalInYear:[NSString stringWithFormat:#"%#", yearTB] andCompletion:^(Receipt *receipt) {
//NSLog(#"%#",receipt.receita_arrecadada); // Print the correct actual value
[newDict addEntriesFromDictionary:dicTemp];
[newDict setObject:year forKey:#"year"];
[newDict setObject:yearTB forKey:#"yearTB"];
[newDict setObject:receipt.receita_arrecadada forKey:#"receita_arrecadada"];
[newDict setObject:receipt.receita_atualizada forKey:#"receita_atualizada"];
[arrayTemp addObject:newDict];
NSLog(#"%#", arrayTemp); // shows me the array of dicts
// until here, its ok!
}];
}
NSLog(#"%#", arrayTemp); // shows me an empty array...F*CK!
Initialise your arrayOfDict.
NSMutableArray * arrayOfDict = [[NSMutableArray alloc] init];
The array is empty because you are not initialising it. write this above line at the top and then add
[arrayOfDict arrayWithObjects: <UR ITEMS>];
Hope it works..!! Happy Coding

How to create NSDictionary inside NSArray and How to access them in ios

I am new to iOS and want to create an NSArray like this which contains an NSDictionary.
[
{
Image: 1, 2,3
Title: 1,2,3
Subtitle:1,2,3
}
]
I have tried this.
NSArray *obj-image=#[#"Test.png",#"Test.png",#"Test.png"];
NSArray *obj-title=#[#"Test",#"Test",#"Test"];
NSArray *obj-subtitle=#[#"Test",#"Test",#"Test"];
NSDictionary * obj_dictionary ={ image : obj_image, title:obj_title, subtitle:obj_subtitle}
NSArray * obj_array= [obj_dictionarry];
But not working and how to access them.
First of all, you initialization of Arrays and Dictionaries is wrong. You cannot use "-" in the names, period.
Second, you need to allocate and then initialize the objects. This is how you do that with arrays:
NSArray *images = [NSArray arrayWithObjects: #"TestImage",#"TestImage",#"TestImage",nil];
NSArray *titles = [NSArray arrayWithObjects: #"TestTitle",#"TestTitle",#"TestTitle",nil];
NSArray *subtitles = [NSArray arrayWithObjects: #"TestSubTitle",#"TestSubTitle",#"TestSubTitle",nil];
Then you need Mutable dictionary and mutable arrays to work with the data (mutable means you can change the values inside, add or remove objects etc.)
This is the most basic example of what you are trying to achieve:
NSArray *images = [NSArray arrayWithObjects: #"TestImage",#"TestImage",#"TestImage",nil];
NSArray *titles = [NSArray arrayWithObjects: #"TestTitle",#"TestTitle",#"TestTitle",nil];
NSArray *subtitles = [NSArray arrayWithObjects: #"TestSubTitle",#"TestSubTitle",#"TestSubTitle",nil];
NSMutableArray *objectsMutable = [[NSMutableArray alloc] init];
for (NSString *string in images) {
NSMutableDictionary *dictMutable = [[NSMutableDictionary alloc] init];
[dictMutable setObject:string forKey:#"image"];
//determining the index of the image
NSInteger stringIndex = [images indexOfObject:string];
[dictMutable setObject:[titles objectAtIndex:stringIndex] forKey:#"title"];
[dictMutable setObject:[subtitles objectAtIndex:stringIndex] forKey:#"subtitle"];
NSDictionary *dict = [[NSDictionary alloc] init];
dict = dictMutable;
[objectsMutable addObject:dict];
}
NSArray *objects = objectsMutable;
NSLog(#"%#", objects);
Hope this helps.
As you can see, I'm going through the images array, capturing the index of each one, an then just apply values of other arrays from the same index into a mutable dictionary.
All I do after that is just make a regular dictionary and array to put the data inside. This is ho the Log will look:
(
{
image = TestImage;
subtitle = TestSubTitle;
title = TestTitle;
},
{
image = TestImage;
subtitle = TestSubTitle;
title = TestTitle;
},
{
image = TestImage;
subtitle = TestSubTitle;
title = TestTitle;
}
)
You have an array with three objects inside, each with their own image, title and subtitle.
Here is code :
NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableDictionary *mdict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"object1",#"key1",#"object2",#"key2",nil];
[array addObject:mDict];
so now if u need to access dictionary from array then :
NSMutableDictionary *mDict1 = [array objectatindex:0];
NSLog(#"%#",[mDict1 valueForkey:#"key1"];
--> print object 1.
This is the way to store array of dictionaries:
NSDictionary *dic=#{#"kishore":#"hai"};
NSMutableArray *arr=[[NSMutableArray alloc]init];
[arr addobject:dic];
this is the way to get those values:
[arr objectforkeyValue #"kishore"];
NSMutableArray *dictArray = [[NSMutableArray alloc] init]; // created and initiated mutable array
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; // created and initiated mutable Dictionary
[dict setObject:#"object1" forKey:#"1"]; // added a key pair in dictionary (you can set multiple objects)
[dictArray addObject:dict]; // added dictionary to array (you can add multiple dictionary to array )
NSLog(#"dictionary inside an array : %#",dictArray[0][#"1"]); // access dictionary from array
You can try something like this
[NSArray arrayWithObjects:#{#"Image":#[#1,#2,#3]},#{#"Title":#[#1,#2,#3]},#{#"SubTitle":#[#1,#2,#3]}, nil];
You can easily create objects of array and access them back using following way.
NSArray *objimage=#[#"Test1.png",#"Test2.png",#"Test3.png"];
NSArray *objtitle=#[#"Test1",#"Test2",#"Test3"];
NSArray *objsubtitle=#[#"Test1",#"Test2",#"Test3"];
NSDictionary *obj_dictionary = #{#"image":objimage,#"Title":objtitle, #"subtitle":objsubtitle};
NSArray * obj_array= [[NSArray alloc] initWithObjects:obj_dictionary, nil]; // Create Array of nested objects
if([obj_array count] > 0) {
NSArray * imageArray = obj_array[0][#"image"]; // Access the nested objects in Array.
NSLog(#"%#", imageArray[0]);
}
first of all u need to declare correct variable name
NSArray *objImage=#[#"Test.png",#"Test.png",#"Test.png"];
NSArray *objTitle=#[#"Test",#"Test",#"Test"];
NSArray *objSubtitle=#[#"Test",#"Test",#"Test"];
at this point all u are created all the array, and u need to create dictionary like below
NSDictionary *obj_dictionary = #{#"image":objImage,#"title":objTitle, #"subtitle":objSubtitle};
// NSArray * obj_array = obj_dictionary[#"image"];
NSArray * obj_array = #[obj_dictionary]; //u can crate array of dictionary like this
in above obj_dictionary will contains all the array like below,
Title = (
Test,
Test,
Test
);
image = (
"Test.png",
"Test.png",
"Test.png"
);
subtitle = (
Test,
Test,
Test
);
and u can access the object in the dictionary like below using a key for example
NSArray * obj_array_images = obj_dictionary[#"image"];
gives an array of images that is associated with key image, similarly u can access other array like this by providing different keys associated with the dictionary obj_dictionary for example
NSArray * obj_array_titles = obj_dictionary[#"title"];
NSArray * obj_array_subtitles = obj_dictionary[#"subtitle"];
edit
NSArray *objImage=#[#"Test.png",#"Test.png",#"Test.png"];
NSArray *objTitle=#[#"Test",#"Test",#"Test"];
NSArray *objSubtitle=#[#"Test",#"Test",#"Test"];
NSDictionary *obj_dictionary = #{#"image":objImage,#"title":objTitle, #"subtitle":objSubtitle};
// NSArray * obj_array = obj_dictionary[#"image"];
NSArray * obj_array = #[obj_dictionary]; //u can crate array of dictionary like this

Resources