Add in one array three or more - ios

I have an output of nsarray like:
2014-12-16 04:55:30.345 Syndasis[17108:7559904] (
"Hello"
)
2014-12-16 04:55:30.348 Syndasis[17108:7559904] (
"Ciao"
)
2014-12-16 04:55:30.351 Syndasis[17108:7559904] (
"Hola"
)
And I don't know how to add to one NSMutableArray. If I make this:
NSMutableArray *ns = [[NSMutableArray alloc] init];
[ns addObject:array];
NSLog(#"%#",ns);
The output is:
2014-12-16 04:59:04.148 Syndasis[17180:7587364] (
(
"Hello"
)
)
2014-12-16 04:59:04.150 Syndasis[17180:7587364] (
(
"Ciao"
)
)
I get the array from:
NSMutableArray *track = [[NSMutableArray alloc] init];
NSDictionary *summary = [dct objectForKey:#"tracks"];
for (NSDictionary *tracks in summary) {
[track addObject:[tracks objectForKey:#"foreign_id"]];
}
NSArray *arrayPR = [track firstObject];
NSMutableArray *ns = [NSMutableArray arrayWithObjects:arrayPR, nil];
NSLog(#"%#",ns);
Thanks!
Track response:
2014-12-16 05:25:29.895 Syndasis[17505:7776895] (
"Hi",
"Hola"
)
2014-12-16 05:25:29.895 Syndasis[17505:7776895] (
"Ciao"
)
I want to make: Make an nsarray from the first string for ever object
2014-12-16 05:25:29.896 Syndasis[17505:7776895] (
"Hi",
"Ciao"
)

I'm not 100% clear about your question, from my understanding, I think you are looking for:
NSMutableArray *ns = [NSMutableArray arrayWithArray: firstArray];
[ns addObjectsFromArray: secondArray];
[ns addObjectsFromArray: thirdArray];

NSMutableArray *track = [[NSMutableArray alloc] init];
NSDictionary *summary = [dct objectForKey:#"tracks"];
for (NSDictionary *tracks in summary)
{
//In tracks dictionary, the object for key 'foreign_id' is a array of strings.
NSArray* lArray = [tracks objectForKey:#"foreign_id"];
//pick first object from array and add it into mutable array.
[track addObject:[lArray firstObject]];
}
NSLog(#"%#",track);

I have one more approach for you. It may be helpful.
Lets add all values in NSMutableString with one separator between each values.
Then separate each values from this string will gives you an object of array.
See below code:
NSMutableString *mString = [NSMutableString new];
for (NSDictionary *tracks in summary) {
[mString appendString:[NSString stringWithFormat:#"%#,",[tracks objectForKey:#"foreign_id"]]];
}
NSMutableArray *array = [NSMutableArray arrayWithArray:[mString componentsSeparatedByString:#","]];
mString = nil;

If albumLists is a mutable array of arrays, and you want an array of the the first objects in each subarray, you can use a KVC Collection Operator:
NSArray* firstTracks = [albumLists valueForKeyPath:#"#unionOfObjects.firstObject"];
Of course, you might have separate arrays album1 & album2 you want to do this for, so you could do this to make the array of arrays and then use the collection operator:
NSArray* firstTracks = [#[album1, album2] valueForKeyPath:#"#unionOfObjects.firstObject"];

FINALLY I GOT THE SOLUTION! I POST THE SOLUTION FOR THE PEOPLE WHO HAVE THE SAME PROBLEM.
I got remembering my past of Javascript developer.
NSMutableArray *nsarray = [NSMutableArray new];
NSArray *array = [[[dct objectForKey:#"tracks"] firstObject] objectForKey:#"foreign_id"];
[nsarray addObject:array];
int len = nsarray.count;
NSMutableArray *var = [[NSMutableArray alloc] init];
for (int i = 0; i < len; i++) {
[var addObject:nsarray[i]];
}
NSLog(#"solution %#",var);
Thanks for the help!

Related

How to get first string of NSArray

Pretty straightforward question:
I have an array like so:
#[#"John Doe", #"Mister Appleseed", #"Steve"];
if it has two (or more) words, I want to delete them all but the first, so the output array should looke something like this:
#[#"John", #"Mister", #"Steve"];
How do I do this?
NSArray * arr = #[#"John Doe", #"Mister Appleseed", #"Steve"];
NSMutableArray * tmp = [NSMutableArray array];
for (NSString * s in arr)
{
NSArray * cmpnts = [s componentsSeparatedByString:#" "];
[tmp addObject:cmpnts[0]];
}
arr = tmp;
Check the following code:
NSMutableArray *finalArray = [[NSMutableArray alloc] init];
NSArray *sourceArray = #[#"John Doe", #"Mister Appleseed", #"Steve"];
for(NSString *str in sourceArray){
[finalArray addObject:[str componentsSepratedByString:#" "] objectAtIndex:0]];
}
You iterate over the array of names, split each at spaces (" "), and then grab the first object: the first name.
NSArray *nameArray = #[#"Name", #"Name Two"]; // original array
NSMutable *modifiedNameArray = [NSMutableArray new]; // new mutable array to add new names
for (NSString *fullName in nameArray) { // for loop
NSString *firstName = [[fullName componentsSeparatedByString:#" "] objectAtIndex:0]; // extract first name
[modifiedNameArray addObject:firstName]; // add first name to mutable array
}
You have to have two arrays, because you cannot mutate an array while it is being used in a loop (even if it's an NSMutableArray).

convert array format according to array character

I have array in this format
rows = [[NSArray alloc] initWithObjects:#"adam", #"alfred", #"ain", #"abdul", #"anastazja", #"angelica",
#"dennis" , #"deamon", #"destiny", #"dragon", #"dry", #"debug" #"drums",
#"Fredric", #"France", #"friends", #"family", #"fatish", #"funeral",
#"Mark", #"Madeline",
#"Nemesis", #"nemo", #"name",
#"Obama", #"Oprah", #"Omen", #"OMG OMG OMG", #"O-Zone", #"Ontario",
#"Zeus", #"Zebra", #"zed", nil];
But i need this in to following format
rows = #[#[#"adam", #"alfred", #"ain", #"abdul", #"anastazja", #"angelica"],
#[#"dennis" , #"deamon", #"destiny", #"dragon", #"dry", #"debug", #"drums"],
#[#"Fredric", #"France", #"friends", #"family", #"fatish", #"funeral"],
#[#"Mark", #"Madeline"],
#[#"Nemesis", #"nemo", #"name"],
#[#"Obama", #"Oprah", #"Omen", #"OMG OMG OMG", #"O-Zone", #"Ontario"],
#[#"Zeus", #"Zebra", #"zed"]];
Means that same starting character in to different dictionary
The easiest approach.
NSArray *rows = ...;
NSMutableDictionary *map = [NSMutableDictionary dictionary];
for (NSString *value in rows) {
NSString *firstLetter = [value substringToIndex:1];
if (!map[firstLetter]) {
map[firstLetter] = #[];
}
NSMutableArray *values = [map[firstLetter] mutableCopy];
[values addObject:value];
map[firstLetter] = values;
}
NSArray *finalRows = [map allValues];
Note that finalRows is not sorted.
If you want to sort your array by it's first letter, you can try this :
NSMutableArray *outputArray = [NSMutableArray new];
NSString *lastFirstLetter = nil;
for(NSString *value in rows) {
NSString *firstLetter = [[value substringToIndex:1] lowerString];
if(![lastFirstLetter isEqualToString:firstLetter]) {
lastFirstLetter = firstLetter;
[outputArray addObject:[NSMutableArray new]];
}
[[outputArray lastObject] addObject:value];
}
The idea is to iterate your input array and if the first letter of your word is different than the precedent, create a new array.

Merge arrays with its count in Objective C

I have an array like this
A = [#"aa",#"cc",#"bb",#"bb",#"cc",#"aa",#"cc"]
I need to convert it to
A = [#"x2 aa",#"x2 bb",#"x3 cc"]
Count the similar elements and create a new string, add it to new array. As shown here:
NSArray *aArray = #[#"aa", #"cc", #"bb", #"bb", #"cc", #"aa", #"cc"];
NSCountedSet *set = [NSCountedSet setWithArray:aArray];
NSMutableArray *countedArray = [NSMutableArray new];
for (NSString *str in set) {
[countedArray addObject:[NSString stringWithFormat:#"x%ld %#",[set countForObject:str], str]];
}
NSLog(#"%#",countedArray);
Output:
(
"x2 bb",
"x2 aa",
"x3 cc" )

NSDictionary adding multiple keys

May I know is that possible for NSDictionary or NSMutableDictionary to add in multiple same keys for different object? It is because of the API that written by the developers are accepting an array.
e.g:
NSArray *ids = #[#"xxx", #"yyy", #"zzz"];
NSMutableDictionary *args = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"someobject", #"somekey"];
I've defined an args and set of ids that picked by user and now I will loop the array.
for( NSString *getId in ids ){
[args setObject:getId forKey:#"ids[]"];
}
So ended up, the results come out are
"somekey" = "someobject", "ids[]" = "zzz";
Is that possible for me to get result as follows?
"somekey" = "someobject", "ids[]" = "xxx", "ids[]" = "yyy", "ids[]" = "zzz";
Please advise, thanks!
Yes it is possible
NSArray *arr = [[NSArray alloc]initWithObjects:#"xxx",#"yyy",#"zzz", nil];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Someobject", #"Somekey", nil];
for (int i = 0; i<[arr count]; i++) {
[dic setValue:[arr objectAtIndex:i] forKey:[NSString stringWithFormat:#"id[%d]", i]];
}
NSLog(#"dic %#",dic);
Use this code sure it would help you.
No, key is unique. But you can put an NSMutableArray in your NSDictionary, and store you values like key => array(x,y,...)
Instead of that you can create Dictionary like this.
YourDictionary = {
"somekey" = "someobject",
"ids[]" = (
"xxx",
"yyy",
"zzz"
)
}
Treat ids[] as an array
Hope this will solve your problem
No that's not the way a dictionary should be used. As peko said, just put your array in the dict:
[args setObject:ids forKey:#"ids"];

I've got strange output from 'componentsSeparatedByString' method of NSString

I want to store the array of NSDictionary to a file. So I write a function to convert from NSArray to NSString. But I got a very strange problem. Here is my code.
+ (NSArray *)arrayForString:(NSString*)dataString
{
NSArray* stringArray = [dataString componentsSeparatedByString:ROW_SEPARATOR];
NSLog(#"%#", stringArray);
NSMutableArray* dictionaryArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < [stringArray count]; i++)
{
NSString* string = [stringArray objectAtIndex:i];
NSLog(#"%#", string);
NSArray* subStrings = [string componentsSeparatedByString:COLUMN_SEPARATOR];
NSDictionary* dic = [[NSDictionary alloc] initWithObjectsAndKeys:[subStrings objectAtIndex:0], PHOTO_NAME, [NSNumber numberWithUnsignedInt:[[subStrings objectAtIndex:1] unsignedIntValue]], PHOTO_SEQ_NO, nil];
[dictionaryArray addObject:dic];
}
return dictionaryArray;
}
Here is the log:
2012-05-05 23:57:35.113 SoundRecognizer[147:707] (
"new Photo/0",
"new Photo/1"
)
2012-05-05 23:57:35.118 SoundRecognizer[147:707] new Photo/0
2012-05-05 23:57:35.123 SoundRecognizer[147:707] -[__NSCFString unsignedIntValue]: unrecognized selector sent to instance 0x1d18c0
How do I get a #"-" from this following array?!
2012-05-05 23:57:35.113 SoundRecognizer[147:707] (
"new Photo/0",
"new Photo/1"
)
NSString doesn't have an unsignedIntValue method. Use intValue instead. But I'm not sure of the point of all this - you can write an array of dictionaries straight to a file anyway (as long as they only contain property list types) using writeToFile: atomically:.

Resources