Splitting NSString not working - iphone

I have seen may topics that talk about that but none of them has been useful for me. I am trying to parse GEORSS and the coordinates are given me with the following format:
<georss:point>4613618.31000115 676474.87007986</georss:point>
So I have tried to split it into an NSArray and then assign them to the corresponding XML key, but it always crash with that error:
2013-04-02 12:33:11.234 GeoRss1[2125:c07] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
* First throw call stack:
(0x1959012 0x1275e7e 0x18fb0b4 0x19d23a8 0x3f48 0xd55a37 0x1c96600 0x1ca1a2b 0x1ca0f07 0xd53e02 0x34b8 0xd7b589 0xd79652 0xd7a89a 0xd7960d 0xd79785 0xcc6a68 0x14a1911 0x14a0bb3 0x14decda 0x18fb8fd 0x14df35c 0x14df2d5 0x13c9250 0x18dcf3f 0x18dc96f 0x18ff734 0x18fef44 0x18fee1b 0x18b37e3 0x18b3668 0x1b9ffc 0x1ddd 0x1d05)
libc++abi.dylib: terminate called throwing an exception
(lldb)
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
}else if([elemAct isEqualToString:#"georss:point"]){
componentes = [[string componentsSeparatedByString:#" "] mutableCopy];
if ( componentes != nil){
[xmlLat appendString:componentes[0]];
[xmlLon appendString:componentes[1]];
}
...
...
In the header of that file I have declares both variables correctly and the synthesized them:
...
#property (strong) NSMutableString *xmlLat;
#property (strong) NSMutableString *xmlLon;
...
Thank you everyone for advanced, you already have helped me with some responses in other topics!

I tried following code :
NSString *abc = #"4613618.31000115 676474.87007986";
NSArray *componentes = [[abc componentsSeparatedByString:#" "] mutableCopy];
NSLog(#"componentes : %#",componentes);
NSMutableString *xmlLat = [[NSMutableString alloc]initWithString:#""];
NSMutableString *xmlLon = [[NSMutableString alloc]initWithString:#""];
[xmlLat appendString:componentes[0]];
[xmlLon appendString:componentes[1]];
NSLog(#"xmlLat : %#",xmlLat);
NSLog(#"xmlLon : %#",xmlLon);
It worked perfectly for me. I thin the problem is you are not getting proper data in your string.

Related

application crashes when appendString for NSMutableString is used

I'm new to objective-c and I decided to start by working through Stanford's CS193s 2010F Session's lectures/assignments.
I was working on the second assignment, and I was stuck when I had to return a NSString that combines(concatenates) every strings inside NSMutableArray. (The MutableArray consists only NSString at its each indices)
My approach was to use a for loop to pass through the MutableArray's indicies(in the code below, the MutableArray is 'anExpression' with type 'id'). I declared a NSMutableString and added NSString at each indices of 'anExpression' array. Here is the code:
+ (NSString *)descriptionOfExpression:(id)anExpression{
NSMutableString *result = [NSMutableString string];
for (int i = 0;i<[anExpression count];i++){
[result appendString:[anExpression objectAtIndex:i]];
}
return result;
}
However, at
[result appendString:[anExpression objectAtIndex:i]];
xcode crashes with following error statements:
2012-07-17 01:44:51.014 Calculator[9470:f803] -[__NSCFNumber length]: unrecognized selector sent to instance 0x68732c0
2012-07-17 01:44:51.015 Calculator[9470:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x68732c0'
*** First throw call stack:
(0x13ca022 0x155bcd6 0x13cbcbd 0x1330ed0 0x1330cb2 0x12d2d18 0x13460d7 0x1397a8d 0x3a50 0x27ed 0x13cbe99 0x1714e 0x170e6 0xbdade 0xbdfa7 0xbd266 0x3c3c0 0x3c5e6 0x22dc4 0x16634 0x12b4ef5 0x139e195 0x1302ff2 0x13018da 0x1300d84 0x1300c9b 0x12b37d8 0x12b388a 0x14626 0x1ed2 0x1e45 0x1)
terminate called throwing an exception
I looked through apple's developer's document, saw 'NSString stringWithFormat:' method, and decided to use this method instead:
+ (NSString *)descriptionOfExpression:(id)anExpression{
NSMutableString *result = [NSMutableString string];
for (int i = 0;i<[anExpression count];i++){
[result appendString:[NSString stringWithFormat:#"%#",[anExpression objectAtIndex:i]]];
}
return result;
}
and it works now.
now I'm confused why second code works but the first doesn't.
I thought appending string only fails(and crashes) when it's passed a nil...
Is there something I'm missing?
Thank you in advance :)
It looks like the contents of anExpression are instances of NSNumber instead of NSString. The error you are getting is a hint as to how appendString works; it's first step is obviously to ask the passed "string" how long it is (presumably so it can allocate enough memory). This is obviously not a method on NSNumber (hence the crash) and stringWithFormat is designed to do type-checking and be more flexible.
I suspect you could also use stringValue just as well:
[result appendString:[[anExpression objectAtIndex:i] stringValue]];
+ (NSString *)descriptionOfExpression:(id)anExpression{
NSMutableString *result = [NSMutableString string];
for (int i = 0;i<[anExpression count];i++) {
[result appendString:[[anExpression objectAtIndex:i] stringValue]];
}
return result;
}
Converting it into string would help you!!
Use this function:
+ (NSString *)descriptionOfExpression:(id)anExpression
{
NSMutableString *result = [[NSMutableString alloc] init];
for (int i = 0;i<[anExpression count];i++)
{
NSString *str = [NSString stringWithFormat:#"%#",[anExpression objectAtIndex:i]];
if(str)
{
[result appendString:str];
}
//OR
//[result appendFormat:[NSString stringWithFormat:#"%#",[anExpression objectAtIndex:i]]];
}
return result;
}

NSMutableString stringWithString:NSString not working for me

This piece of code below is causing my app to crash
EDIT
#interface termsAndConditions : NSObject
{
NSMutableString *titleText;
NSMutableString *bodyText;
NSMutableArray *arrayBodyText;
}
#property (nonatomic, copy) NSMutableString *titleText;
#property (nonatomic, copy) NSMutableString *bodyText;
*EDIT*
else if ([[self.arrayBodyText objectAtIndex:x] isKindOfClass:[NSString class]])
{
if (x == 0)
{
self.bodyText=[NSMutableString stringWithString:[self.arrayBodyText
objectAtIndex:x]];
}
else
{
[self.bodyText appendString:[self.arrayBodyText objectAtIndex:x] ];
}
the arrayBodyText is an array of NSString which I got from a dictionary and that I want to join them altogether in 1 NSMutableString.
When the app crashes it gives the message :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'
* Call stack at first throw:
Basically I need help to read this array of NSStrings into 1 NSMutableString.
Thanks
-Code
Do this:
self.bodyText = [[[self.arrayBodyText componentsJoinedByString:#""] mutableCopy] autorelease];

Splitting an NSString

I have the following code which fetches a string from an array of strings and splits the string into 2 parts.
NSString *temp = [tableViewData objectAtIndex:indexPath.row];
NSArray *tempArray = [temp componentsSeparatedByString: #"_"];
cell.textLabel.text = [tempArray objectAtIndex:1];
and the String which is added is as follows
newTitle = [NSString stringWithFormat:#"%d_%#",[Daily_QuoteViewController counter],title];
[plistArray addObject:newTitle];
[plistArray writeToFile:filepath atomically: YES];
Which is adding an index and a string.
I am trying to then split that string in the first code section. but trying to access the second part of the string at index 1 gives an out of bounds error.
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
What is the best way to split the string in order to use both parts of the string
split firstPart_secondPart
Thanks in advance.
This seems fine to me. Can you print out NSString temp? NSLog(#"%#", temp);

Parsing XML not working, why?

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
nestedChannels = [ [ NSMutableArray alloc ] init ];
....
}
- (void)parser:(NSXMLParser *)parser didStartElement....
{
Channel *channel = [ [ Channel alloc ] init ];
[ nestedChannels addObject:channel ];
....
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string....
{
Channel *channel = [ nestedChannels lastObject ];
channel.thumbnail = string;
....
}
#interface Channel : NSObject {
NSMutableString *thumbnail;
}
#property (nonatomic, retain) NSMutableString *thumbnail;
Error: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFString thumbnail]: unrecognized selector sent to instance 0x381c350'
It's like is not able to recognize the type of the object. Am I missing something
**nestedChannels is a NSMutableArray*
Seems like this is causing the problem.
channel.thumbnail = string;
What type is thumbnail in channel and what mutators are available?
To me it looks like it is trying to set string to thumbnail but there's no setter that accepts string on thumbnail. Is thumbnail NSString?
It's unclear what you're asking; are these different methods? Your code formatting is a little odd. One thing to note is that, if the second snippet is indeed a method (though there are no brackets), you need to append the data passed in, as it may not be a complete element:
if (channel.name == nil) channel.name = [NSMutableString string];
[channel.name appendString: string];

iPhone, show NSMutable Array containing NSStrings in UIPickerView

I have a method with the following code:
NSMutableArray *pickerArray = [[NSMutableArray alloc] init];
int i;
for(i = 1; i <= 7; i++) {
NSString *myString = [NSString stringWithFormat:#"%#", i];
[pickerArray addObject:myString];
}
for(i = 1; i <= 7; i++) {
NSString *fieldName = [[NSString alloc] initWithFormat:#"column%d", i];
[self setValue:pickerArray forKey:fieldName]; // setValue or initWithArray ???
[fieldName release];
[pickerArray release];
}
srandom(time(NULL));
When I build the application everything builds correctly but it crashes on start in the console i get the following error:
* -[NSCFString superview]: unrecognized selector sent to instance 0x380da90
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString superview]: unrecognized selector sent to instance 0x380da90'
If instead of using an array containing strings I use UIImageView containing UIImages then everything works correctly...
I only would like to populate my picker with an array of numbers from 1 to 50...
Any help would be really appreciated... this thing is driving me mad :)
I don't think you want [myString release]; in your first for loop as the string you create is auto-released (rule of thumb, anything that is created without alloc, init, or new is auto-released)
Problem solved.... was something to do with the number of element in the pickerview, nothing to do with the method itself! Thanks anyway!