stringWithFormat and NSArray - objective-c

I have an NSArray with objects :
NSArray *array = [NSArray arrayWithObjects:#"Saturday",#"Sunday",#"Monday",#"tuesday",#"Wednesday",#"thursday",nil];
and some times like this :
array = [NSArray arrayWithObjects:#"Tuesday",#"Monday",nil];
I would like to have a string like this :
dinner : sunday, monday,tusday, wednesday, thurs...
or
dinner : tuesday, monday.
How i can i do this with stringWithFormat?
thanks

Use NSArray's componentsJoinedByString:.
NSArray *array = [NSArray arrayWithObjects:#"Saturday",#"Sunday",#"Monday",#"tuesday",#"Wednesday",#"thursday",nil];
NSString *string = [NSString stringWithFormat:#"dinner: %#", [array componentsJoinedByString:#", "]];

NSString *string = [NSString stringWithFormat:#"Dinner: %#",[array componentsJoinedByString:#","]];

Related

How to get the date and title and description alone from this parsing result

How to get the date and title and description alone from this parsing result:
title = "Inter-views 29/03/2017 random description here (its in arabic);
title = " \U0627\U062e\U0628\U0627\U0631 \U0627\U0644\U0635\U0628\U0627\U062d 14/04/2017";
That's the parsing from same api but different (didSelectRowAtIndexPath).
I'm currently using this code but as I notified the parsing is different. So I cannot use static logic.
Code:
NSString *myString = [item objectForKey:#"title"];
NSMutableArray *myArray = [[NSMutableArray alloc]
initWithArray:[myString componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#" "]]];
NSString *Category = #"";
if(myArray.count>=1){
Category = [myArray objectAtIndex:0];
[myArray removeObjectAtIndex:0];
}
NSString *Date = #"";
if(myArray.count>=1) {
Date = [myArray objectAtIndex:0];
[myArray removeObjectAtIndex:0];
}
cell.dateLabel.text = [[NSString alloc] initWithFormat:#"%#", [Utils dateTransform:[Date stringByReplacingOccurrencesOfString:#" " withString:#""] FromFormat:#"dd-MM-yyyy" ToFormat:#"dd-MMMM-yyyy"]];
NSString *Title = #"";
for (NSString *word in myArray) {
Title = [Title stringByAppendingString:word];
Title = [Title stringByAppendingString:#" "];
}
cell.titleAndDescLabel.text =[NSString stringWithFormat:#"%# %#", Category,Title];
Your have to use for loop to get value
for (NSInteger i=0; i<[[jsonObject objectForKey:#"yourMainKey"]count]; i++)
{
NSString *strname =[[[jsonObject objectForKey:#"yourMainKey"]objectAtIndex:i]objectForKey:#"date"];
NSLog(#"strname==%#, strname);
NSString *str title =[[[jsonObject objectForKey:#"yourMainKey"]objectAtIndex:i]objectForKey:#"title"];
NSLog(#"strtitle ==%#, strtitle);
NSString *str description =[[[jsonObject objectForKey:#"yourMainKey"]objectAtIndex:i]objectForKey:#"description"];
NSLog(#"description ==%#, description);
}

[mutableArray initWithArray: [str componentsSeparatedByString:#" "]] compile is right,but encounter error while running

[mutableArray initWithArray: [str componentsSeparatedByString:#" "]];
componentsSeparatedByString returns a NSArray *, and initWithArray accepts an NSArray *.
However, when I run this code there is some error. What's the issue?
You forgot the call to alloc:
// Assuming:
NSMutableArray *mutableArray;
// Then:
mutableArray = [[NSMutableArray alloc] initWithArray:[str componentsSeparatedByString:#" "]];
Alternatively:
mutableArray = [[str componentsSeparatedByString:#" "] mutableCopy];

How can I convert the values is an NSDictionary into an NSString?

I have a method in Objective-c that takes in an NSDictionary and returns the values as a space-separated NSString. This application is cross-platform, and as such I cannot use fast enumeration. This is what I have so far, followed by the output (which shows that the String is never created):
-(NSString *)stringValuesFromDict:(NSDictionary *map)
{
NSArray *values = [map allValues];
NSString *params = [NSString string];
NSLog(#"values length: %d", [values count]);
NSLog(#"values = %#", [values description]);
for (int i = 0; i < [values count]; i++)
{
[params stringByAppendingString:[values objectAtIndex:i]];
[params stringByAppendingString:#" "];
}
NSLog(#"params = %#", params);
return params;
}
The NSDictionary:
{"arg1"="monkey"}
The output:
values length: 1
values = (
monkey
)
params =
What am I doing wrong? How can I get params to be set to monkey?
If I read the question correctly, all you need is
[[dict allValues] componentsJoinedByString:#" "]
You need a mutable string. Change this:
NSString *params = [NSString string];
to this:
NSMutableString *params = [NSMutableString string];
Then change these:
[params stringByAppendingString:[values objectAtIndex:i]];
[params stringByAppendingString:#" "];
to these:
[params appendString:[values objectAtIndex:i]];
[params appendString:#" "];
stringByAppendingString: returns a new string. So you would have to do something like this:
params = [params stringByAppendingString:[values objectAtIndex:i]];
But the disadvantage is, that every time a new string is created, which is wasting memory
You should propably use a NSMutableString instead. And then you can just call
[params appendString:[values objectAtIndex:i]];

Copying NSDictionary to NSArray

I have a variable declared as NSDictionary. How can I copy it to an extern const NSArray variable? For example:
NSMutableDictionary *selectedItem = [self.dataArray objectAtIndex:indexPath.row];
[selectedItem setObject:[NSNumber numberWithBool:targetCustomCell.checked]
forKey:#"checked"];
allKeys returns an NSArray containing all the keys. allValues returns an NSArray containing all the values.
NSArray *keys = [myDict allKeys];
NSArray *values = [myDict allValues];

Join an Array in Objective-C

I'm looking for a method of turning a NSMutableArray into a string. Is there anything on a par with this Ruby array method?
>> array1 = [1, 2, 3]
>> array1.join(',')
=> "1,2,3"
Cheers!
NSArray *array1 = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];
NSString *joinedString = [array1 componentsJoinedByString:#","];
componentsJoinedByString: will join the components in the array by the specified string and return a string representation of the array.
The method you are looking for is componentsJoinedByString.
NSArray *a = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];//returns a pointer to NSArray
NSString *b = [a componentsJoinedByString:#","];//returns a pointer to NSString
NSLog(#"%#", b); // Will output 1,2,3
NSArray class reference:
NSArray *pathArray = [NSArray arrayWithObjects:#"here",
#"be", #"dragons", nil];
NSLog(#"%#",
[pathArray componentsJoinedByString:#" "]);