Multiple NSDocuments & Shared Instances

Discussion in 'Mac Programming' started by skunkworker, Sep 15, 2009.

  1. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #1
    So i've got a nsdocument-based application that can have multiple documents open at a time.And I have a number of different views and data classes for managing everything.
    It all works great until I try to create a shared instance that allows the other views to pull data and access the editordocument class
    like

    Code:
    [[EditorDocument sharedInstance] editingState]


    And in the main NSDocument class
    Code:
    EditorDocument *g_SharedInstance = nil;
    
    + (EditorDocument*) sharedInstance {
    assert (g_SharedInstance != NULL);
    
    if (g_SharedInstance == nil) {
    g_SharedInstance = [EditorDocument alloc];
    } 
    
    return g_SharedInstance;
    }
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    So, there's a few things wrong here. First and foremost: it's basically never correct to call +alloc without -init*. Secondarily: if you support multiple documents, how can you have the document stored in a single global variable?
     
  3. skunkworker thread starter macrumors regular

    Joined:
    Sep 9, 2007
    #3
    I have been trying to move it from a single document based to multiple document based.
     

Share This Page