I would like to loop through an array strings and add them to an NSString in the following way:
NSMutableArray *emailsArray = [[NSMutableArray alloc] initWithObjects:#"One", #"Two", #"Three", nil];
for (id email in emailsArray {
NSString *emails = ??;
}
So the final NSString should be the following:
NSString *emails = #"One|Two|Three";
Use [emailsArray componentsJoinedByString:#"|"] for this.
Sample:
NSMutableArray *emailsArray = [[NSMutableArray alloc] initWithObjects:#"One", #"Two", #"Three", nil];
NSString *emails = [emailsArray componentsJoinedByString:#"|"];
Here you'll have emails = #"One|Two|Three".
Related
This question already has answers here:
Compare 2 nsmutablearray and get different object to third array in ios
(4 answers)
Closed 9 years ago.
I have two MutableArray values like.
NSMutableArray *array=[[NSMutableArray alloc]initWithObjects:#"Apple", #"Orange", #"Grapes", #"Banana", nil];
NSMutableArray *array1=[[NSMutableArray alloc]initWithObjects:#"Apple", #"Orange", #"Grapes", nil];
Now i have to compare that two Mutable arrays and display that unmatched object "Banana" into one string.
I am fresher to iOS so, anybody would send me the code for that problem.
Thanks in Advance.
As others have suggest, NSSet is probably your best bet. However, given that *array is mutable, you could simply remove the objects from it contained in *array1
NSMutableArray *array=[[NSMutableArray alloc]initWithObjects:#"Apple", #"Orange", #"Grapes", #"Banana", nil];
NSMutableArray *array1=[[NSMutableArray alloc]initWithObjects:#"Apple", #"Orange", #"Grapes", nil];
[array removeObjectsInArray:array1];
NSLog(#"array: %#", array); // array: ( Banana )
// if you require result as a string
NSString *objectsAsString = [array componentsJoinedByString:#", "];
NSLog(#"objects as string: %#", objectsAsString); // objects as string: Banana
for(int i=0;i<[array count];i++)
{
NSString *str1 = [array objectAtIndex:i];
for(int j=0;j<[array1 count];j++)
{
NSString *str2 = [array1 objectAtIndex:j];
if([str1 isEqualToString:str2])
{
//do something which you want i.e add the values to some other array
}
}
}
You should probably use NSSet for this purpose
NSSet *set1 = [NSSet setWithObjects:#"a", #"s", #"d", #"f", nil];
NSSet *set2 = [NSSet setWithObjects:#"a", #"s", nil];
NSMutableSet *notInSet1 = [NSMutableSet setWithSet:set2];
[notInSet1 minusSet:set1];
NSMutableSet *notInSet2 = [NSMutableSet setWithSet:set1];
[notInSet2 minusSet:set2];
NSMutableSet *symmetricDifference = [NSMutableSet setWithSet:notInSet1];
[symmetricDifference unionSet:notInSet2];
NSArray *array1 = [[NSArray alloc] initWithObjects:#"a",#"b",#"c",nil];
NSArray *array2 = [[NSArray alloc] initWithObjects:#"a",#"d",#"c",nil];
NSMutableArray *ary_result = [[NSMutableArray alloc] init];
NSMutableArray *ary_resultUnmatched = [[NSMutableArray alloc] init];
for(int i = 0;i<[array1 count];i++)
{
for(int j= 0;j<[array2 count];j++)
{
if([[array1 objectAtIndex:i] isEqualToString:[array2 objectAtIndex:j]])
{
[ary_result addObject:[array1 objectAtIndex:i]];
} else {
[ary_resultUnmatched addObject:[array1 objectAtIndex:i]];
}
}
}
NSLog(#"%#",ary_result);//it will print a,c
NSLog(#"%#",ary_resultUnmatched);//it will print b,d
so in else condition you'll have your un matched values
I need to convert this "5?8?519223cef9cee4df999436c5e8f3e96a?EVAL_TIME?60?2013-03-21" string into dictionary. Separated by "?"
Dictionary would be some thing like
{
sometext1 = "5",
sometext2 = "8",
sometext3 = "519223cef9cee4df999436c5e8f3e96a",
sometext4 = "EVAL_TIME",
sometext5 = "60",
sometext6 = "2013-03-21"
}
Thank you .
Break the string to smaller strings and loop for them.
This is the way
NSArray *objects = [inputString componentsSeparatedByString:#"?"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
int i = 1;
for (NSString *str in objects)
{
[dict setObject:str forKey:[NSString stringWithFormat:#"sometext%d", i++]];
}
Try
NSString *string = #"5?8?3519223cef9cee4df999436c5e8f3e96a?EVAL_TIME?60?2013-03-21";
NSArray *stringComponents = [string componentsSeparatedByString:#"?"];
//This is very risky, your code is at the mercy of the input string
NSArray *keys = #[#"cid",#"avid",#"sid",#"TLicense",#"LLicense",#"date"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (int idx = 0; idx<[stringComponents count]; idx++) {
NSString *value = stringComponents[idx];
NSString *key = keys[idx];
[dictionary setObject:value forKey:key];
}
EDIT: More optimized
NSString *string = #"5?8?3519223cef9cee4df999436c5e8f3e96a?EVAL_TIME?60?2013-03-21";
NSArray *stringComponents = [string componentsSeparatedByString:#"?"];
NSArray *keys = #[#"cid",#"avid",#"sid",#"TLicense",#"LLicense",#"date"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjects:stringComponents forKeys:keys];
first separate the string into several arrays by '?'.
then add the string in you dictionary.
sth like this:
NSString *str = #"5?8?519223cef9cee4df999436c5e8f3e96a?EVAL_TIME?60?2013-03-21";
NSArray *valueArray = [str componentsSeparatedByString:#"?"];
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
for (int i = 0; i <[valueArray count]; i ++) {
[keyArray addObject:[NSString stringWithFormat:#"sometext%d",i+1]];
}
NSDictionary *dic = [[NSDictionary alloc] initWithObjects:valueArray forKeys:keyArray];
For the future: If you were to store your data in JSON format (closer to what you have anyway), it'll be much easier to deal with and transfer between systems. You can easily read it...using NSJSONSerialization
I use NSString to append strings, but when I release the "cacheStr" NSString, the iphone simulator crash.
Where should I put the release code ?
init code : I use three NSString to append content of the dataArray.
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
NSString *cacheStr = [[NSString alloc] init];
NSString *tmpStr = [[NSString alloc] init];
NSString *notiyStr = [[NSString alloc] initWithFormat:#"This is a test message!"];
dataArray = [NSArray arrayWithObjects:
#"currency1.png",
#"currency2.png",
#"currency3.png",
#"currency4.png",
#"currency5.png",
#"xxxxxx",
#"currency1.png",
#"currency2.png",
#"currency3.png",
#"currency4.png",
#"currency5.png",
#"xxxxxx",
nil];
append string code : use for loop to append strings.
int isFailed = 0;
int countOfDataArray = [dataArray count];
if (!isFailed) {
for (int i=0; i < countOfDataArray; i++) {
if ([[dataArray objectAtIndex:i] isEqualToString:#"xxxxxx"]) {
tmpStr = [cacheStr stringByAppendingFormat:#"%#\n", [dataArray objectAtIndex:i]];
}
else {
tmpStr = [cacheStr stringByAppendingFormat:#"value %d : %#\n", i+1, [dataArray objectAtIndex:i]];
}
cacheStr = [tmpStr copy];
[tmpStr release];
}
}
tmpStr = [notiyStr stringByAppendingString:cacheStr];
release code : when I add [cacheStr release], the simulator will crash...
[dataArray release];
[notiyStr release];
// [cacheStr release]; /* crash ... */
Thanks!
its more simple to declare a NSString like this
NSString *tmpStr = #"This is a test message!";
then no need to release it,when you dont use alloc
This
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
dataArray = [NSArray arrayWithObjects:
#"currency1.png",
#"currency2.png",
#"currency3.png",
#"currency4.png",
#"currency5.png",
#"xxxxxx",
#"currency1.png",
#"currency2.png",
#"currency3.png",
#"currency4.png",
#"currency5.png",
#"xxxxxx",
nil];
is strange! You are allocating a mutable array and assigning the pointer pointing to that array to an array which you do not own. It is probably already (auto)released when you try to use it. Therefore int countOfDataArray = [dataArray count]; will result in countOfDataArray being zero. Therefore cacheStr is never set. Still it shouldn't crash on releasing the cacheStr.
Change it to:
NSMutableArray *dataArray = [[NSMutableArray alloc] initWithObjects: #"currency1.png",
#"currency2.png",
#"currency3.png",
#"currency4.png",
#"currency5.png",
#"xxxxxx",
#"currency1.png",
#"currency2.png",
#"currency3.png",
#"currency4.png",
#"currency5.png",
#"xxxxxx",
nil];
and you should be fine.
dont use [NSString alloc] init. To use NSString value=#"Message"; No need to release it.
simple question, I need to structure data in the following format:
UserID:
1 {
Name = Bob
Surname = Hope
}
2 {
...
I can used an NSMutableDictionary to add a single layer with keys, but I am unable to create a child associate with a certain key.
I have tried creating a mutable dictionary and assigning that to a key:
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
NSArray *keys = [[NSArray alloc] initWithObjects:#"Name", #"Surname", nil];
NSArray *details = [[NSArray alloc] initWithObjects:#"Bob",#"Hope", nil];
NSDictionary *person = [[NSMutableDictionary alloc] initWithObjects:details forKeys:keys];
[dic setValue:#"1" forKey:#"id"];
[[dic objectForKey:#"id"] setDictionary: person];
I think it is better to use an array of dictionaries, each dictionary representing a user.
NSMutableArray *users= [[NSMutableArray alloc] init];
NSArray *keys = [[NSArray alloc] initWithObjects:#"Name", #"Surname", nil];
NSArray *details = [[NSArray alloc] initWithObjects:#"Bob",#"Hope", nil];
NSDictionary *person = [[NSMutableDictionary alloc] initWithObjects:details forKeys:keys];
[users addObject:person];
u can also set the objects for dictionary in this way
[dic setObject:person forKey:#"id"];
How Can we Store An group OF NSString Objects to an Single Array.....
starting from index = 0;
NSArray *array = [NSArray arrayWithObjects:string1, string2, string3, nil];
or
NSMutableArray *array = [NSMutableArray array];
[array addObject:string1];
[array addObject:string2];
pick one, or improve your question if I've misunderstood you.
An example:
NSArray *myStringArray = [NSArray arrayWithObjects:#"String", #"Another String", #"Last string", nil];
NSArray *myStringArray = [NSArray arrayWithObjects: string1, string2, string3, ..., stringn, nil];