How to convert elements of NSMutableArray to NSString - iphone

I have 1 NSMutableArray and I want to convert whatever data in array will be in NSString.
tell me code for that. Array is nothing but object of NSMutableArray class.

If you want only the elements of array then you can try componentsJoinedByString:. This method returns all the elements with separator string without other formatting info.
[array componentsJoinedByString:#","];
Here "," is separator string.

It depends on how you want your string.
One approach could be iterate through array and convert each element of it.
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
[result appendString:[obj description]];
}
NSLog(#"The concatenated string is %#", result);
You can modify the above code based on item's class.
Below code will convert Array to string with commas and other information.
NSString * result = [array description];

It depends on how do you want to use that string.
One way to convert an object to a string is to call -description (or -descriptionWithLocale:) on that object. For NSArray (or NSMutableArray) -description method returns a string that represents the contents of the receiver, formatted as a property list. The result you'll get will also depend on how -description method implemented in objects in array.

Related

NSMutableArray is deleting all object with same string

I am using one NSMutableArray with same string object.
Here is the code
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:#"hello",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",nil];
NSObject *obj = [arr objectAtIndex:2];
[arr removeObject:obj];
NSLog(#"%#",arr);
When i try to remove 3rd object of array, its removing all object with "hi" string.
I am not getting why its happening.
my doubt is while removing object, NSMutableArray match string or address.
It's because you're using removeObject which removes all objects that are "equal" to the one you pass in. As per this Apple documentation:
This method uses indexOfObject: to locate matches and then removes
them by using removeObjectAtIndex:. Thus, matches are determined on
the basis of an object’s response to the isEqual: message. If the
array does not contain anObject, the method has no effect (although it
does incur the overhead of searching the contents).
You're seeing the effects of literal strings here where each of those #"hi" objects will turn out to be the same object just added many times.
What you really want to do is this:
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:#"hello",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",nil];
[arr removeObjectAtIndex:2];
NSLog(#"%#",arr);
Then you're specifically removing the object at index 2.
According to https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html
removeObject:
Removes all occurrences in the array of a given object.
which is exactly the behaviour you're seeing. If you want to remove the object at a particular position, you want removeObjectAtIndex:.
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:#"hello",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",nil];
NSUInteger obj = [arr indexOfObject:#"hi"]; //Returns the lowest integer of the specified object
[arr removeObjectAtIndex:obj]; //removes the object from the array
NSLog(#"%#",arr);

Strings from NSArray into annotation, mapkit

I am trying to populate the Title, and Subtitle using mapkit with the following lines of code.
The textItems array holds two strings.
NSArray *textItems = [searchString componentsSeparatedByString:#","];
addAnnotation =
[[AddressAnnotation alloc] initWithCoordinate:location
mTitle:[[textItems objectAtIndex:0] stringValue]
mSubTitle:[[textItems objectAtIndex:1] stringValue]];
The app stops when it reaches the 'addAnnotation'.
If I change mTitle:[[textItems objectAtIndex:0] stringValue] to mTitle:#"test" i.e. it works fine. When I debug I can see that the data in the textItems array is present.
Any ideas?
Thanks.
The componentsSeparatedByString method returns an array of NSString objects.
You are calling stringValue on those objects but stringValue applies to NSNumber objects--not NSString so you must be getting an "unrecognized selector" error.
Remove the calls to stringValue:
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location
mTitle:[textItems objectAtIndex:0]
mSubTitle:[textItems objectAtIndex:1]];
However, it would still be a good idea to check the count before accessing those indexes in the array and use a default value if the array returns only 0 or 1 objects.
I would set a breakpoint between the first and second line you posted. When you get there, go down to the console and type "po textItems" and "po [textItems count]". They will print the array and the number of objects in the array respectively. At the very least it's a check to make sure that you're getting the number of objects in the array you expect.

Adding string to an array

i wanted to know how to add strings into an array.
I used the following methods but it is showing null.
1) [arrData addObject:[NSString stringWithString:strlast]];
2) [arrData addObject:strlast];
Thanks in advance
You can't add anything to an NSArray once it's created. You need to use an NSMutableArray if you want to make changes to it.
Update: You may actually have two problems.
Using an NSArray instead of an NSMutableArray when mutability is needed.
Not initializing the array object (either kind). If arrData is nil, you can happily send as many messages as you want to nil. Nothing will happen.
If it is showing null (nil) you need to make sure you set arrData somewhere in your code before trying to addObject:.
arrData = [[NSMutableArray alloc] init];
Also strlast is a string so use your second example, the first example is pointless.
[arrData addObject:strlast];
Did you allocate an array and assign it to arrData?
Try:
NSMutableArray *arrData = [NSMutableArray array];
NSString *string = #"My string";
[arrData addObject:string];
NSLog(#"%#", [arrData objectAtIndex:0]); //logs "My string"
If you're using a non-mutable array, you can also use arrayByAddingObject:
arrData = [arrData arrayByAddingObject: strlast];
but a mutable array is probably a better idea.

How to add the static data into the Dynamic array in iPhone?

I want to add the static contents into the Mutable Array. I have parsed the data using XML parsing and stored the dynamic data into the mutable array. Now i want to add the static string into that Mutable Array. So how can i add that?
For Eg:
In my dynamic Array of content is,
{
first,
third,
fourth,
fifth.
}
I want to add the static String into the 1st index of array. Because my requirement is, displayed the static content into the 1st position.
Expected output:
{
first,
second, //Add the static data into the First index of Array
third,
fourth,
fifth.
}
So how can i do this?
Thanks.
If I understand what you mean. You can use:
[NSMutableArray insertObject:atIndex:]
Supposing myArray is the instance of NSMutableArray:
[myArray insertObject:#"second" atIndex:1];
instead of NSString you can insert every object you want.
After creating NSMutableArray from XML parsing you can implement like this,
suppoose In XMLArray you having your xml data now you want to insert static date at any location,
NSMutableArray *tempArray=[NSMutable array];
for(int i=0;i<[XMLArray count];i++)
{
if(i==1)//accrding to you
[tempArray addObject:yourStaticData];//yourStaticData is object having your data.
else
[tempArray addObject:[XMLArray objectAtIndex:i]];
}
//use tempArray according to you.

Looping through values in NSDictionary throws fault

I have a plist file that I am loading into an NSDictionary *setDictionary that contains a set of fields, pictureDesc1, pictureDesc2, etc.
Once the NSDictionary is loaded, I can retrieve a value using
[setDictionary objectForKey:#"pictureDesc1"];
But I cannot do a loop like this:
for (int i=1; i<=numberOfPictures; i++) {
NSString *keyName = [NSString stringWithFormat:#"pictureDesc%d",i];
NSString *description = [NSString stringWithFormat:#"%#", [setDictionary objectForKey:keyName]];
}
Is there a different way to dynamically call keys from within an NSDictionary?
If you keys fit a numerical pattern, then it would be easier and more robust to simply store them in a array. You could even store an array as a value in the dictionary with the key of "pictureDescription".
Then when you wanted to loop through them just use a simple numerical loop:
NSArray *pictArray=[setDictionary valueForKey:#"pictureDescription"];
NSString *description;
for (i=0;i<[pictArray count];i++){
description=[pictArray objectAtIndex:i];
//... do whatever
}
If you find yourself shoehorning the functionality of an array into a dictionary or vice versa you probably should back out and just use an array or dictionary in the first place. When using a dictionary, just use an enumerator to step through the values.