Objective C, Core Data issue - objective-c

I'm following a tutorial in "More iPhone 3 Development. In the code, they have a line:
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
I get an error: "accessing unknown 'managedObjectContext' getter method" on that line.
I've downloaded his sample code and his program runs fine with that line of code. I've tried rewriting the code and I've even copied and pasted his code into my program, but I still get the error.
I am using the newest SDK, and I'm wondering if something might have changed so this getter method doesn't work anymore, but I do not get any warning about deprecation.
Does anyone know what is going on, or better yet, a better way to write this line and not get the error.
Thanks
LadyDev

When you create a core data project there's a method that is automatically created for you...
- (NSManagedObjectContext *) managedObjectContext
That's your AppDelegate's getter method for the managed object context. Also in the header file you will see that getter method defined:
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
So make sure you AppDelegate has these then you should not have that problem.

I had the same problem, i found that when I created the project it put 3 #property declarations in the SuperDBAppDelegate.m file which may be a new way of doing it i'm not quite sure. Anyway I took it from there and put it in the .h file and that fixed the error, however now I have other issues which I am still debugging but hopefully that works for you.

Related

How do I view Obj-C subclass variables from Swift in the Xcode debugger?

I've got a mixed Obj-C / Swift project. Whenever I hit a breakpoint in a Swift class, the Obj-C object variables view just shows values for parent classes, but not the subclass itself.
For instance, if I have the following Obj-C class:
#interface CameraPlayerCell
#property (nonatomic, strong) Camera* camera;
#end
When I hit a breakpoint in my Swift CollectionViewController implementation, the Xcode debugger values view shows:
Searched for this for about an hour, but apparently I don't know how to ask Google what I'm looking for (or they're just being Google & showing me what they think I want rather than what I actually want...)
I know there is a way to add an expression to the Variables View so that it will show the member variables for CameraPlayerCell. I've read an article somewhere and have done this before, but for the life of me I cannot remember how to do it.
Have you tried frame variable?
From Apple:
"You use the frame variable (f v) command to get a list of all the variables in the stack frame."
Example:
Cast the value:
po (CameraPlayerCell *)cell
Then you can access the subclass properties:
po ((CameraPlayerCell *) cell).camera
Works fine on my machine. Keep in mind that an Objective-C property is merely a shorthand for accessor methods for an ivar; it is the ivar that will appear in the variables list. (I assume that yours is automatically synthesized.)

Document based Obj-C (Mac) app issuing error about nsTextViewObj

I'm playing around with Julius Guzy's sample code for creating a document based application that can save text files. I ran his code and it works perfectly and the app runs/save files (All in the latest version of Xcode. Though Guzy's sample code compiles using the 10.6 SDK). I'm trying to learn from it and rewrite the code. I have everything in order to compile the app but for the strangest thing in MyDocument.m. I am unable to run it.
#synthesize nsTextViewObj;
Xcode is giving me this error, which I don't really understand it.
Existing ivar 'nsTextViewObj' for property 'nsTextViewObj' with
assign attribute must be __unsafe_unretained
nsTextViewObj; is suppose to be linked to the "File Owners" in IB. I did that in the first place. It beats me why I'm getting that error. SDK conflicts maybe? How can I fix this?
ARC requires some changes to the code. Xcode provides a tool to automatically convert non-ARC code to ARC: Edit / Refactor / Convert to Objective-C ARC…
The Xcode tool is a good start, still I would recommend you to read Transitioning to ARC Release Notes.
I don't know Julius Guzy's code, but probably you should change it to something like:
#interface ifc_name : NSWindowController {
NSTextView *__unsafe_unretained nsTextViewObject;
}
#property (unsafe_unretained) NSTextView *nsTextViewObject;
As the name implies, unsafe_unretained is not safe: when the object is deallocated, your reference is invalid. If you are deploying only to iOS 5 or higher, and OS-X 10.7 or higher, you can use weak instead. With weak your reference will become zero when the object is deallocated.

Setting property values not sticking / viewDidLoad being called multiple times in Xcode 4.3

So this is weird.
I have a property:
#property (weak, nonatomic) Something *something; // .h
#synthesize something=_something // .m
For some reason, setting that property isn't keeping _something set.
I even wrote a setter:
- (void)setSomething:(Something *)something {
_something = something;
}
The passed something is a valid instance of the object. However, after stepping through the line _something = something, _something is nil and something is still a valid object.
This is making me crazy, because everything looks right to me. I've written this same kind of thing a thousand times. The only thing that's new is that this is in Xcode 4.3.
Any help would be appreciated. Thanks!
ETA: So, perhaps this is a hint? viewDidLoad is being called a whole whackload of times, in batches of 4. I'm using storyboards, so the view is automatically set properly. Maybe the view controller isn't properly getting loaded or initialized or something? I've got the class set and everything looks like it should be working, but it isn't.
OK, I figured it out. I'm not sure where this method came from, but this was in the SomethingViewControler code:
- (void)loadView {
// If you create your views manually, you MUST override this method and use it to create your views.
// If you use Interface Builder to create your views, then you must NOT override this method.
}
Because I'm using storyboards, the very presence of this method caused all manner of problems. I'm pretty embarrassed, because I probably copied it and pasted it from somewhere without thinking about it.
Anyway, I hope this helps someone. It only killed most of my day.
Maybe the thing you're assigning to that property is't pointed to from anywhere else. So after the assignment the only pointer to that object is your property and it's week so the object is thrown out of heap

Unrecognized selector error when calling a super's initWithAttributedString: method from an NSAttributedString subclass

I'm tearing my hair out trying to do the simplest of tasks... subclassing an NSAttributedString. Trying to call the super class's initWithAttributedString: method is causing an unrecognized selector sent to instance error.
MODAttributedString.h:
#import <Foundation/Foundation.h>
#interface MODAttributedString : NSAttributedString
#property (nonatomic, retain) NSDictionary *links;
+ (MODAttributedString*) attributedStringWithFormat:(NSString*)text args:(id)argOne, ... NS_REQUIRES_NIL_TERMINATION;
+ (MODAttributedString*) attributedStringWithFormat:(NSString*)text attributes:(NSDictionary*)attributeDict;
#end
The code that is causing the crash (I'll explain the reason I split the alloc from the init in a moment):
MODAttributedString *modString = [MODAttributedString alloc];
// Pausing debugger here and typing 'po modString' causes gdb error
modString = [modString initWithAttributedString:attributedString];
My only clue is that stepping over the alloc call, when I try to po modString, I'm given this error:
"The program being debugged hit an ObjC exception while in a function called from gdb.
If you don't want exception throws to interrupt functions called by gdb
set objc-exceptions-interrupt-hand-call-fns to off.
GDB has restored the context to what it was before the call.
To change this behavior use "set unwindonsignal off"
Evaluation of the expression containing the function (_NSPrintForDebugger) will be abandoned."
If I temporarily change the super class of MODAttributedString to a UIView, the alloc does not cause the gdb error (I stop the debugger prior to the init, which would obviously not work for anything other than an attributed string). However, common classes like NSArray, NSDictionary and NSAttributedString all fail with the same error.
In the method that calls the [MODAttributedString alloc] I use NSAttributedString as its own standalone class just fine. I am sure I'm including the MODAttributedString header in this .m file as well.
I'm using Xcode 4.2 and the iPhone 5 simulator. I've cleaned the project multiple times, tried creating a new project, tried using both LLVM GCC 4.2 and Apple LLVM 3.0, restarted Xcode and restarted my machine all to no success. I searched for this particular issue heavily before posting, but I only found issues related to properties, never to a superclass's public methods.
Is this a build settings issue? A configuration error? A compiler bug? I've subclassed common Apple classes hundreds of times, and for some reason this is the first time I've ever had an issue. Has anyone else ever had a similar problem? It's probably a really simple fix, but I just can't seem to figure it out on my own.
Thanks in advance!
It isn't "the simplest of things". You can't subclass NSAttributedString - it's part of a class cluster. This means (among other things) that the class returned when you instantiate is not necessarily the class you asked for. See What exactly is a so called "Class Cluster" in Objective-C?
It is possible, with great pain and difficulty, to subclass within a class cluster, but my advice is to write a wrapper class instead; you'll be much happier. Cocoa's dynamic redirection of unhandled methods makes this very easy.

Xcode does not recognise methods added through categories

I've created a category to add methods to NSManagedObjectModel. Everything works fine except Xcode keeps throwing warnings about NSManagedObjectModel not implementing the methods. How can I force it to recognise the method?
Did you #import the .h of the category?