delegate works in xcode 5 but not in xcode 6 - delegates

#property (unsafe_unretained,nonatomic) id<SceneDelegate> delegate;
it works fine on xcode 5, but it gives me this error on xcode 6.1
Error: Property type 'id<SceneDelegate>' is incompatible with type 'id<SKSceneDelegate>' inherited from 'SKScene'
what's that mean?
Addition:
beginning of scene.h
#protocol SceneDelegate <NSObject>
- (void) eventStart;
#end
#interface Scene : SKScene<SKPhysicsContactDelegate>
#property (unsafe_unretained,nonatomic) id<SceneDelegate> delegate;
viewController.h
#import "Scene.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController<SceneDelegate, AVAudioPlayerDelegate>
viewController.m
scene.delegate = self;
That's all the lines contain SceneDelegate.

It means SKScene already has a property of the same name: delegate
You are trying to redeclare that property but with a different protocol: SceneDelegate instead of SKSceneDelegate.
So either you wanted to use the delegate property, in that case you needn't declare that property, just assign your SKSceneDelegate object to the delegate property. For example:
self.delegate = mySceneDelegateObject;
Otherwise use a different name instead of delegate if SceneDelegate is an actual protocol you created (and consider renaming the protocol because it's easily confused with SKSceneDelegate).

Related

Expected Identifier or "(" before "{" token

Somehow I got this error in XCode 4.0.2, not sure what is wrong.
File: HomeViewController.h
#import <UIKit/UIKit.h>
#interface HomeViewController : UIViewController <UITabBarDelegate>
{
UIButton *Button1, *Button2, *Button3;
}
#property (nonatomic, retain) IBOutlet UIButton *Button1;
#property (nonatomic, retain) IBOutlet UIButton *Button2;
#property (nonatomic, retain) IBOutlet UIButton *Button3;
.... other member functions...
....
#end
File: HomeViewController.m
......
#import "RemoteServiceManager.h"
#interface HomeViewController()
{ //This is where the error happens: Expected Identifier or "(" before "{" token
RemoteServiceManager* serviceManager;
}
#end
#implementation HomeViewController
#synthesize Button1, Button2, Button3;
.... other member functions
....
#end
Looks like it does not recognize RemoteServiceManager. Wherever I used the serviceManager, it will say HomeViewController has no member named serviceManager.
Is it possible that is caused by XCode version? I am using XCode 4.0.2 on Mac OS X 10.6.7.
Thanks.
you cant add instance variables to private categories.
put properties in there instead, and synthesize them to obtain a variable as well as an internal getter/setter
#interface HomeViewController
#property (nonatomic, strong) NSString *privateProperty;
#end
#implementation HomeViewController
#synthesize privateProperty = _privateProperty;
#end
or you can add the instance variable to the class itself.
#implementation HomeViewController
NSString *privateVariable;
#end
Bear in mind also. that if you create a category in another file, any variables you declare in the body of that category will be static across all instances. definitely something to keep an eye out for.
To recap. you can create a variable in the interface of the main category. or in the implementation of the main category.
and the private category is for you to add prototypes to your class that will let the rest of the file know they "will be/are" available.
the old xcode cant do this, no. it does know class extensions yet because it ships with an older version of the LLVM compiler
You probably found your answer, but I post the answer here for somebody who encounters the same problem:
as Daij said, the problem is due to the version of compiler, so to fix this you need to change the compiler setting:
Build Setting > Build Options > Compiler for C/C++/ObjectiveC
Change value from "LLVM GCC 4.2" to "Apple LLVM compiler 4.2"
Hope it helps.

Avoid method not found for protocol methods

My Cocoa AppDelegate contains a reference of type ID to it's main view. The reference is polymorphic because it may point to a subclass of PDFView or a subclass of NSImageView, depending on the origin of the view's image. Both view subclasses implement the same protocol, so my AppDelegate does not have to know what type of view it's dealing with. However, every time I call one of the protocol methods I get a warning that says "Instance method '-methodName' not found (return type defaults to 'id')". I can either ignore the warning or force the issue by using "performSelector:(#selector(methodName:)" to call the protocol method.
Is there something I can do (or should have done) to eliminate the warning without resorting to performSelector?
//FLAppDelegate.h
#interface FLAppDelegate : NSObject <NSApplicationDelegate>
{
...
IBOutlet id _formImageView; //type is FLPDFView* or FLImageView*
…
}
//FLFormImageProtocol.h
#protocol FLFormImageProtocol <NSObject>
#required
- (void) methodName;
#end
//FLPDFView.h
#interface FLPDFView : PDFView <FLFormImageProtocol>
#end
//FLImageView.h
#interface FLImageView : NSImageView <FLFormImageProtocol>
#end
Type the instance variable with the protocol:
IBOutlet id<FLFormImageProtocol> _formImageView;

self.delegate respondsToSelector: ... does not compile

I've implemented a protocol with an optional method and in the calling method I want to send respondsToSelector: to self.delegate before I send the message, but that does not compile. The fail message is:
No Known instance method for selector 'respondsToSelector'.
As a work-around, I "sanitized" the delegate as shown below, which compiles...
//MyClass.h:
#class MyClass;
#Protocol MyClassDelegate
- (void)myClass:(MyClass *)sender willDoSomething:(BOOL)animated;
#end
#interface MyClass : UIViewController
#property (nonatomic, weak) id<MyClassDelegate> delegate;
#end
and
//MyClass.m:
...
#synthesize delegate = _delegate;
...
id sanitizedDelegate = self.delegate; //Hmmmm... why does this work?
if ([sanitizedDelegate respondsToSelector:#selector(myClass:willDoSomething:)]) {
[self.delegate myClass:self willDoSomething:animated];
}
.
I checked a number of posts including this one but it does not answer the compilation fail issue.
Also, alternative accessors do not work...
[self delegate]
//or
_delegate
Has anyone seen this or can advise a better way of handling?
IOS 5.0:(9A334), Xcode 4.2.1 (4D502)
-respondsToSelector: is a method on NSObject. Either assume that your id delegate is in fact an NSObject, and cast it:
[(NSObject*)self.delegate respondsToSelector:#selector(myClass:willDoSomething:)]
Or, better, make your delegate explicitly an NSObject:
#property (nonatomic, weak) NSObject<MyClassDelegate>* delegate;
Or make the protocol be a sub-protocol of NSObject:
#protocol MyClassDelegate <NSObject>
Basically you are saying that your delegate is constrained only by your <MyClassDelegate> protocol so the compiler assumes that those are the only methods available. What you need to do is have the protocol extend <NSObject> like so:
#Protocol MyClassDelegate <NSObject>
- (void)myClass:(MyClass *)sender willDoSomething:(BOOL)animated;
#end
That way the compiler knows that any object which conforms to your protocol also conforms to the <NSObject> protocol which defines respondsToSelector:.

are there any XCode plugin's that help automate creating delegates?

Just wondering if there is anything available (out of the box, or 3rd party add-on's) that speed up developing in Xcode via automating creation of some of the basic patterns...
the one in mind I'm thinking of is setting up inter-controller comms using a delegate, so automating the creation of a delegate
There are addons for xcode (I did not remember now). I installed one time, but thought difficult to use.
But you can fix it using the "Code Snippet Library", so create a new one for your delegate and it can work.
See here how create a new code snippet
This is the basic form:
#protocol SomeControllerDelegate;
#interface SomeController : UIViewController
{
...
id <SomeControllerDelegate> _delegate;
}
#property (nonatomic) id <SomeControllerDelegate> delegate; // #synthesize delegate = _delegate;
#end
#protocol SomeControllerDelegate <NSObject>
- (void) someControllerDidFinish: (SomeController *) controller;
- (void) someController: (SomeController *) controller didChangeObject: (id) object;
#end

In cocoa2.0 does #property obviate variable declaration in the interface

Just experimenting with #property and #synthesize:
#interface Greeter : NSObject
//{
// NSString * name;
//}
#property (assign) NSString * name;
- (NSString *) greeting;
#end
It seems to be the case that if you declare a variable using #property that you don't have to declare it between the braces (and you don't even need the braces if all of your interface variables are all declared using #property). Is this always correct? And is it good style to leave out the top part of the interface (braces included)? I have been using both and been irritated by the redundancy.
There is no “Cocoa 2.0”.
In Objective-C 2.0, on the modern runtime, yes, you can leave out the instance variables, and the property will generate them for you. The legacy runtime on Mac OS X still requires explicit instance variables.
You cannot leave out the ivar section entirely yet, but you can leave it empty.
Here is the webpage where I first found out you can automatically have you properties synthesized and also declare new properties in class extensions. It gives a bit of interesting back story as well.
http://www.mcubedsw.com/blog/index.php/site/comments/new_objective-c_features/
As for style and correctness, I've been using primarily properties for the last couple of weeks and it has made my code look quite clean! I can now declare private properties in my implementation and not have them exposed in the header making any interface to use my classes very simple and non-confusing to use.
I've ran into a problem when using interface builder where having an iVar to any subviews of a view controller still has to be declared in the header for interface builder to see it as an IBOutlet and assign to it. You can still declare those #private though and then have the private properties declared in a class extension in your implementation if you really want it as a property for you to use.
// In your header
#interface MenuViewController : UIViewController {
#private
IBOutlet UIButton *buttonPeopleShouldNotKnowAbout;
}
#end
// And in your implementation
#implementation MenuViewController ()
#property (nonatomic, readwrite, assign) IBOutlet UIButton *buttonPeopleShouldNotKnowAbout;
#end

Resources