How to remove brackets and , from NSArray? - ios

I have an array contains array at each index in it.
array is :(
(
"http://localhost/ColorPicker/upload/2014-01-14-04-01-19g1.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-20g2.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-20g3.jpg"
),
(
"http://localhost/ColorPicker/upload/2014-01-14-04-01-49y1.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-50y2.jpg"
),
(
"http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg"
)
)
I want to make a single array of that like
(
"http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg"
)
How can I eliminate (),() inside the array and make a single array containing the urls.

You'll need to make a new array:
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (NSArray *a in array)
[newArray addObjectsFromArray:a];

You can flatten your array using the Key-Value Coding operator "#unionOfArrays":
NSArray *nested = #[#[#"A1", #"A2", #"A3"], #[#"B1", #"B2", #"B3"], #[#"C1", #"C2", #"C3"]];
NSArray *flattened = [nested valueForKeyPath:#"#unionOfArrays.self"];
NSLog(#"nested = %#", nested);
NSLog(#"flattened = %#", flattened);
Output:
nested = (
(
A1,
A2,
A3
),
(
B1,
B2,
B3
),
(
C1,
C2,
C3
)
)
flattened = (
A1,
A2,
A3,
B1,
B2,
B3,
C1,
C2,
C3
)

You need to write code to walk your outer array, copying the contents of the second-level array to a "flat" array. Something like this:
(Edited based on Carl Norum's post to use addObjectsFromArray)
-(NSArray )flattenArray: (NSArray *) sourceArray;
{
NSMutableArray *result = [[NSMutableArray alloc] init];
for (NSArray *array sourceArray)
{
//Make sure this object is an array of some kind.
//(use isKindOFClass to handle different types of array class cluster)
if ([array isKindOfClass: [NSArray class])
{
[result addObjectsFromArray: array];
}
else
{
NSLog(#"Non-array object %# found. Adding directly.", array);
[result addObject: array];
}
return [result copy]; //return an immutable copy of the result array
}

you'll have to normalise your array
loop through the array, then all of it's sub arrays and add them to another array
Something like this should be enough to get you started: here

Related

iOS Get all NSArray into one

I've NSArray with NSArray in. I would like to get all NSArray into one.
I think there is a simple function to use, isn't it ?
Here is what I receive :
(
(
n0eGi1KJWq,
KHGeW32548,
),
(
n0eGi1KJWq
)
)
I would like to get :
(
n0eGi1KJWq,
KHGeW32548,
n0eGi1KJWq
)
A simply loop can be used to create a new array:
NSArray *mainArray = ... // the array containing the other arrays
NSMutableArray *finalArray = [NSMutableArray array];
for (NSArray *innerArray in mainArray) {
[finalArray addObjectsFromArray:innerArray];
}
At this point finalArray will have all of the objects from all of the other arrays.
You can also use #unionOfArrays for this purpose:
NSArray *flatArray = [array valueForKeyPath: #"#unionOfArrays.self"];
If you need a deeper flattening (for three dimensional array), use it twice:
NSArray *flatArray = [array valueForKeyPath: #"#unionOfArrays.self.#unionOfArrays.self"];

Objective-C Remove extra parentheses inside NSArray

I have this Mutable array that has this value
myArray = (
(
"1466759705.37136"
),
(
"1466761980.81194"
),
(
"1466990587.45011"
)
I want to remove the () on each element inside the array and make it like this
innerArrayOfDate = (
"1466759705.37136",
"1466761980.81194",
"1466990587.45011"
)
I tried myArray[0] to see if I will not include the parentheses but it still includes.
myArray[0] = (
"1466759705.37136"
)
You have array value which is inside of another array.
The value which you want is at 0 index of array so, you need to just add this myArray[0][0] instead of this myArray[0]
myArray[0] = (
"1466759705.37136"
)
myArray[0][0] = "1466759705.37136"
try this,
NSArray *myArray = #[#[#"1466759705.37136"],#[#"1466761980.81194"],#[#"1466990587.45011"]];
NSMutableArray *newArray = [[NSMutableArray alloc]init];
for (NSArray *array in myArray) {
[newArray addObjectsFromArray:array];
}

Difference between these 2 NSArrays

What is the difference between these two NSArray outputs using NSLog?
//Use to get all values from within Dictionary
NSArray * values = [self.dict allValues];
//Title
self.namesArray = [values valueForKey:#"Imade"];
gives the following which is broken up into the KEY values they are stored in:
Names : (
(
tttt,
tgrtg,
trgrtgrtgrtgrtg
),
(
fcxczxc,
zcxzc,
asad
),
(
sdedw,
frfefr
)
)
and
self.colorArray= [[NSArray alloc] initWithObjects: #"Red", #"Yellow", #"Green",
#"Blue", #"Purpole", nil];
gives
Names : (
Red,
Yellow,
Green,
Blue,
Purpole
)
Now I can populate a uiTableViewCell with the colored NSArray however I cannot with the Names NSArray. Why is this and how do I fix it? Do I need to break the namesArray down further and how do I do this? Im sorry if this is obvious.
If needed
//Use to get Key Values from within Dictionary
self.sectionArray = [self.dict allKeys];
UPDATE:
It looks like the first array is an array of arrays, and the second one is an array of strings.

How to store for one key multple values?

I am new to objective c. I am interesting in having something like this:
- for each key I want to store multiple values like:
2 holds a,b,c
3 holds d,e,f
When pressing 2 3 or 2 3 3, I want to have at output all the combinations from these 6 values. Should I use a NSMutableDictionary for this? I need some advices!
You can store arrays in dictionaries. For example
NSDictionary *mapping = #{#"2": #[#"a", #"b", #"c"]};
and you could for each key press add the objects from the array in the dictionary to an intermediate array
NSMutableArray *values = [NSMutableArray array];
...
// For each time a key is pressed
[values addObjectsFromArray:#[mapping[keyPressed]]];
...
When you want to display the output you calculate all combinations for all values in the values array.
For store multiple value of single key, you need to add array as value of dictionary key, such like,
NSArray *temArray1 = ...// which has value a,b,c
NSArray *temArray2 = ...// which has value d,e,f
Add this array as value of specific key, such like
NSMutableDictionary *temDic = [[NSMutableDictionary alloc] init];
[temDic setValue:temArray1 forKey#"2"];
[temDic setValue:temArray1 forKey#"3"];
NSLog(#"%#", temDic)
Above code describe simple logic as per your requirement change it as you need.
Please Try this..
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSArray *aryFlashCardRed=[[NSArray alloc]initWithObjects:#"f1",#"f2",#"f3", nil];
NSArray *aryFlashCardYellow=[[NSArray alloc]initWithObjects:#"f4",#"f5",#"f6", nil];
NSArray *aryFlashCardGreen=[[NSArray alloc]initWithObjects:#"f7",#"f8",#"f9", nil];
NSArray *aryScore=[[NSArray alloc]initWithObjects:#"10",#"20",#"30", nil];
[dictionary setObject:aryFlashCardRed forKey:#"red"];
[dictionary setObject:aryFlashCardYellow forKey:#"yellow"];
[dictionary setObject:aryFlashCardGreen forKey:#"green"];
[dictionary setObject:aryScore forKey:#"score"];
Display dictionary like this
{
green = (
f7,
f8,
f9
);
red = (
f1,
f2,
f3
);
score = (
10,
20,
30
);
yellow = (
f4,
f5,
f6
);
}
Objective-c has 3 types of collections: NSArray, NSSet, NSDictionary (and their mutable equivalents). All this collections can store only objects. This collections uses for different cases, so try to find case which is appropriate to You and use appropriate collection.
P.S. My first wish was to write RTFM
Try like below :-
NSDictionary *yourDict=[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:#"a", #"b", #"c",nil]
forKeys:[NSArray arrayWithObjects:#"2",nil]];

Objective C: Sort Two Dimensional Array

I have an array of arrays. The contained array's first elements are all NSDate objects. I would like to sort the array containing the arrays in order from most recent to least. For some reason, the below sorting algorithm results in an infinite loop. Can anyone help me out? Thank you.
Best...SL
//array is the array containing all of the other arrays(that have NSDates as their first elements)
//temp is the new array being added to the end of the array, to later be sorted into the correct position.
[array addObject:temp];
NSMutableArray *tempArray;
for (int i=0; i<[array count]; i++)
{
NSDate *session1, *session2;
session1 = [[array objectAtIndex:i] objectAtIndex:0];
session2 = [[array objectAtIndex:[array count]-1] objectAtIndex:0];
if([session1 compare:session2] == NSOrderedDescending)
{
tempArray = [array objectAtIndex:i];
[array insertObject:[array objectAtIndex:[array count]-1] atIndex:i];
[array insertObject:tempArray atIndex:[array count]-1];
}
}
This results in an infinite loop because, in every step, you're inserting two more values into the array. Thus your array is growing faster than you are traversing it. I'm assuming you meant to swap the values.
In any case, a much simpler and more efficient sort is to use the built-in sorting capabilities:
// NSArray *sortedArray, with the unsorted 'array' pulled from some other instance
sortedArray = [array sortedArrayUsingComparator:^(id a, id b) {
return [[b objectAtIndex:0] compare:[a objectAtIndex:0]];
}];
If array is mutable and you want to sort it in place:
[array sortUsingComparator:^(id a, id b) {
return [b[0] compare:a[0]];
}];
If array is immutable or you want to leave it alone and make a sorted copy:
NSArray *sortedArray = [array sortedArrayUsingComparator:^(id a, id b) {
return [b[0] compare:a[0]];
}];

Resources