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.
Related
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"];
I am trying to add NSMutableArray in another NSMutableArray. But what I am trying to do is nested arrays.
My current code is:
NSMutableArray *array1 = [NSMutableArray arrayWithObjects: #"Red", #"Green", #"Blue", #"Yellow", nil];
NSMutableArray *array2 =[[NSMutableArray alloc] init];
[array2 addObject:array1];
This code is adding 4 objects in array2 but I want it to add array1 as single object.
Edit: This code is working I know but in my case in XCode something is wrong with initializing and it is adding 4 objects. I still could not figure it out. So this piece of code is working properly. So the problem was about initialization in a for loop.
I copy/pasted your code, and it adds one object to array2, not four.
Printing description of array2:
<__NSArrayM 0xc46c7b0>( <-- THIS ARRAY HAS 1 OBJECT
<__NSArrayM 0xc488770>( <-- THIS ARRAY HAS 4 OBJECTS
Red,
Green,
Blue,
Yellow
)
)
You may be are getting confused by the fact that printing the description, prints the contents of the inner array also.
Try:
NSMutableArray *array2 =[[NSMutableArray alloc] initWithArray:array1];
i am using this and its working
NSArray *array1 = #[array2, array3, ...];
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
According to NSArray class reference there are 4 type of methods to sort array:
1- sortedArrayUsingComparator:
2- sortedArrayUsingSelector:
3- sortedArrayUsingFunction:context:
4- sortedArrayUsingDescriptors:
For first three methods it mentioned :
The new array contains references to the receiving array’s elements, not copies of them.
But for the forth method (descriptor) it mentioned:
A copy of the receiving array sorted as specified by sortDescriptors.
But following example shows like the other 3 methods, descriptor also retain original array and do not return a new copy of it:
NSString *last = #"lastName";
NSString *first = #"firstName";
NSMutableArray *array = [NSMutableArray array];
NSDictionary *dict;
NSMutableString *FN1= [NSMutableString stringWithFormat:#"Joe"];
NSMutableString *LN1= [NSMutableString stringWithFormat:#"Smith"];
NSMutableString *FN2= [NSMutableString stringWithFormat:#"Robert"];
NSMutableString *LN2= [NSMutableString stringWithFormat:#"Jones"];
dict = [NSDictionary dictionaryWithObjectsAndKeys: FN1, first, LN1, last, nil];
[array addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys: FN2, first, LN2, last, nil];
[array addObject:dict];
// array[0].first = "Joe" , array[0].last = "Smith"
// array[1].first = "Robert" , array[1].last = "Jones"
NSSortDescriptor *lastDescriptor =[[NSSortDescriptor alloc] initWithKey:last
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *firstDescriptor =[[NSSortDescriptor alloc] initWithKey:first
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)];
NSArray *descriptors = [NSArray arrayWithObjects:lastDescriptor, firstDescriptor, nil];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:descriptors];
// array[1] == sortedArray[0] == ("Robert" , "Jones")
// comparing array entries whether they are same or not:
NSLog(#" %p , %p " , [array objectAtIndex:1] , [sortedArray objectAtIndex:0] );
// 0x10010c520 , 0x10010c520
it shows objects in both arrays are same,
"A copy of the receiving array sorted as specified by sortDescriptors" means that the array object is copied not the elements in the array. The reason the documentation uses the word "copy" is to make it clear that the returned array is not the same array instance as the receiver.
Elements in an array are never copied in Cocoa with the exception of initWithArray:copyItems:YES which will copy the first level items in the original array to the new array. Even then, this copy is done by calling copyWithZone: on the elements, so caveats apply depending on what elements are in your array.
Note that Cocoa is reference counted, so the concept of "deep copies" is not inherently built in for a reason. This is also (in part) the reason why array objects in cocoa come in two flavors (NSArray and NSMutableArray) and are usually immutable (NSArray) instead of as in other languages where there is not usually a concept of immutable and mutable arrays.
see this SO answer for how to get a "deep copy" of an NSArray.
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]];