How to notify of an error...

  • Ok.  I'm sorry if this sounds like a dumb question.  Have only been
    doing Cocoa for about 3-4 months after switching from a Windows .NET
    environment, I don't know everything yet :)

    I have a class that at some point reads a one line file and parses the
    data (No I can not change this, it's reading a file from a 3rd party
    app, not my own).  Well, if for some reason it encounters an error how
    do I notify the calling class?

    For instance: My main window controller will be the one that creates
    this object, the object will then at some point load the file, if it
    encounters an error how does it relay that information to the window
    controller so I can display a proper error message?

    I thought about just passing the window controller to the initWith
    section but to me that just seems like a waste of memory...

    In .NET I would use events, but I am not sure in Cocoa?

    Thanks
    -Tim
  • On 11/6/07, Tim Davis <binary.method...> wrote:
    > Ok.  I'm sorry if this sounds like a dumb question.  Have only been
    > doing Cocoa for about 3-4 months after switching from a Windows .NET
    > environment, I don't know everything yet :)
    >
    > I have a class that at some point reads a one line file and parses the
    > data (No I can not change this, it's reading a file from a 3rd party
    > app, not my own).  Well, if for some reason it encounters an error how
    > do I notify the calling class?
    >
    > For instance: My main window controller will be the one that creates
    > this object, the object will then at some point load the file, if it
    > encounters an error how does it relay that information to the window
    > controller so I can display a proper error message?
    >
    > I thought about just passing the window controller to the initWith
    > section but to me that just seems like a waste of memory...
    >
    > In .NET I would use events, but I am not sure in Cocoa?

    <http://developer.apple.com/documentation/Cocoa/Conceptual/ErrorHandlingCoco
    a/index.html
    >

    In particular...

    <http://developer.apple.com/documentation/Cocoa/Conceptual/ErrorHandlingCoco
    a/ErrorRespondRecover/chapter_3_section_2.html
    >

    -Shawn
  • > I thought about just passing the window controller to the initWith
    > section but to me that just seems like a waste of memory...

      Premature optimization. A single pointer to an interesting object on
    a desktop computer really is nothing at all to worry about
    memory-wise. After all, you're not making a copy of your window
    controller, just referencing it ...

    > In .NET I would use events, but I am not sure in Cocoa?

      In Cocoa you could use NSNotification ... however depending on your
    requirements it might be simpler for the controller to call the
    object's "open and parse the file" method, which would pass back the
    overall status of the command, displaying an error if the status isn't
    "a-okay".

      Many mechanisms within Cocoa itself already use this pattern
    (they're handed an NSError** which is specified if there's a problem,
    then the caller checks this error and displays it to the user if it's
    set).

    --
    I.S.
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today