Automatic #property synthesize not working on NSManagedObject subclass - xcode

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.

Related

XCode Protocols

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.

Why are #end's not being seen by compiler?

I have 2 classes, an NSObject and an NSView. No AppDelegate.
I was getting a hard error "method definition not in #implementation context" even though the methods were between #implementation and #end. Moving a #import ahead of #implementation eliminated that but then variables from the other file were not known. To get rid of it, I wound up // commenting out all the code lines in both .h and .m files. There's no code left except #interfaces, #implementations, and #ends.
I still get a warning "#end must appear in implementation context." One of them appears immediately after the #end!
I have not attempted to use IB in this program.
Other programs compile correctly, and I think the code is correct in his one and it should work.
Is there some way the compiler gets stuck? OSX 10.6.8, Xcode 3.2.6.
I gave up on that project, created a new project, and copied and pasted the text from the bad one. The new project works fine with the same old text.
Then I ran memtest repeatedly and Applejack to clean up. The hardware is ok.
Probably the compiler is flaky; I've had to do this a couple times this year. Last time an error message said that the compiler had a partition problem; Google links said to upgrade and maybe the new version would fix the problem. So I installed Xcode 3.2.6, which produced the same error, unfortunately. Copy and paste to a new project was necessary, again.

XCode 4.2 auto add synthesize and method bodies

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.

Why can't I set a referencing outlet for my NSTextView in XCode 4?

I'm making a simple dialog in XCode 4 using the Interface Builder. I have three NSButtons, all of which are hooked up to their relevant IBOutlets. However I have an NSTextView, which I'd also like to hook up. I have the declaration for it in MyDialog.h:
#interface MyDialog : NibLoaderOC {
#public
IBOutlet NSTextView* tv;
IBOutlet NSButton* acceptButton;
IBOutlet NSButton* editButton;
IBOutlet NSButton* rejectButton;
}
#end
But when I drag the 'New Referencing Outlet' line over tv, it won't highlight. If I try and make a new outlet, it says:
Could not insert new outlet connection: Could not find any information for the class named MyDialog
I have tried restarting XCode, this does nothing. What am I doing wrong?
I noticed that .m file was moved inside en.lproj folder.
Just delete (reference only) the .m file from the Xcode and moved .m out of the en.lproj.
Add it again.It will fix the issue.
Don't worry, you will get all your connections back.
Although I'd still be interested in a proper solution, I fixed this by just manually editing the xib code to create the link.
This has the unfortunate side-effect of Interface Editor refusing to play nicely with the file (although all my manual edits were correct, and the code compiles and runs perfectly).
i got this bug in xcode 4.6.0. restarting xcode didn't fix it for me. deleting the derived data for the project, then restarting xcode did fix it.
delete /Users/myusername/Library/Developer/Xcode/DerivedData/MyProject
Another thing to check for is to make sure that if you have a custom class that you specify it under "Custom class" in the Identity inspector of Interface Builder.
So for instance if you have the following declaration
#interface MyViewController : UIViewController {
#public
IBOutlet MyCustomView* aCustomView;
}
#end
Make sure you add that into the inspector as pictured. Otherwise I've found that you won't be able to hook it up.

Xcode4 Automatic Generation of #property declarations and all that goes with it

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 :-)

Resources