Expected ')' before error in objective-c protocol declaration - objective-c

In WebServiceAPI.h, which I referred in the code below, i declared a protocol with a required metod -(void) apiFinished:(WebServiceAPI *)api. When compiling the code i get this error: WebServiceAPI.h:13: error: expected ')' before 'WebServiceAPI' (line 13 is where the method of the protocol is declared). where am I doing wrong?
#import <Foundation/Foundation.h>
#protocol WebServiceAPIDelegate
#required
-(void) apiFinished:(WebServiceAPI *)api;
#end
#interface WebServiceAPI : NSObject{
NSString *address;
NSMutableData *dataWebService;
}
#property (nonatomic, assign) id <WebServiceAPIDelegate>delegate;
#property(nonatomic, retain) NSString *address;
#property(nonatomic, retain) NSMutableData *dataWebService;
#end

The problem is that WebServiceAPIDelegate doesn't know about the class WebServiceAPI when it is defined. Add a #class directive before you create WebServiceAPIDelegate #protocol declaration.
// Add the following line to let the compiler stop worrying about
// the existance of class WebServiceAPI
#class WebServiceAPI;
#protocol WebServiceAPIDelegate
#required
-(void) apiFinished:(WebServiceAPI *)api;
#end

Related

Declare protocol conformance in objective-c protocol forward-declaration

Assume a class has been defined like this in OriginalObject.h:
#protocol OriginalDelegate;
#interface OriginalObject : NSObject {}
#property (nullable, nonatomic, weak) id<OriginalDelegate> delegate;
#end
#protocol OriginalDelegate <NSObject>
// … delegate method declarations …
#end
Now, in ExtendedObject.h, I want to do this:
#import "OriginalObject.h"
#protocol ExtendedDelegate;
#interface ExtendedObject : OriginalObject {}
#property (nullable, nonatomic, weak) id<ExtendedDelegate> delegate;
#end
#protocol ExtendedDelegate <OriginalDelegate>
// … additional delegate method declarations …
#end
Attempting this gives me the following warning on the #property … delegate; line of ExtendedObject.h:
Property type 'id<ExtendedDelegate> _Nullable' is incompatible with type 'id<OriginalDelegate> _Nullable' inherited from 'OriginalObject'
It appears that the compiler doesn't know ExtendedDelegate will conform to OriginalDelegate. Moving the full protocol declaration above the ExtendedObject interface in ExtendedObject.h resolves the warning:
#import "OriginalObject.h"
#protocol ExtendedDelegate <OriginalDelegate>
// … additional delegate method declarations …
#end
#interface ExtendedObject : OriginalObject {}
#property (nullable, nonatomic, weak) id<ExtendedDelegate> delegate;
#end
What I'd like to know is… is there any way to tell the compiler in the forward-declaration that ExtendedDelegate will conform to OriginalDelegate (enabling use of something more like the first version of ExtendedObject.h above)?
Neither of the following attempts at such a forward-declaration seem to be valid syntax:
#protocol ExtendedDelegate <OriginalDelegate>;
#protocol ExtendedDelegate : OriginalDelegate;

incompatible with type id<NSTextFieldDelegate>

I am getting new warning on my old OSX app. I am using OSX 10.10 and I am not quite sure where the problem is. Can someone help?
The actual warning is Property type 'id is incompatible with type id inherited from NSTextField
#import <Cocoa/Cocoa.h>
#import "HyperlinkTextFieldDelegate.h"
#interface HyperlinkTextField : NSTextField <NSTextFieldDelegate>
#property (assign) id <HyperlinkTextFieldDelegate> delegate; <--- warning showing up here
#end
The main implementation is
#interface HyperlinkTextField ()
#property (nonatomic, readonly) NSArray *hyperlinkInfos;
#property (nonatomic, readonly) NSTextView *textView;
- (void)_resetHyperlinkCursorRects;
#end
#define kHyperlinkInfoCharacterRangeKey #"range"
#define kHyperlinkInfoURLKey #"url"
#define kHyperlinkInfoRectKey #"rect"
#implementation HyperlinkTextField
#synthesize delegate;
And the delegate file is
#import <Foundation/Foundation.h>
#protocol HyperlinkTextFieldDelegate <NSObject>
- (void) barLinkClicked: (id) sender;
#end
NSTextField already has a delegate property, and it is typed as id<NSTextFieldDelegate>. Thus, your HyperinkTextField, which is a subclass of NSTextField, inherits this property, just as the error message clearly tells you. You cannot override this inherited property and type it as id<HyperlinkTextFieldDelegate> where that is a different type.

'expected a type' errors in Obj-C using Parse

I'm declaring three related classes for my program and getting numerous 'Expected a type' and 'Unknown type name' errors. Having looked at similar problems on StackOverflow, I thought that perhaps I was having an issue with circular dependencies, but I can't seem to resolve the errors by using #class forward declarations. What do I need to do to get this project to build?
BQUser.h
#import <Parse/Parse.h>
#import "BQQuestion.h"
#import "BQAnswer.h"
#interface BQUser : PFUser<PFSubclassing>
#property (retain) NSString *userDescription;
#property (retain) PFFile *userImage;
- (BQQuestion *) addNewQuestion:(NSString *) question; //expected a type
- (BQAnswer *) addNewAnswer:(NSString *) answer toQuestion:(BQQuestion *) question; //expected a type
- (void) likeAnswer: (BQAnswer *) answer; //expected a type
#end
BQQuestion.h
#import <Parse/Parse.h>
#import "BQUser.h"
#interface BQQuestion : PFObject<PFSubclassing>
#property (retain) BQUser *user; //unknown type name 'BQUser'; did you mean 'PFUser'?
#property (retain) NSString *questionText;
#property (retain) NSArray *answers;
#property int answerCount;
+ (NSString *)parseClassName;
#end
BQAnswer.h
#import <Parse/Parse.h>
#import "BQUser.h"
#import "BQQuestion.h"
#interface BQAnswer : PFObject<PFSubclassing>
#property (retain) BQUser *user; //unknown type 'BQUser'; did you mean 'PFUser'?
#property (retain) BQQuestion *question; //unknown type name 'BQQuestion'
#property (retain) NSString *answerText;
#property int votes;
+ (NSString *)parseClassName;
#end

Why am I getting unsafe retained?

I've just starting out with obj-c and I created 2 files, a .h and a .m file. The .h file is..
#import <Foundation/Foundation.h>
#interface CardUnit : NSObject
{
#private
NSString *_name;
NSString *_gold;
}
#property (nonatomic, assign) NSString *name;
#property (nonatomic, assign) NSString *gold;
#end
and the .m file is
#import "CardUnit.h"
#implementation CardUnit
#synthesize gold = _gold;
#synthesize name = _name;
#end
But it's giving me 2 errors on the #synthesize lines, which are...
"Existing ivar "_gold" for property gold with assign attribute must be __unsafe retained" and the same for name.
From the error i see you are using ARC, Automatic Reference Counting .
Basically you can get rid of all the #synthesize statements and even the private declarations of the ivar's name and gold is not necessary.
All you need is the CardUnit.h to be like this :
#interface CardUnit : NSObject
#property (assign) NSString *name;
#property (assign) NSString *gold;
#end
The Xcode compiler will take care of the rest.
See also this reply on SO

Forward declaration error

I have a protocol like this:
#import <Foundation/Foundation.h>
#protocol Prot1 <NSObject>
#required
- (void)methodInProtocol;
#end
This is a protocol for a delegate I want to store in a class like this:
#import <Cocoa/Cocoa.h>
#class Prot1;
#interface Class1 : NSObject
#property (nonatomic, strong) Prot1 *delegate;
- (void)methodInClass;
#end
The implementation for this class is like this:
#import "Class1.h"
#import "Prot1.h"
#implementation Class1
#synthesize delegate;
- (void)methodInClass {
[delegate methodInProt];
}
#end
When I build these pieces of code, I get the following error:
Receiver type 'Prot1' for instance message is a forward declaration
What is wrong here? I did understand that I have to do a forward declaration via #class for the protocol and I thought I only had to #import the protocol, in the class implementation... Isn't that right?
As it isnt a class, you have to define it as what it is - a protocol ;)
Use forward declaration: #protocol Prot1;;
And use the property like that:
#property (nonatomic, strong) id<Prot1> delegate;