Professional Web Applications Themes

GLobal Objects - Mac Programming

Hello: I am new to Cocoa/objective C and need some help. I have programming in C on other platforms, but I am still trying to get the feel for Cocoa (I really like it already). #1 Lets suppose I have an application in which all methods of ALL of the controller classes need access to global application data. For example all of the controller objects might need access to an array of strings or a dictionary that is modified by different parts of my program. Can I globally instantiante objects and variables right before the beginning of the main function ...

  1. #1

    Default GLobal Objects

    Hello:
    I am new to Cocoa/objective C and need some help. I have programming in C
    on other platforms, but I am still trying to get the feel for Cocoa (I
    really like it already).

    #1
    Lets suppose I have an application in which all methods of ALL of the
    controller classes need access to global application data. For example all
    of the controller objects might need access to an array of strings or a
    dictionary that is modified by different parts of my program. Can I
    globally instantiante objects and variables right before the beginning of
    the main function (like I can in regular c)? Is there a way to dipose of
    these objects upon program exit?

    #2

    I found some examples of doing drawings inside of an NSview that is embedded
    in some window. It looked neat in that the NSview handled clipping,
    Allowing to scroll across an image, etc. Here is my question:

    It seemed in all of the examples that the code was directly drawing to the
    NSview. Is it possible to defined a space in memory (say 1000 by 1000) and
    draw to that. Tnen send one command such that NSview will display it (or at
    least a clipped view/scaled view of it depending on the size of the view
    window)? Kinda like double buffering

    #3 Suppose I draw a line from (0,0) to (0,72) in an NSview. Am I correct
    in saying that 1 pixel on the screen or an increment of one in a coordinate
    is equal to 1/72" of an inch? If so then my line above would be 1 inch
    long, right?. If I specify fraction al coordinates, will cocoa figure out
    all the details of its size when say printing the NSview?

    #4 If the line drawn above is specific to have a width of say 10. If I
    scale the NSview with an NSaffinetransform by 2 I know that the coordinates
    for the endpoints will scale by 2, BUT, will the width of the line scale 2?
    Would this be true for text as well? Will text (its size, etc) in an NSview
    scale also?

    Last question! Can someone recommend a good book on Cocoa. I have the
    dummies book, but it is nowhere in-depth enough for what I need. I want to
    really get at the neat features at Cocoa. It would be nice to have
    something that covered all the new stuff in OS 10.3.

    Thanks For the help in advance and sorry for the long post,
    Eli Hughes



    Thanks,
    Eli Hughes


    Eli Guest

  2. #2

    Default Re: GLobal Objects

    Eli Hughes wrote:
     

    The OO way to do this would be to create a singleton class which you
    would access throughout your app like [[MyClass sharedInstance]
    someMethod]. Do it something like:

    #import "MyClass.h"

    static MyClass *sharedInstance = nil;

    implementation MyClass

    +(MyClass *)sharedInstance
    {
    if ( sharedInstance == nil )
    {
    sharedInstance = [[super alloc] init];
    }

    return sharedInstance;
    }

    end

     

    <http://www.cocoadev.com/index.pl?CocoaBooks> has a listing of Cocoa
    books with community reviews. There's a new edition of the Hillegas book
    coming out, but I don't think any of the others cover 10.3 or Xcode.
    endian Guest

  3. #3

    Default Re: GLobal Objects

    On Tue, 4 May 2004, Eli Hughes wrote:
     

    You can do this with NSImage. Make an NSImage of the appropriate size,
    lock focus on it, draw, unlock focus, then you can draw the NSImage into
    your view when you need to get it onto the screen.
     

    I haven't made printing code, so I'm not sure on that part, but I'm pretty
    sure that all of the answers here are "yes", modulo the "one inch" part.
    If you draw something that's 72 pixels long, it will be 72 pixels long.
    The actual physical length of the line depends on the size of the user's
    screen, resolution, etc. If you're printing then I think it will be 1",
    though.
     

    Everything should scale, it will be just like zooming in to 200%.
    Michael Guest

  4. #4

    Default Re: GLobal Objects

    You can instantiate plain c data the same way you always did. If you have
    something you want to explicitly delete instead of just letting it go way
    when the program does, it's generally easiest to initialize it early in
    main() and delete it just before returning from main().

    Creating Objective-C objects before main() is trickier: while it works, I'm
    nervous about using the Cocoa runtime before it's officially ready.
    I once tried creating some global NSColors that way and ran into issues
    because NSColor's are autoreleased when created and no valid autorelease
    pool existed at that point. It didn't seem to matter matter (they should
    never have been deleted anyway) but I don't like logging a warning on every
    startup.

    If anyone knows more about this I'd be happy to hear it too.

    Paul


    "Eli Hughes" <edu> wrote in message
    news:NGKlc.22981$gnilink.net... 
    all 


    Paul Guest

  5. #5

    Default Re: GLobal Objects

    In article <NGKlc.22981$gnilink.net>,
    "Eli Hughes" <edu> wrote:
     

    Parts of this were already answered, but it should be noted that objects
    will be disposed of when your process exits. It's not clean, though, so
    it's only safe to be lazy in that fashion if you're not holding onto any
    resources other than memory.
     

    You can draw into an NSImage and then draw that image to the screen. But
    note that double-buffering is there for free already.
     

    Very subjective, but I'm a fan of Aaron Hillegass' "Cocoa Programming
    for Mac OS X." A new edition covering 10.3 and XCode was released
    yesterday, so when/if you get a copy make sure it's 2nd ed.

    --
    Standard output is like your butt. Everyone has one. When using a bathroom,
    they all default to going into a toilet. However, a person can redirect his
    "standard output" to somewhere else, if he so chooses. - Jeremy Nixon
    Gregory Guest

  6. #6

    Default Re: GLobal Objects

    In article <NGKlc.22981$gnilink.net>,
    "Eli Hughes" <edu> wrote:
     

    This is usually done in OO languages using the "Singleton" design
    pattern:

    static MyObject* gSharedMyObject = nil;

    implementation MyObject

    +(MyObject*) sharedMyObject
    {
    if( gSharedMyObject == nil )
    gSharedMyObject = [[MyObject alloc] init];

    return gSharedMyObject;
    }

    end

    If you want more than one object to be shared by different parts of
    your app, you can either make this shared object contain an NSArray in
    an instance variable listing all the objects you're interested in, or if
    they're not equivalent (usually this means of the same class), you can
    make them separate member variables of that object.

    Other options are to create these objects in your application
    delegate's init method and store them in the delegate's instance
    variables (You typically create your delegate by instantiating one in
    your MainMenu.nib file).

    You can of course also just instantiate your objects in your
    MainMenu.nib, but this may get cluttered and awkward for more than one
    or two objects.

    You don't really need to dispose of them afterwards. Their lifetime is
    the application's lifetime, and as such they're usually automatically
    torn down when your application quits (by the OS, which just "kills your
    memory space" when your app quits). But note that they don't get any
    dealloc messages in that case (this usually confuses developers because
    the NSApplication instance and some other objects never get dealloc
    messages unless you explicitly release them from applicationWillQuit:)
     

    Have a glance at NSImage, it is exactly for that.
     

    I honestly don't know. You're probably aware that users can print at,
    say 50%, so I guess you're not asking about that, because when the user
    prints at 50%, your application still draws at 100%. The printer just
    outputs the image using smaller pixels (or more correctly, less pixels).

    I guess you're also aware that most screens today don't really display
    at 72dpi. In fact, most print at something between 75 and 90, IIRC. So,
    if you're expecting to be able to draw a 2cm line by holding a ruler to
    the screen and get a 2cm line on the printer, that definitely won't work.
     

    I think this depends on when the transform is applied. I remember that
    the Quartz (CoreGraphics) docs said something about tricking the
    transform not to mess with the line width by doing things in a
    particular order. I think in some cases, the transform is only applied
    to the path, and in others it's applied to the entirety of all drawn
    pixels.

    Now, my brain is a little muddy right now, but I think maths supports
    your assumption of the line size scaling x2 when the coordinates scale
    by two. Imagine you had two points: {1,1} and {2,2}. The distance
    between those is {1,1}. Now, if you multiply the points by two, you get
    {2,2} and {4,4}. The distance between those is {2,2}, which happens to
    be the same as {1,1} *2. Formal proof that this is also true for n+1 is
    left as an exercise to the reader.
     

    Aaron Hillegass' book. A new edition with 10.3 additions is due out
    soon.
     

    Hmmm... I think there's a book called something like "Core MacOS X and
    Unix programming" or something like that that supposedly is more
    advanced. Never had it in my hands, though.

    Cheers,
    -- Uli
    http://www.zathras.de
    Uli Guest

  7. #7

    Default Re: GLobal Objects

    In article <supernews.com>,
    "Paul" <com> wrote:
     

    Well, you can, but it's un-OO. In general, do that stuff in your
    application delegate's init method and tear them down in
    applicationWillTerminate:. If you find yourself modifying main.m, you're
    very likely "guilty of procedural thinking". :-)

    Though tearing them down is optional if they'll live throughout the
    app's lifetime. After all, Unix takes care of tearing down the app's
    memory. You only need to explicity release them if you need their
    dealloc methods to run before your app quits (e.g. to get rid of
    temporary files your object creates or whatever it may want to do in
    dealloc).
     

    You should *not* create most objects without a valid NSApplication
    instance up and running. Just don't do it. y things will happen.
    The exception are those in Foundation which are intended for use without
    NSApplication.
     

    As I said, do as I said above. That said, whenever you're not violating
    OO design, if you get a message about a missing autorelease pool, just
    create it:

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    // Do ObjC calls that need a pool here.

    [pool release]; // This releases all autoreleased objects.


    Cheers,
    -- Uli
    http://www.zathras.de
    Uli Guest

Similar Threads

  1. #40604 [NEW]: Objects disappear from the global scope
    By dagdamor at simps dot ru in forum PHP Bugs
    Replies: 5
    Last Post: February 23rd, 11:59 PM
  2. Controlling all objects with a single global variable
    By rick@mcorp in forum Macromedia Director Lingo
    Replies: 2
    Last Post: December 24th, 06:23 AM
  3. Replies: 1
    Last Post: October 23rd, 03:12 PM
  4. Replies: 3
    Last Post: July 31st, 09:12 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139