Slew of Quick Cocoa Questions!

Discussion in 'Mac Programming' started by Littleodie914, Apr 2, 2007.

  1. macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #1
    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
     

    Attached Files:

  2. macrumors 6502

    Joined:
    Jan 1, 2002
    Location:
    Baton Rouge, Louisiana
    #2
    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!
     
  3. thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #3
    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:
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.

    This would look something like this
    Code:
    @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:

    Code:
    [[DockIconController sharedInstance] updateApplicationBadge];
    
     
  5. ATG
    macrumors regular

    Joined:
    Aug 7, 2005
    #5
    They should probably be using the offical singleton design pattern which is:
    Code:
    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;
    }
     
  6. thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #6
    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.
     
  7. macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #7
    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.
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    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:

    Code:
    [[NSDocumentController sharedDocumentController] currentDocument];
    
    As I always seem to end up saying: it's all in the documentation :p
     
  9. thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #9
    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)

    Ah, that did it! :D Thanks a bunch.
     
  10. macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #10
    Can you post a screenshot for thread 0, preferably showing the entirety of the stack trace (the part in the upper left corner)?
     
  11. thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #11
    I would, if I could find a thread 0. :) Here's a post of thread 1, though!
     

    Attached Files:

  12. macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #12
    That's a bit odd... are there any threads with more black entries in the stack?
     
  13. thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #13
    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:
     
  14. macrumors 604

    gkarris

    Joined:
    Dec 31, 2004
    Location:
    "No escape from Reality..."
    #14
    Quick Cocoa

    I grew up on Swiss Miss - the one with the tiny marshmallows... :p
     
  15. thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #15
    Who didn't? :D
     

Share This Page