Error with custom Class definition in protocol - iphone

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 *)makeUrlWithString:(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

Related

Subclassing a subclass of UIImageView - Forward declaration errors

I have read question after question about people getting the same error as me, but I simply do not understand them, so before you go searching for duplicate questions, maybe someone can explain to me what I am doing wrong with this subclassing deal.
I have a subclass of UIImageView called swapView that I want to subclass to override the method -(void)count for special cases. I went to subclass this as I have any pre-existing UIKit class, but when I tried to build and run the project, I get this error:
Attempting to use the forward class 'swapView' as superclass of 'coinView'
I have tried putting both the #import statement of swapView and #class swapView in coinView.h and I've tried putting the import statement in coinView.m, but it refuses to build because of this continued error. If I move the import statement into the .m file, all references to the superclass's methods and properties, such as #property (nonatomic) int max; cause errors as well.
What am I doing wrong?
swapView.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#class ViewController;
#interface swapView : UIImageView
{
NSTimer* tmr;
}
#property (nonatomic) int current;
#property (nonatomic) int max;
#property (nonatomic, retain) UIImage* firstImage;
#property (nonatomic, retain) UIImage* secondImage;
#property (nonatomic) BOOL smallMax;
#property (nonatomic, retain) ViewController* pvc;
- (BOOL)testCollision:(CGPoint)point;
- (float)randomFloatBetween:(float)smallNumber bigNumber:(float)bigNumber;
#end
coinView.h
#import "swapView.h"
#class swapView;
#interface coinView : swapView
- (void)count;
- (void)move;
#end
For inheritance, the superclass MUST be inherited.
coinView.h
#import "swapView.h"
#interface coinView : swapView
- (void)count;
- (void)move;
#end
You're both forward declaring and importing ViewController.h in your swapView, which may cause compiler to complain.
swapView.h
#import <UIKit/UIKit.h>
#class ViewController
#interface swapView : UIImageView
.
.
.
#end

Cocos2d ,need to include protocol in my implementation file

So basically I have a protocol inside my interface that I need to include in my implementation because I am getting an incomplete error and therefore can't continue.
. h file
#interface waveLayer1 : CCLayer <GameKitHelperProtocol>
{
...
}
.m file
#implementation waveLayer1
GameKitHelper.h file
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#protocol GameKitHelperProtocol
-(void) onLocalPlayerAuthenticationChanged;
-(void) onFriendListReceived: (NSArray*)friends;
-(void) onPlayerInfoReceived:(NSArray*)players;
#end
#interface GameKitHelper : NSObject {
id<GameKitHelperProtocol> delegate; bool isGameCenterAvailable; NSError* lastError;
}
#property (nonatomic, retain) id<GameKitHelperProtocol> delegate;
#property (nonatomic, readonly) bool isGameCenterAvailable; #property (nonatomic, readonly) NSError* lastError;
+(GameKitHelper*) sharedGameKitHelper;
// Player authentication, info
-(void) authenticateLocalPlayer;
-(void) getLocalPlayerFriends;
-(void) getPlayerInfo:(NSArray*)players;
#end
The error is "Method in protocol not implemented" I have more files I can show ,but to save room I decided to see if you can help me fix this with just these codes
#interface waveLayer1 : CCLayer <GameKitHelperProtocol>
This says that "wavelayer1" implements the protocol "GameKitHelperProtocol".
Method in protocol not implemented
says that a method declared in a protocol has not been implemented. Chances are that you forgot to implement one of the "GameKitHelperProtocol" methods, which makes your class NOT implement that protocol, which violates the declaration you made, which causes the compiler to output an error.
Implement these 3 methods in your waveLayer1 class..
-(void) onLocalPlayerAuthenticationChanged;
-(void) onFriendListReceived:(NSArray*)friends;
-(void) onPlayerInfoReceived:(NSArray*)players;
When you declare that a class adopts a protocol, you must write an implementation for all required methods that are defined in that protocol. So in this case, you need to add method implementations that are defined in GameKitHelperProtocol.

Protocol declaration warning

MPPopoverControllerDelegate.h file
#import <Foundation/Foundation.h>
#class MPPopoverController;
#protocol MPPopoverControllerDelegate <NSObject>
#optional
- (void)popoverControllerDidDismissPopover:(MPPopoverController *)popoverController;
#end
MPPopoverController.h file
#import <UIKit/UIKit.h>
#protocol MPPopoverControllerDelegate;
#interface MPPopoverController : UIViewController <MPPopoverControllerDelegate>
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate;
#end
MPPopoverController.m file
#import "MPPopoverController.h"
#import "MPPopoverControllerDelegate.h"
#implementation MPPopoverController
#end
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate; : this line has warning
Cannot find protocol definition for 'MPPopoverControllerDelegate'
What is wrong? And how to fix this warning?
if replace '#protocol MPPopoverControllerDelegate'; with '#import "MPPopoverControllerDelegate.h', everything will be ok. But link - in Referring to Other Protocols you can see that apple says to use #protocol
Is there an absolute need for your protocol declaration to be in a different header file? Unless it's quite a large protocol definition (which yours isn't), I would suggest declaring it below your interface declaration.
MPPopoverController.h
#import <UIKit/UIKit.h>
#protocol MPPopoverControllerDelegate;
#interface MPPopoverController : UIViewController
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate;
#end
#protocol MPPopoverControllerDelegate <NSObject>
#optional
- (void)popoverControllerDidDismissPopover:(MPPopoverController *)popoverController;
#end
Compiler read your .m file, and load .h files when necessary. So it loads MPPopoverController.h first and when it read it delegate protocol is still undeclared. You could easily fix this warning just by swapping include lines. To let compiler read delegate .h file first.
#import "MPPopoverControllerDelegate.h"
#import "MPPopoverController.h"
#implementation MPPopoverController
#end
Are you sure you're ever including MPPopoverControllerDelegate.h somewhere?
Import MPPopoverControllerDelegate.h in MPPopoverController.h and MPPopoverController.h should look like:
The code copy pasted from the question and edited was removed. The following code is copy pasted from xcode.
The MPPopoverControllerDelegate.h:
#class MPPopoverController;
#protocol MPPopoverControllerDelegate <NSObject>
#optional
- (void)popoverControllerDidDismissPopover:(MPPopoverController *)popoverController;
#end
The MPPopoverController.h
#protocol MPPopoverControllerDelegate;
#interface MPPopoverController : UIViewController{
id<MPPopoverControllerDelegate> delegate;
}
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate;
#end
The MPPopoverController.m
#implementation MPPopoverController
#synthesize delegate;
//rest of view controller class.
The problem is that in your MPPopoverController interface you specify <MPPopoverControllerDelegate>. This means that the class implements this protocol! Is wrong because the class is the owner of the protocol. So your logic is wrong in some point.

Incomplete #implementation Startscreen

I'm receiving an warning on the implentation Topscore line in the code under here:
#import <Twitter/Twitter.h>
#import <GameKit/GameKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <AudioToolbox/AudioServices.h>
#interface Topscore ()
#property (strong, nonatomic) NSString *imageString;
#property (strong, nonatomic) NSString *urlString;
- (void)clearLabels;
#end
#implementation Topscore // Warning on this line
#synthesize count, date, urlString = _urlString, imageString = _imageString, delegate, Topscore, currentLeaderBoard, earnedAchievementCache;
Incomplete implementation
My startscreen.h header file looks like the code under here:
#interface Startscreen : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *audioPlayer;
}
- (IBAction)tweetTapped:(id)sender;
- (IBAction) startgame;
- (IBAction) showLeader;
- (IBAction) subScore;
- (IBAction) showLeader;
- (IBAction) addScore;
- (IBAction) settings;
-(IBAction)playSound:(id)sender;
-(IBAction)pause:(id)sender;
-(IBAction)stopmusic;
#end
Does anyone know whats wrong?
This just means that you have not implemented, or defined, all of the methods that you wrote in your header file. If you define each IBAction in your implementation file, this error will go away. For example:
- (IBAction)tweetTapped:(id)sender {
//Do whatever you want it do do here
}
Overall, the error warning just means that you need to define everything. Because it is just a warning, your app will still build and run anyway.

Syntax for creating View delegate in Objective-C

I am trying to create a delegate protocol for a custom UIView. Here is my first attempt:
#protocol FunViewDelegate
#optional
- (void) funViewDidInitialize:(FunView *)funView;
#end
#interface FunView : UIView {
#private
}
#property(nonatomic, assign) id<FunViewDelegate> delegate;
#end
This doesn't work because the FunView interface has not been declared at the time of the FunViewDelegate declaration. I have tried adding a prototype ala C++ before the #protocol:
#interface FunView;
But this just drives the compiler nuts. How am I supposed to do this?
Forward class syntax is #class Foo;, not #interface Foo;.
It would seem that you can forward declare protocols:
#protocol FunViewDelegate;
#interface FunView : UIView {
#private
id<FunViewDelegate> delegate;
}
#property(nonatomic, assign) id<FunViewDelegate> delegate;
#end
#protocol FunViewDelegate
#optional
- (void) funViewDidInitialize:(FunView *)funView;
#end