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

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

Related

delegate works in xcode 5 but not in xcode 6

#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).

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.

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

How to clear a text field in Cocoa

I'm trying to make a simple Cocoa application using the newest version of XCode. In interface builder I added NSTextField and NSButton. When I press the button, I want it to clear whatever is in the text field.
I made a new class called AppController.h. This is the contents:
#import <Foundation/Foundation.h>
#interface AppController : NSObject {
IBOutlet id textView;
}
- (IBAction) clearText: sender;
#end
AppController.m looks like this:
#import "AppController.h"
#implementation AppController
- (IBAction) clearText: sender
{
[textView setString: #" "];
}
#end
I connected the button to clearText and the textbox to textView.
The program compiles and runs. But when I press the button, nothing happens. Why is that?
here's the run down.
1.Create an IBAction in your appController class header.
- (IBAction)someMethod:(id)sender;
2.Then create an IBOutlet for you text field
IBOutlet NSTextField *textFieldname;
You then connect the IBAction to the button in interface builder, and your IBOutlet to your textfield.
Inside the implementation file (.m) IBAction method do
- (IBAction)someMethod:(id)sender{
textFieldname.stringValue = #"";
}
This is very basic. I suggest you google for some tutorials. There's plenty out there. Here's something that may help. Chapter 8 describes how to do exactly what you're asking.
link text

What's wrong with this Cocoa code?

I'm trying to make a simple Cocoa application using XCode 3.2.3. In interface builder I added NSTextField and NSButton. When I press the button, I want it to clear whatever is in the text field.
I made a new class called AppController.h. This is the contents:
#import <Foundation/Foundation.h>
#interface AppController : NSObject {
IBOutlet id textView;
}
- (IBAction) clearText: sender;
#end
AppController.m looks like this:
#import "AppController.h"
#implementation AppController
- (IBAction) clearText: sender
{
[textView setString: #" "];
}
#end
I connected the button to clearText and the textbox to textView.
The program compiles without error and runs. But when I press the button, nothing happens. Why is that?
Using id for an IBOutlet is a bad practice. Use
IBOutlet NSTextView* textView;
instead.
Please check using the debugger, or putting NSLog(#"foo!"); before [textView setString:#""] to see if the action method is really called.
Another pitfall is that there are NSTextView and NSTextField. These two are different!
The former supports both setString: and setStringValue:, while the latter only supports setStringValue:.
Which object did you use in the interface builder?

Resources