Multiple NSDocuments & Shared Instances

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

  1. skunkworker macrumors regular

    Sep 9, 2007
    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

    [[EditorDocument sharedInstance] editingState]

    And in the main NSDocument class
    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


    Sep 13, 2001
    Portland, OR
    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

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

Share This Page