0

***** EDIT ***** What I'm not sure of is how to access an entity from the model in the code, and how to access a specific instance of an entity in that code. That sums up the main issues I'm having.

***** END EDIT *****

I have a tableview with a button to add to it. When the button is clicked, the user is presented with an open dialog where they select a file(s). A new Object is added to the array controller. What I'm not sure how to do is to edit the core data attributes for this new object. There are two attributes, filename and pathname, and I'm not sure how to edit them. If you look at the bottom of the openPanelDidEnd:returnCode:contextInfo: function you'll see what I'm trying to accomplish.

- (IBAction)addAttachment:(id)sender
{
    panel = [NSOpenPanel openPanel];
    [panel beginSheetForDirectory:nil
                 file:nil
              modalForWindow:[NSApp mainWindow]
            modalDelegate:self
             didEndSelector:@selector(openPanelDidEnd:
                            returnCode:
                            contextInfo:)
              contextInfo:NULL];
}

- (void)openPanelDidEnd:(NSOpenPanel *)openPanel
             returnCode:(int)returnCode
             contextInfo:(void *)x
{
    if (returnCode == NSOKButton)
    {
        NSArray *files = [openPanel filenames];

        int i;
        for (i = 0; i < [files count]; i++)
        {
            NSString *file = [files objectAtIndex:i];
            [attachmentController add:x];
            // How do I add filenames here?
            // I'm assuming it involves KVC like
            // [something setValue:@"file" forKey:@"filename"];
            // But I don't know hot to get the something
            // i.e. since I have multiple attachments,
            // how do I get the one I just created
        }
    }
}

*********** EDIT ************** Simplified, my model has 2 entities: Attachment and Item. Item has a to-many relationship with Attachment, as each Item may have many Attachment's.

My openPanelDidEnd:returnCode:contextInfo: method now looks like this:

        NSString *filename = [files objectAtIndex:i];
        MySchoolPlanner_AppDelegate *myAppDelegate = [[MySchoolPlanner_AppDelegate init] alloc];
        [NSEntityDescription insertNewObjectForEntityForName:@"Attachment"
                                      inManagedObjectContext:[myAppDelegate managedObjectContext]];
        [myAppDelegate release];

For some reason, the table view bound to the Attachment array controller does not add any. Also, I'm not sure how to access the attachment I just created to use KVC on it.

1 Answer 1

2

NSArray's add: method is something you'd hook a button up to, when you have a Core Data entity that can be created and used without any initialization. In this case just call NSEntityDescription's

+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context

with the managed object context you're using with your array controller and the appropriate entity name to create your managed object in code. You can set properties on it directly if you've created a subclass for your entity, or just use key value coding if you haven't done that yet.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help, but I'm still a little confused. See my edit. Thanks!
Well, in your code you're creating an entirely new app controller, with either a second MO context or nil, and destroying it afterwards. This is a little backwards, since you already have an existing context to use. If I were in your shoes I would actually put Core Data on hold until you've had a chance to familiarize yourself with the Cocoa framework and the design patterns it uses. Core Data makes things easy in some respects, but it's not intended to replace the basics.
I know I'm probably in over my head, but I've been building an app based on Core Data, albeit with most Core Data things done in IB. I plan on familiarizing myself extensively with Core Data in the near future, but I'd like to get this one solved right now. Thanks for all your help, though. I think what I'm not sure of is how to access an entity from the model in the code, and how to access a specific instance of an entity in that code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.