Re: A Return NSString Problem
Re: A Return NSString Problem
- Subject: Re: A Return NSString Problem
- From: Ken Thomases <email@hidden>
- Date: Sun, 29 May 2011 22:33:44 -0500
On May 29, 2011, at 10:19 PM, Bing Li wrote:
> I got a problem when processing NSString returned from a method, which
> extracting data from an XML. The data is extracted correctly by the
> following method.
>
> However, when executing "isEqualToString" with the returned NSString, it got
> the exception as follows.
>
> "-[NSXMLNode isEqualToString:]: unrecognized selector sent to instance
> 0x1001233e0"
This message indicates that -isEqualToString: was sent to an instance of NSXMLNode, not NSString.
>
> If tracking with XCode, the NSString variable which holds the returned value
> from the following method is specified as "Variable is not a CFString".
Another indication that the variable is not actually an NSString. Keep in mind the difference between the declared type of a variable and the actual type of the object it points to. They can be different, usually because of bugs.
> How to fix the bug?
>
> + (NSString *) Read:(NSString *)xml Path:(NSString *)xPath
> {
> NSError *err = nil;
> NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithXMLString:xml
> options:NSXMLDocumentTidyXML error:&err];
> NSArray *nodes = [xmlDoc nodesForXPath:xPath error:&err];
> [xmlDoc release];
> [err release];
You don't own "err", so you must not release it. Also, if you're not interested in the error, just pass NULL for that parameter. Furthermore, the value of "err" is undefined if the method succeeded. There's no guarantee that the method will have left your initial value alone, nor that it will set it to nil. In other words, you must not touch "err" unless "nodes" is nil.
> if ([nodes count] > 0)
> {
> return [[nodes objectAtIndex:0] autorelease];
You don't own the object from the array, so you are not entitled to autorelease it.
Also, this is where your problem came in. The nodes array contains NSXMLNode instances, not strings. You can ask an NSXMLNode for its text contents with the -stringValue method.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden