iOS How Array objects separated by commas convert into string? - ios

I have an array named as child_list like child_list: "9,8,21,22,20,24,25", take it in NSString at index 0 when I convert it into string its work but when i use NSMutableArray it crashed, I don't know why.
My code is here:
if([theDictionary objectForKey:#"child_list"])
{
NSString *str = [[theDictionary objectForKey:#"child_list"]objectAtIndex:0];
NSLog(#"Child List %#",str);
_child_list = [[NSMutableArray alloc ]initWithObjects: [str componentsSeparatedByString:#","], nil];
}
When break point come on str it show result when it comes on _child_list it crashed.

Use initWithArray method while allocating _child_list NSMutableArray
if([theDictionary objectForKey:#"child_list"])
{
NSString *str = [[theDictionary objectForKey:#"child_list"]objectAtIndex:0];
NSLog(#"Child List %#",str);
_child_list = [[NSMutableArray alloc ] initWithArray:[str componentsSeparatedByString:#","]];
}

Please share what your log returns and the dictionary you used..
The code [str componentsSeparatedByString:#","] returns an Array. So, when you're allocating a mutable array, you're creating array of array... Instead using initWithObjects, use initWithArray method and pass [str componentsSeparatedByString:#","] to it.

Related

How can I change substring(s) in an array of strings in Objective-C?

I have a weird error with decoding & sign so its always shown as &, so && will be &&, a&b is shown as a&b etc.
I have those weird characters in my array of strings sometimes, so I want to remove them. To be more clear, if I have an array like this:
"str1", "a&b", "12345", "d&&emo&"
I want to it to be:
"str1", "a&b", "12345", "d&&emo&"
So all & should be changed into just &.
This is the code I tried, but it didn't help. Elements are not changing their values:
for (int i = 0; i <= self.array-1; i++)
{
NSLog(#"%#", self.array[i]);
[self.array[i] stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
NSLog(#"%#", self.array[i]);
}
I am new to Objective-C.
NSString instances are immutable, which means you can't change them once they are created. The stringByReplacingOccurencesOfString:withString: method actually returns a new NSString instance. You could use NSMutableString, or alternatively, you can create a new array containing the replacement strings, e.g.:
NSMutableArray *newArray = [NSMutableArray array];
for (NSString *input in self.array)
{
NSString *replacement = [input stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
[newArray addObject:replacement];
}
self.array = newArray;

NSString in NSMutableArray isn't the same on myString

I have a problem with NSString and NSMutableArray.
When I retrieve a string from mutable array ,it has more white spaces, and I don't understand why it's happening.
I'll explain, I have an array and populate it by query (using sqlite3):
NSMutableArray *fileNameAttached = [[NSMutableArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
and it's like this:
<__NSArrayM 0x15e233980>(
<__NSArrayM 0x15e26afd0>(
Allegato N. 1
)
)
When I retrieve the string Allegato N.1 with this code:
NSString *test = [NSString stringWithFormat:#"%#", [fileNameAttached objectAtIndex:0] ];
string test is like this:
(
"Allegato N. 1"
)
Why my string isn't only:
Allegato N.1
When I put it in a label it's not correct because contains () and white spaces.
The query for DB is:
NSString *query = [NSString stringWithFormat:#"SELECT fileName FROM Attach WHERE ID = '%#';",ticketID];
and works perfectly,in fact when I populate tableview cell it's OK. But I don't understand because my string test contains 3 line with more white spaces.
Please help Me.
Thank you and sorry for my english.
In your log there are two arrays.
(
(
Allegato N. 1
)
)
There are two round brackets thats means two array.
You are fetching data like NSString *test = [NSString stringWithFormat:#"%#", [fileNameAttached objectAtIndex:0] ];
That means first object of outer array so it is another array. so you need to do something like
NSArray *temp =[fileNameAttached objectAtIndex:0];
NSString *test = [NSString stringWithFormat:#"%#", [temp objectAtIndex:0] ];
I think you are using appcoda's DBManager class to do this. If it is so then you everytime got two array when load query.
Hope this will help :)

NSArray componentsJoinedByString with condition

For example, I have an array
NSArray *array = [NSArray arrayWithObjects:#"one<true>",#"two<false>",#"three<true>",#"four<false>"];
I want the output as NSString using componentsJoinedByString
"one<true>'&'two<false>'&&'three<true>'&'four<false>'&&'"
That is, in an array if an object contain a value <true> it should be joined by '&' and if an array value contain a value <false> it should be joined by '&&'.
I know, I can run a for loop and using if condition, I can achieve the output. But I'm looking for other efficient way to implement this.
Thanks in advance.
It may be help
you can use stringByReplacingOccurrencesOfString
for ex:
yourArrayJoinString=[yourArrayJoinString stringByReplacingOccurrencesOfString:#"<true>" withString:#"<true>'&'"];
yourArrayJoinString=[yourArrayJoinString stringByReplacingOccurrencesOfString:#"<false>" withString:#"<false>'&&'"];
Try with this
NSArray *array = [NSArray arrayWithObjects:#"one<true>",#"two<false>",#"three<true>",#"four<false>" ,nil];
NSString *str = [array componentsJoinedByString:#""];
str =[str stringByReplacingOccurrencesOfString:#"<false>" withString:#"<false>'&&'"];
str =[str stringByReplacingOccurrencesOfString:#"<true>" withString:#"<true>'&'"];

How to remove conflicting return type?

I am dividing string and and storing it in splitArray and want to return it.
But I am getting conflicting array on the first line
- (NSArray *)subdividingString:(NSString *)string {
NSArray *splitArray = [string componentsSeparatedByString:#" "];
return splitArray;
}
First: there is nothing wrong with the code, but you are most likely having another issue (e.g. where you call subdividingString:).
Second: You shouldn't introduce a method that is exactly doing what another one is doing already. Just use
NSString *mystring = #"some string";
NSArray *chunks = [mystring componentsSeparatedByString:#" "];

How to add String from one array to another

I can't seem to append a string from arrayOne into arrayTwo. It's a very simple problem and I have Googled around and have tried different examples that I found below. Does anyone see the obvious issue here?
[_arrayTwo addObject:arrayOne[i]]; // int i
[_arrayTwo addObject:[arrayOne objectAtIndex:i]]; // int i
[_arrayTwo addObject:#"Test"]; // I can't even add a literal string
NSString *tempString = [arrayOne objectAtIndex:i];
[arrayTwo addObject:tempString];
NSLog Output:
NSLog(#"%#", _imageArray); // Result is "(null)"
Additional Notes:
arrayOne is healthy (contains NSString values)
Both arrays are NSMutableArray (declared as properties in the .h)
I believe _imageArray is nil
Try adding in
_imageArray = [[NSMutableArray alloc] init];
In your init method call.

Resources