开发者

Error with custom Class definition in protocol

I'm trying to set up a custom delegate protocol and am getting a strange error that I don't understand. I wonder if someone could point out what I'm doing wrong here (I'm still new to Ob-C and protocol use)...

The situation is that I've built my own URLLoader class to manage loading and parsing data from the internet. I'm now trying to set up a protocol for delegates to implement that will respond to the URLLoader's events. So, below is my protocol...

  #import <UIKit/UIKit.h>
  #import "URLLoader.h"

  /**
   * Protocol for delegates that will respond to a load.
   */
  @protocol URLLoadResponder <NSObject>
  - (void)loadDidComplete:(URLLoader *)loader;
  - (void)loadDidFail:(URLLoader *)loader withError:(NSString *)error;
  @end

However, I'm getting the following error for both method signatures:

  Expected ')' before 'URLLoader'

I feel like I must be overlooking something small and silly. Any help folks could offer would be greatly appreciated!

Whoops ... it was pointed out that I should include URLLoader.h. Here it is:

#import <Foundation/Foundation.h>
#import "URLLoadResponder.h"


/**
 * URLLoader inferface.
 */
@interface URLLoader : NSObject {
    NSString *name;
    NSString *loadedData;
    NSMutableData *responseData;
    NSObject *delegate;
    BOOL _isLoaded;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *loadedData;
@property (nonatomic, retain) NSObject *delegate;

- (void)loadFromURL:(NSString *)url;
- (void)addCompleteListener:(id)observer selector:(SEL)sel;
- (void)removeCompleteListener:(id)observer;
- (void)parseLoadedData:(NSString *)data;
- (void)complete;
- (void)close;
- (BOOL)isLoaded;
+ (NSURL *)makeUrlWit开发者_如何学GohString:(NSString *)url;
+ (URLLoader *)initWithName:(NSString *)name;

@end


You have a nice circular reference in your headers, because each header include the other (URLLoader includes URLLoadResponder and URLLoadResponder includes `URLLoader).

You can break it by using a forward declaration:

#import <UIKit/UIKit.h>
//#import "URLLoader.h" <-- Remove it to break the circular reference

@class URLLoader; // <-- Forward declaration

/**
 * Protocol for delegates that will respond to a load.
 */
@protocol URLLoadResponder <NSObject>
- (void)loadDidComplete:(URLLoader *)loader;
- (void)loadDidFail:(URLLoader *)loader withError:(NSString *)error;
@end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜