Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

qwerty2k

macrumors regular
Original poster
Dec 27, 2007
157
5
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?
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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.
 

davedelong

macrumors member
Sep 9, 2007
50
0
Right here.
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.

HTH,

Dave
 

qwerty2k

macrumors regular
Original poster
Dec 27, 2007
157
5
hi,

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?
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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.
 

davedelong

macrumors member
Sep 9, 2007
50
0
Right here.
hi,

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?

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 MacResearch.org:

http://www.macresearch.org/cocoa-scientists-part-xxiii-itunes-ifying-core-data-app

HTH,

Dave
 

qwerty2k

macrumors regular
Original poster
Dec 27, 2007
157
5
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).

Player.h
Code:
#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;

@end

@interface Player (CoreDataGeneratedAccessors)
- (void)addPlayersHandsObject:(Hands *)value;
- (void)removePlayersHandsObject:(Hands *)value;
- (void)addPlayersHands:(NSSet *)value;
- (void)removePlayersHands:(NSSet *)value;

@end

player.m
Code:
#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];
	player.name = playerName;
	player.profit = playerProfit;
	return player;
}

@dynamic name;
@dynamic profit;
@dynamic playersHands;

AppDelegate code: (ive included import player.h etc up at the top)

Code:
- (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

Scott
 

davedelong

macrumors member
Sep 9, 2007
50
0
Right here.
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;

HTH,

Dave
 

qwerty2k

macrumors regular
Original poster
Dec 27, 2007
157
5
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 :(
 

lucasgladding

macrumors 6502
Feb 16, 2007
319
1
Waterloo, Ontario
+ (Player *) addPlayer: playerName: (NSString *) playerProfit: (NSNumber *)playerProfit inManagedObjectContext: (NSManagedObjectContext *)context;

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

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

Scott Stevenson has some great resources for anyone new to Cocoa at http://www.cocoadevcentral.com. You may want to give this a quick read for some of the syntax details:

http://cocoadevcentral.com/d/learn_objectivec/

Best of luck

Luke Gladding
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.