Discussion in 'Mac Programming' started by qwerty2k, Aug 25, 2008.

  1. macrumors regular

    so im just getting my head around coredata (or trying to ;)).

    i have made my entities via the gui and used the coredata entity on the gui builder to display the data, this works well and what i want...

    however, i wish to define a function within the entities and bind that function to a button, now i have changed the entities into new nsmanagedobject classes by selecting them then file->new->ns managed object, however i can't seem to get a function no matter where/how i define it to appear in the list when i ctrl+drag from the button to the arraycontroller in the gui builder.

    anyone offer any advice?
  2. macrumors 601


    Not sure I'm understanding what you're doing, but you can't define functions in the GUI modeler tool, you have to write those yourself. Then you connect them in Interface Builder just like any other action (most likely you'd connect through some kind of controller object though, not directly to a managed object, and have that call methods in your managed objects). I don't know if it's impossible but usually managed objects don't have action ("IBAction") methods, probably because they're usually collection objects.
  3. macrumors member


    That would be bad design. Your view isn't supposed to interact directly with the Model (the Core Data stuff). That's what controller objects are for. They tell the model when the view wants to change stuff, and they update the view when the model has changed.

    I would recommend reading up on Model-View-Controller if you don't get why it should be that way.


  4. macrumors regular


    basically all i want to do is when a button is pressed is read in some data from a file and then add it to the coredata data, ive got an entity called 'Player' which i have created a subclass of ns managed object for called players, now within this class i wanted a function called say 'loadFromFile' which would load the data from file into the coredata structure.

    is this still considered bad design?
  5. macrumors 601


    You probably want to make a controller object (application controller or whatever), do your file loading in a method there, which would initialize a new Player object to the values from the file, and then add it to the managed context.
  6. macrumors member


    In a nutshell, yes. Models don't do stuff like that. As mentioned just before this post, that's the job of a controller object. The controller object reads the file, and then creates new model objects based off what the file contains. The model objects themselves don't care at all about whether their data is coming from a file of an NSURLConnection or whatever.

    For a great example that's nearly identical to what you're describing, check out this article on


  7. macrumors regular

    will give the link a read, appreciate your time chaps :)
  8. macrumors regular

    thanks for the above link it has helped me immensely, i am however having troubles and for the life of me cannot work it out.

    if i copy and paste the code from that link for appDelegate and the exact functin definitions it uses in the molecule class it works fine, as soon as i even tweak the parameters the function in AppDelegate passes to the function in the entity (so ive changed the entity declaration etc) then i get an error "unrecognized selector sent to class" even for instance if i just delete 1 of the parameters.

    Here's my code that ive got so far below (its very much test with minimal info just trying to get the thing to work).

    #import <CoreData/CoreData.h>
    @class Hands;
    @interface Player :  NSManagedObject  
    +(Player *) addPlayer: playerName:(NSString *) playerProfit: NSNumber inManagedObjectContext:(NSManagedObjectContext *)context;
    @property (retain) NSString * name;
    @property (retain) NSNumber * profit;
    @property (retain) NSSet* playersHands;
    @interface Player (CoreDataGeneratedAccessors)
    - (void)addPlayersHandsObject:(Hands *)value;
    - (void)removePlayersHandsObject:(Hands *)value;
    - (void)addPlayersHands:(NSSet *)value;
    - (void)removePlayersHands:(NSSet *)value;
    #import "Player.h"
    #import "Hands.h"
    @implementation Player 
    +(Player *) addPlayer: playerName:(NSString *) playerProfit: NSNumber inManagedObjectContext:(NSManagedObjectContext *)context {
    	Player *player;
    	player = [NSEntityDescription insertNewObjectForEntityForName:@"Player" 
    											inManagedObjectContext:context]; = playerName;
    	player.profit = playerProfit;
    	return player;
    @dynamic name;
    @dynamic profit;
    @dynamic playersHands;
    AppDelegate code: (ive included import player.h etc up at the top)

    - (IBAction)loadPlayers:(id)sender {
    	[Player addPlayer: @"qwerty2k" playerProfit:1.2 inManagedObjectContext:[self managedObjectContext]];
    now the code compiles fine but at run time when i click the button ive bound to loadPlayers function it says the error: *** +[Player addPlayer:playerProfit:inManagedObjectContext:]: unrecognized selector sent to class 0x4120

    but i can't figure out which part is wrong, its probably something glaringly obvious, objective-c is new to me, im more used to c++.

    thanks again in advanced

  9. macrumors member


    This line is your problem:
    +(Player *) addPlayer: playerName: (NSString *) playerProfit: NSNumber inManagedObjectContext: (NSManagedObjectContext *)context;

    You declare part of your selector as "playerProfit:", but then you don't finish it. You have a syntax error. It should be:

    + (Player *) addPlayer: playerName: (NSString *) playerProfit: (NSNumber *)playerProfit inManagedObjectContext: (NSManagedObjectContext *)context;


  10. macrumors regular

    edited to what you said (though i don;t understand why that one changes to what you said and playerName doesnt?), still the same error though :(
  11. macrumors 6502

    Sorry to jump into the conversation late, but this should handle it.

    + (Player *)addPlayer:(NSString *)playerName playerProfit:(NSNumber *)playerProfit inManagedObjectContext:(NSManagedObjectContext *)context;
    Scott Stevenson has some great resources for anyone new to Cocoa at You may want to give this a quick read for some of the syntax details:

    Best of luck

    Luke Gladding

Share This Page