Is there anyway to make xcode (4.2) automatically add synthesize and the empty method bodies to the implementation file (for properties and methods defined in the interface) ?
There is no built in solution for that in 4.2. You could try creating a shell script that searches your header for properties and then adds the #synthesize lines at the right place.
Why don't you use the latest (4.4) version instead? 4.4 can automatically synthesize the ivars for you. All you have to do is declare a #property and Xcode takes care of the rest.
Related
To be on the safe side, and as my project is relatively small, when I wanted to change the name of a class in Swift, I changed it manually and went through the files individually - storyboard, class file, and in project navigator.
In Xcode, is there another way to do this to be sure of replacing all instances, or is manually the only way to go? Can you safely use a find and replace for this purpose?
Select the class name in the declaration, right-click/control click on it, choose "refactor" and select "rename". That will do a complete rename, including references to the class from your storyboards. It's much safer than doing it manually (assuming the Swift editor is as smart as the Objective-C editor - I'm still pretty new to Swift development, so I haven't tested this feature with Swift.)
(As of version 6.2, Xcode is only capable of doing this with Objective-C and C code.)
I am adding a new protocol to my project but XCode does not recognize the code. I already have other protocols in the same project without any problems but this time the funny thing is the color of the code is not the right one and the automatic text helper is not recognizing the language.
For example in a protocol the code appear like that:
#import <Foundation/Foundation.h>
#protocol URLGetDelegate <NSObject>
#required
#optional
- (void)setWeather:(NSArray*) data;
- (void)setChemists:(NSData*) data;
#end
Then NSObject appear in purple, however in the new protocol the NSObject appear in black and when I type code, NSO... XCode does not offer me the words NSObject automatically.
#import <Foundation/Foundation.h>
#protocol CompanyDelegate <NSObject>
#end
Any help?
Thanks
What the Laure_f_o is referring to is the lack of standard auto completion and text colorization that happens in XCode; essentially XCode refusing to recognize a newly added protocol as such. So when you try and add it to a class and declare that class as subscribing to that protocol, you get a build error.
I just had the exact same sort of problem: other protocols in my project that work fine, but when trying to add a new one XCode just would not recognize it at all. This is all for XCode 4.6.2
What finally solved it for me was the following (based on Javy's advice here: Xcode Not Immediately Recognizing New Classes (iOS)):
1.) Clean Project
2.) Close project (but not XCode)
3.) Open Organizer in Xcode (under the Window menu)
4.) Select "Projects" tab
5.) Delete "Derived Data" (if you have saved snapshots, you'll have to delete those first)
6.) Quit XCode and restart computer
7.) Reopen project and clean it again
8.) Import the problem protocol into a class and declare that class as subscribing to it
Then, and only then, did I finally get the protocol in question to start behaving as such and didn't get build errors trying to use it in classes.
After updating to the newest Version of Xcode 4.5 for iOS6 last night, i get Warnings and Errors like this
Property 'mapAnnotation' requires method 'mapAnnotation' to be defined
- use #synthesize, #dynamic or provide a method implementation in this class implementation
because of missing #synthesize Statements, and even Errors about unknown iVars if i use them.
The thing is, i thought it was not necessary to write these #synthesize statements since the last Xcode Update to 4.5 that came out with Mountain Lion, AND all my projects worked without them before i've updated Xcode last night (i've deleted a whole bunch of #synthesize statements from my files back then)
It's even still in the Release-Notes:
• Objective-C #synthesize command is generated by default when using properties.
So i'm confused, am i missing a new Project-Setting that turns automatic #synthesize generation on?
But it's not even working when i create a new Project and try it
I faced the same problem and found the reason and the solution.
If you look at the header file of NSManagedObject in iOS 6 SDK, you'll see "NS_REQUIRES_PROPERTY_DEFINITIONS" which forces classes to specify #dynamic or #synthesize for properties.
(You can see the NS_REQUIRES_PROPERTY_DEFINITIONS in the API diff between iOS 5.1 and iOS 6.0.)
This is because the compiler has to know if you want a property to be dynamic or synthesized especially in the implementation of a subclass of NSManagedObject class.
I could solve this problem simply by adding the #synthesize lines explicitly for the properties other than #dynamic in NSManagedObject subclasses.
I have a generic header file that I include in every project. Among other things, it defines a preprocessor macro for easily obtaining a reference to the app delegate. The problem is, the class name of the app delegate changes from project to project, as it includes the product name (AppDelegate). Therefore I wonder if it's somehow possible to use ${PRODUCT_NAME}, or a similar macro construct, in header files?
Set Preprocessor Macros in Xcode Build Settings.
APPDELEGATE_CLASS=$(PRODUCT_NAME)AppDelegate
In xcconfig,
GCC_PREPROCESSOR_DEFINITIONS = APPDELEGATE_CLASS=$(PRODUCT_NAME)AppDelegate
Then you can use APPDELEGATE_CLASS macro in your code.
#interface APPDELEGATE_CLASS : NSObject <UIApplicationDelegate> {
When I was using Xcode 3 I had configured it with a perl script I found on the web that allowed me to automatically generate the #property, #synthesize and dealloc code for new instance variables. Xcode 4 doesn't have that User Scripts menu though.
Is there a way to plug in these kinds of User Scripts for Xcode 4 or does Xcode 4 have a new feature that generates that stuff automatically? (I've looked and can't find one though).
I'm not talking about linking to IBOutlets. I know that I can drag from a xib and it will create all of the right stuff for IBOutlets, but I'm looking for something that will create #property, #synthesize and dealloc code for any instance variable.
Thanks,
Kenny
I would strongly (and I can't emphasize this enough) suggest the application:
Accessorizer
This is by far the most useful application for writing objective-C and objective-C 2.0 code that I have found. Originally written in the pre ObjC2.0 days, it created ivar getters and setters for you. Over the years it just keeps evolving. Now it does properties, container classes, key value encoding, KVO.
It tailors to your style: What line do you put brackets on, how do you indent, how do you comment? It also installs as a service so you can call it up from XCode, copy to the clipboard and paste right back into XCode.
I am not the author, I get nothing for plugging this. However, I have introduced its usage to every Mac team I've worked on and have converted every Obj-C programmer to it on first use.
It's got a demo period. Definitely at least try it out and see if it solves your issue.
I managed to get the Xcode 3 perl script mentioned above working under Xcode 4 as a behaviour.
I put it up on github. https://bitly.com/HXHiij
Hope that helps.
I haven't tested either of these yet, but both claim to support Xcode 4:
https://github.com/CraigWilliams/property_synthesize_dealloc
https://public.me.com/dhoerl (Navigate to Xcode/PropertyMaster)
I also miss the property related scripts, but I have found a workaround. As noted elsewhere, XCode 4 will automatically generate the property/synthesize/dealloc code for IBoutlets that are hooked up through IB.
So I have a dummy XIB in my project called Property_Hack.XIB with some UILabels, and whenever I need a new property for a class I go to Property_Hack, change the owner, and "connect" a label to the file. All you have to do is enter the name of the ivar you want, and XCode will add the #property statement to the .h, and in the .m it will add the #synthesize and dealloc code. Then I modify the .h (delete IBoutlet keyword, and change UILabel to the datatype you want).
Rather hackish and far from perfect, but it is still a time saver compared to the manual effort of modifying both files. Since NSIntegers and BOOLs do not need dealloc code, and are assign instead of retain, I usually set them up manually instead of using this approach. Whether or not NSStrings should be copy or retain is an argument I will not rehash here :-)