PDA

View Full Version : Slew of Quick Cocoa Questions!




Littleodie914
Apr 2, 2007, 08:20 PM
Hey guys, I've got a few Cocoa issues I'm having trouble getting over. Believe me, I've tried figuring them out on my own and I can't get any of them.


Every once in a while, (NOT every time) when I build and go, the debugger also loads and gives me the error seen in the image below. I don't have much practice with the debugger, so I don't really know what the problem is, other than it has to do with a calendar date. Any insight?
I know Core Data stores its data on its own, but I changed to a Core Data Document-Based app with the same name, same structure, etc. and everything is gone. Is there any way to remedy this?
In MyDocument.m, i can call a method that does: [self updateApplicationBadge]. I'm trying to call the same method in a separate class, but I can't figure out how to get the instance of the MyDocument. Like instead of [self updateApplicationBadge], [MyDocument updateApplicationBadge].


Thanks guys! :D



macfreek57
Apr 2, 2007, 10:08 PM
sorry, i'm new to cocoa programming, but one thing I do know (concerning referencing My Document) is that in Interface Builder, "File's Owner" is supposed to be an instance of My Document. Programmatically, i'm not sure how that helps, but the thought just jumped out from my brain when I read what you were having a problem with.
As far as the debugger is concerned, I would set a breakpoint in your code where that is happening (preferably just before) and step-thru, monitoring the variables that are being dealt with in that section of code.
Good luck!

Littleodie914
Apr 2, 2007, 10:23 PM
sorry, i'm new to cocoa programming, but one thing I do know (concerning referencing My Document) is that in Interface Builder, "File's Owner" is supposed to be an instance of My Document. Programmatically, i'm not sure how that helps, but the thought just jumped out from my brain when I read what you were having a problem with.
As far as the debugger is concerned, I would set a breakpoint in your code where that is happening (preferably just before) and step-thru, monitoring the variables that are being dealt with in that section of code.
Good luck!Ya, I've used File's Owner before to connect the Outlets/Actions defined in MyDocument.h, but the programmatically part is what I'm stuck on. :) As for the breakpoints, I don't actually know where the error is happening, so I don't know where to place the breakpoint. :eek:

robbieduncan
Apr 3, 2007, 02:28 AM
Hey guys, I've got a few Cocoa issues I'm having trouble getting over. Believe me, I've tried figuring them out on my own and I can't get any of them.


In MyDocument.m, i can call a method that does: [self updateApplicationBadge]. I'm trying to call the same method in a separate class, but I can't figure out how to get the instance of the MyDocument. Like instead of [self updateApplicationBadge], [MyDocument updateApplicationBadge].


Thanks guys! :D

Basic OO programming. Is updateApplicationBadge a class method (prefixed with a +) or an instance method (prefixed with a -). If it's a class method then you can just use [classname method]. It sounds like it not a class method and you have defined a utility class to update your dock badge and want to call a method in it. This would appear to be a good time to use a singleton object (http://en.wikipedia.org/wiki/Singleton_pattern).

This would look something like this

@implementation DockIconController
static DockIconController* _sharedDockIconController;
+ (id) sharedInstance
{
if (! _sharedDockIconController)
{
// There is not a shared instance so create one...
_sharedDockIconController = [[DockIconController alloc] init];
}
// return the shared nstance
return _sharedDockIconController;
}

- (void) updateApplicationBadge
{
// Your code here
}
@end


You would use this something like this:


[[DockIconController sharedInstance] updateApplicationBadge];

ATG
Apr 3, 2007, 04:18 AM
Basic OO programming. Is updateApplicationBadge a class method (prefixed with a +) or an instance method (prefixed with a -). If it's a class method then you can just use [classname method]. It sounds like it not a class method and you have defined a utility class to update your dock badge and want to call a method in it. This would appear to be a good time to use a singleton object (http://en.wikipedia.org/wiki/Singleton_pattern).

This would look something like this

@implementation DockIconController
static DockIconController* _sharedDockIconController;
+ (id) sharedInstance
{
if (! _sharedDockIconController)
{
// There is not a shared instance so create one...
_sharedDockIconController = [[DockIconController alloc] init];
}
// return the shared nstance
return _sharedDockIconController;
}

- (void) updateApplicationBadge
{
// Your code here
}
@end


You would use this something like this:


[[DockIconController sharedInstance] updateApplicationBadge];


They should probably be using the offical singleton design pattern (http://www.cocoadev.com/index.pl?SingletonDesignPattern) which is:
static MyGizmoClass *sharedGizmoManager = nil;

+ (MyGizmoClass*)sharedManager
{
@synchronized(self) {
if (sharedGizmoManager == nil) {
[[self alloc] init]; // assignment not done here
}
}
return sharedGizmoManager;
}

+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (sharedGizmoManager == nil) {
sharedGizmoManager = [super allocWithZone:zone];
return sharedGizmoManager; // assignment and return on first allocation
}
}
return nil; //on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone
{
return self;
}

- (id)retain
{
return self;
}

- (unsigned)retainCount
{
return UINT_MAX; //denotes an object that cannot be released
}

- (void)release
{
//do nothing
}

- (id)autorelease
{
return self;
}

Littleodie914
Apr 3, 2007, 11:27 AM
I see what you guys are saying, but the problem is, I'm trying to get the instance of MyDocument. Like the method is defined in MyDocument.h, and is implemented in MyDocument.m. I want to call it from another class, called iProcrastinateMO.m.

GeeYouEye
Apr 3, 2007, 11:29 AM
From the look of the picture, it would seem that your app is multi-threading, and the heartbeat thread (possibly, based on the NSCalendarDate stuff in there) has been assigned to thread 3. Go look at thread 0, 1, and 2 to see where the actual crash is; I suspect thread 3 is merely hanging wherever the crashed one is (maybe it's waiting for data), especially since the line its on is blue, not red. Red indicates the crash line.

robbieduncan
Apr 3, 2007, 11:39 AM
You can ask the NSDocumentController for an array of documents and work through them. Or if you simply want the foremost document you can use:


[[NSDocumentController sharedDocumentController] currentDocument];


As I always seem to end up saying: it's all in the documentation :p

Littleodie914
Apr 3, 2007, 11:47 AM
From the look of the picture, it would seem that your app is multi-threading, and the heartbeat thread (possibly, based on the NSCalendarDate stuff in there) has been assigned to thread 3. Go look at thread 0, 1, and 2 to see where the actual crash is; I suspect thread 3 is merely hanging wherever the crashed one is (maybe it's waiting for data), especially since the line its on is blue, not red. Red indicates the crash line.Hmm, whenever I try to go to the other threads, they just get weirder and weirder. (just a bunch of mach's and other 3/4-letter, terse messages)

You can ask the NSDocumentController for an array of documents and work through them. Or if you simply want the foremost document you can use:


[[NSDocumentController sharedDocumentController] currentDocument];


As I always seem to end up saying: it's all in the documentation :pAh, that did it! :D Thanks a bunch.

GeeYouEye
Apr 3, 2007, 04:52 PM
Hmm, whenever I try to go to the other threads, they just get weirder and weirder. (just a bunch of mach's and other 3/4-letter, terse messages)


Can you post a screenshot for thread 0, preferably showing the entirety of the stack trace (the part in the upper left corner)?

Littleodie914
Apr 3, 2007, 04:57 PM
Can you post a screenshot for thread 0, preferably showing the entirety of the stack trace (the part in the upper left corner)?I would, if I could find a thread 0. :) Here's a post of thread 1, though!

GeeYouEye
Apr 3, 2007, 05:50 PM
I would, if I could find a thread 0. :) Here's a post of thread 1, though!

That's a bit odd... are there any threads with more black entries in the stack?

Littleodie914
Apr 3, 2007, 06:57 PM
That's a bit odd... are there any threads with more black entries in the stack?Nope, the "main" thing in thread 1 is the only one. (If I understand your question correctly.) All the other threads just have grey entries. :confused:

gkarris
Apr 3, 2007, 07:30 PM
I grew up on Swiss Miss - the one with the tiny marshmallows... :p

Littleodie914
Apr 3, 2007, 07:52 PM
I grew up on Swiss Miss - the one with the tiny marshmallows... :pWho didn't? :D