Is there a key combination in Xcode to implement a Protocol? - objective-c

In Visual Studio if I define a class to implement an interface e.g.
class MyObject : ISerializable {}
I am able to right click on ISerializable, select "Implement Interface" from the context menu and see the appropriate methods appear in my class definition.
class MyObject : ISerializable {
#region ISerializable Members
public void GetObjectData(SerializationInfo info,
StreamingContext context)
{
throw new NotImplementedException();
}
#endregion
}
Is there anything anything like this functionality available in Xcode on the Mac? I would like to be able to automatically implement Protocols in this way. Maybe with the optional methods generated but commented out.

XCode currently does not support that kind of automation. But: an easy way to get your code bootstrapped with a protocol is to option-click the protocol name in your class declaration
#interface FooAppDelegate : NSObject <NSApplicationDelegate,
NSTableViewDelegate> {
to quickly open the .h file defining the protocol. From there, copy and paste the methods you're interested in. Those headers tend to be well-commented, which helps in determining which methods you can safely ignore.

I have not seen that feature in Xcode.
But it seems like someone could write a new user script called "Place Implementor Defs on Clipboard" that sits inside of Scripts > Code.
You did not find this useful.

There is not currently such a refactoring in Xcode.
If you'd like it, please file an enhancement request.

I know this thread s a bit old, but I wondered the same thing and found this question.
In my case, I'm defining a property in the interface (.h) and I want to synthesize it in the implementation (.m). I also need to implement methods defined in the interface. Yes, Xcode helps as others have mentioned, but modern IDEs offer these productivity enhancements for things we do frequently. It appears that this is still not a feature in Xcode 4.3.3. However, the feature is available in JetBrains' AppCode. I'm only dabbling with the trial, but it appears to only be possible one property or method at a time, not the whole interface like Visual Studio.

Xcode can help you per protocol method, lets say you have a protocol like this:
#protocol PosterousWebsitesDelegate <NSObject>
- (void)PosterousWebsitesLoadSuccess:(PosterousWebsites*)websites;
#end
in the #implementation section of your .m file you can start writing the name of the method and pressing ESC key to autocomplete the signature of the method/selector:
-(void)Poste (...press ESC...)
Xcode will autocomplete a full signature of the #protocol method, pres TAB to confirm the code.
If you are really committing to learn OSX/iOS Development, I would recommend you to read "XCode 3 Unleashed", a book that really helped me to know Xcode as deep as I know VS :)

check this plugin
https://github.com/music4kid/FastStub-Xcode
it does the thing that you are asking for and more.

Macrumors had a discussion on this too. There is a link to some apple scripts. I haven't actually tried these.

Related

Intellisense - deduce right Class by its methods

I'm used to work with NetBeans and now I'm trying IntelliJ. So my question is: Does IntelliJ has a way to get right class by its methods?
For example, in NetBeans if I write:
glGenBu // Intellisense will kick in and will suggest me
to use GL15.glGenBuffers() method from GL15 class
This will automatically import the right library.
This is very handy because I'm working with LWJGL and it has a bad way to manage OpenGL methods ('GLXX' where XX is the version of OpenGL and all methods that appeared in that version are stored in that class) and I never remember the right class where my desired method is.
Thank you.
Pressing Ctrl+Space when you already see completion list will show more suggestions, with static methods from the entire project among them. See https://www.jetbrains.com/help/idea/auto-completing-code.html for more details.

How to define methods and properties only visible within an objective-c framework?

I am trying to write an objective-c framework and I would like to have methods and properties visible only within the framework. I know I could define them in a class extension inside the implementation file but then they will not be accessible by other classes.
One way I was thinking to do it was to define a category for example MyClass+Internals and make that header private. but make the MyClass.h header public. I was wondering if there was a better way of doing this. Also, I'm not sure you can define properties within a category I thought it was only methods. Thanks for any suggestions or feedback.
Say you have a class named "Foo", then in "Foo_Framework.h", create:
#interface Foo()
#property ....;
- .... method ....
#end
Then, make sure that "Foo_Framework.h" is imported before the #implementation Foo. That'll cause the class Foo to be compiled with the extended interface found in said header file. That header can then be used throughout your framework. Just don't make it available outside said framework.
You are correct that you can't declare properties (that are synthesized) in a category. That was one of the primary motivations for the creation of class extensions, of which the above is an example.

How do i interface my objc program with an objc++ library?

I have an objc program and i would like to use a widget that is written in objc++ (namely https://launchpad.net/scintilla-cocoa). How do i go about this? Basically i want a new window controller object to interface with this objc++ library to define a scintilla text editor widget. Simply creating a new 'objc class' and accessing the library from there generates a bunch of errors related to the C++ class keyword and so on.
Thanks in advance
Since I'm the one who put you into the (hopefully rewarding :-)) trouble of using Scintilla, here I am.
Let's say we create a ScintillaView subclass, named ppScintillaEditor.
The file should have an .mm extension (e.g. ppScintillaEditor.mm)
The code would be roughly like this...
Interface
#import "Scintilla/ScintillaView.h"
#interface ppScintillaEditor : ScintillaView
{
// your iVars
}
// your properties / methods / whatever
Now, as for the implementation part, remember to put some initialization method to set up the view properly (as in the example accompanying Scintilla-cocoa; I mean the Test project)
Sidenote : Of course, you can create subclasses, categories or whatever on top the ScintillaView class, pretty much based on what you need - I, for example, have create a separate Category just in order to group there some ScintillaView specific commands (sooner or later, you'll notice that for some more advanced Scintilla manipulations, although it's there, it may need some polishing to be a bit more cocoa-friendly, so here you go...)
Now, last but not least...
To resolve the "bunch of errors related to the C++ class keyword and so on", as I've shown in my other video-response to your comment, all you have to do is :
Go to your project's Build Settings
Under Apple LLVM Compiler 3.0 - Preprocessing
Option Preprocessor Macros
Add to both Debug and Release :
SCI_NAMESPACE SCI_LEXER
And that's it. :-)
Hint : The above are defined by Scintilla to avoid clashes between C and non-C elements, like above... so, all it takes is to notify the preprocessor and the rest is taken care of....
you would create an objc class which has the interface your app needs, then implement and add the ivars and implement -- all behind a compilation firewall so the objc++ sources are not included in the header. your implementation would provide any necessary conversions.
it is like you have already done, but you remove the scintilla headers from the header for your wrapper -- they are visible only to your wrapper's implementation.
Update
To illustrate one possible approach:
MONScintillaWrapper.h
// no c++/scintilla sources should be included in this header
#import <Foundation/Foundation.h>
#interface MONScintillaWrapper : NSObject
- (void)setBackgroundColor:(NSColor *)pColor;
#end
MONScintillaWrapper.mm
#import "MONScintillaWrapper.h"
#implementation MONScintillaWrapper
{
scintilla::t_thing scintillaThing;
}
- (void)setBackgroundColor:(NSColor *)pColor
{
...convert pColor to a scintilla color and pass that to scintillaThing...
}
#end

How to fix "Protocol Not Implemented"

When I make a new controller called TestController, I insert
<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UISearchBarDelegate>
to the interface of Test.h
When I run the app on the simulator, there are many issues that say
"incomplete implementation" and "method in protocol not implemented"
I know some method is not implemented, but which one? How do I know what method I should add?
Show the issues navigator and expand one of the "method in protocol not implemented" issues. The first detail item will show you where the method is defined in the headers (and, thus, which #protocol it's in). Even better, though, the second detail will show you the name of the protocol.
Switch to the Issues navigator. Ignore the Incomplete implementation message, and look at the Method in protocol not implemented message. You can expand this message to get more information that the compiler provided. The first detail is what you're looking for:
You can see here that I've forgotten to include a method for tableView:numberOfRowsInSection:. I can copy this declaration and paste it into my own code to start writing the method.
Note that there are three issues in my issue navigator. The third is another missing method, which can also be expanded and copied in the same way.
There are two things I can think of which you can look for. First, look in your .h file and see if you have any method prototypes that you may have forgotten to implement, or else implemented and then changed the implementation such that it no longer matched the prototype's method signature.
Second, if the compiler isn't telling you which methods are missing, look at the documentation of UITableViewDelegate, UITableViewDataSource, etc. In the Tasks section, where it lists the various public class and instance methods, it will say "required method" next to any that need to be there. For example, if you class conforms to the UITableViewDataSource delegate protocol, -tableView:cellForRowAtIndexPath: and -tableView:numberOfRowsInSection: are required.
Check your projectname-Prefix.pch file. I got carried away there and imported something I shouldn't have, and that made it look like the one protocol I was implementing in that class was not implemented, when in fact it was. Just go ahead and make sure you aren't including any protocols there.
In my case it was because the file I shouldn't have imported was another protocol file, that was not directly related to the class where I was getting the warning.
Also when you clean out your *-Prefix.pch file, make sure you close and re-open Xcode. I had to do that to get it to take.
Hope this helps!!
Check your implementation file to see if you have included the there. If you have that bit, you need to delete it.

Generate skeleton method bodies in implementation files in Xcode

In Xcode, for Obj-C programs, is there a way to generate skeleton method body in the .m file if I add a new method or implement an interface in the corresponding .h file? Like in Eclipse, if you implement an interface, it will bring in the method skeleton according to the definition of the interface.
if you do a lot of OS X/iOS development, the best option i know of (for objc) is Accessorizer.app.