Re: A Return NSString Problem
Re: A Return NSString Problem
- Subject: Re: A Return NSString Problem
- From: Chris Hanson <email@hidden>
- Date: Sun, 29 May 2011 20:43:03 -0700
On May 29, 2011, at 8:19 PM, Bing Li <email@hidden> wrote:
> + (NSString *) Read:(NSString *)xml Path:(NSString *)xPath
You need to start following the Cocoa naming and other conventions. They may be different than what you're used to, but it will help you a lot in the long term to write code that fits in well with the platform.
> {
> NSError *err = nil;
> NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithXMLString:xml
> options:NSXMLDocumentTidyXML error:&err];
> NSArray *nodes = [xmlDoc nodesForXPath:xPath error:&err];
This will get all of the nodes that match the given path. They are instances of NSXMLNode, or one of its subclasses.
> [xmlDoc release];
> [err release];
Why you release the object pointed to by err here?
> if ([nodes count] > 0)
> {
> return [[nodes objectAtIndex:0] autorelease];
Why do you have that autorelease there?
Aldo, this is going to return an instance of NSXMLNode or a subclass, since that's what are in the array returned by -nodesForXPath:error: not strings.
> }
> else
> {
> return Constants.WWW.EMPTY_STRING;
What is this? This isn't the usual Cocoa and Objective-C convention. Remember, this isn't Java or C# or Visual BASIC or whatever you're used to; follow the conventions of the platform. If you don't know what they are for a specific situation, just ask. (Ask the mailing list, not me personally.)
> }
> }
I think your overall issue is that you need to develop a better understanding of C's type system; just saying your method returns an NSString doesn't convert anything to one. This may be exacerbated by trying to follow the conventions of some other language or platform in tour coding, rather than Objective-C and Cocoa's own conventions.
-- Chris
_______________________________________________
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