开发者

How do I call a method from an imported class in Objective C?

开发者 https://www.devze.com 2022-12-20 11:37 出处:网络
I have some code which fetches an XML file from a URL and then parses it using NSXMLParser.Currently this code is contained within the viewController which calls it.I would like, for re-usability sake

I have some code which fetches an XML file from a URL and then parses it using NSXMLParser. Currently this code is contained within the viewController which calls it. I would like, for re-usability sake, to move the parsing code to an external class and call it from the viewController.

I tried to achieve this as follows:

Created a blank NSObject class file, put the parsing code into the .h and .m files, then used an #import statement in my viewController.m file and an @Class statement in the viewController.h file so I could call the methods from within the viewController class.

I'm not sure how to proceed or whether I'm approaching this the wrong way, please advise.


I instantiate the external class with the following line in the @interface of my viewController

XMLParser *xmlParser;

In viewDidLoad for my viewController class I call a method I have written in XMLParser.m. The call is performed as follows:

[xmlParser fetchXML];

The problem is that when I compile I get the following warning:

'XMLParser' may not respond to开发者_StackOverflow中文版 '-fetchXML'

When the view loads at runtime the method isn't called, or it's called but doesn't run


You should have a file called XMLParser.h that includes something like:

@interface XMLParser : NSObject
{
    ...
}   
- (void)fetchXML;

Then in viewController.m, there should be:

#import "XMLParser.h"

I would suspect you're missing one of these steps.

0

精彩评论

暂无评论...
验证码 换一张
取 消