Objective-C Remove extra parentheses inside NSArray - ios

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

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

how to find different object in NSMutableArray and add to another NSMutableArray?

I have one problem and I want to search every object in my NSMutableArray.
if this object (the object for searching) no "0" add to another NSMutableArray with name : StarArray
this is my first NSMutableArray
(
(
1,
1,
6
),
(
0
),
(
0,
3
),
(
5,
3
),
(
0
)
)
this NSMutableArray is 2d (matrix) and I want get every object except object that have 0 value and add to StarArray
StarArray : {1,1,6,3,5,3}
Try this
It will take less iterations to execute
NSMutableArray *resultArr = [NSMutableArray array];
for(NSArray *arr in yourArr)
{
NSMutableArray *ar = [NSMutableArray arrayWithArray:arr];
while( [ar containsObject:#"0"])
{
[ar removeObject:#"0"];
}
if([ar count])
[resultArr addObjectsFromArray:ar];
}
Try this code, it may work:
NSMutableArray *StarArray = [[NSMutableArray alloc]init];
for (int i = 0; i < [firstArray count] ; i++) {
for (id object in [firstArray objectAtIndex:i]) {
if (![object isEqualToString:#"0"]) {
[StarArray addObject:object];
}
}
}
NSLog(#"%#", StarArray);

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.

Append an object in an array

I want to append an object in an array.
For example,
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i=0; i<5; i++)
{
[array addObject:#[#"Any"]];
}
It gives an output like this:
array: (
(
Any
),
(
Any
),
(
Any
),
(
Any
),
(
Any
)
)
Now I want to append object at index 3 of an array so that it could appear like below:
array: (
(
Any
),
(
Any
),
(
Any
),
(
Any, Where, How, When
),
(
Any
)
)
Use function insertObjectAtIndex to achieve it.
[array insertObject:anObject atIndex:2];
If I understand you...
[array replaceObjectAtIndex:2 withObject:#[[[array objectAtIndex:2] firstObject],#"Where",#"How",#"When"]];
Try this:
NSMutableArray *array = [NSMutableArray new];
for (int i=0; i<5; i++)
{
[array addObject:[NSMutableArray arrayWithObject:#"Any"]];
}
NSLog(#"%#", array);
NSMutableArray *subArray = [array objectAtIndex:3];
[subArray addObjectsFromArray:#[#"Where", #"How", #"When"]];
NSLog(#"%#", array);
Note, #[] creates an instance of NSArray class which cannot be modified later. You can modify only instances of NSMutableArray class.

How to remove brackets and , from NSArray?

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

Resources