Re: Core Data Form

2008-09-05 Thread Jim Turner
Jon,

First off, unless you're doing something special you don't need to
create custom classes for each entity.  If your managed object really
is just slinging strings, leave the class as NSManagedObject and
create a new entity with:

NSManagedObject *hostObj = [NSEntityDescription
insertNewObjectForEntityForName:@Host inManagedObjectContext:[[NSApp
delegate] managedObjectContext]];

then set the values on that object with:

[hostObj setValue: hostString forKey:@hostStringKey];
etc...

Second, your +(id)addHostFromPanel: method is a class method yet
you're asking for [self managedObjectContext], which is an instance
method.  Basically, you don't have a properly initialized managed
object at the time you asked for the context it's in.

If you're set on sticking with the class method, you could do
something like the following:

// Written in TextEdit, forgive the formatting
+(id)addHostFromPanel:(NSString *)hostPanel name:(NSString *)hostName
ipAddress:(NSString *)ipAddress group:(NSString *)group
inManagedObjectContext:(NSManangedObjectContext *)objContext
{
self = [super insertNewObjectForEntityForName:@Host
inManagedObjectContext:objContext];
if( self != nil )
{
[self setValue:hostName forKey:@hostNameKey];
etc...
}

return( self );
}


I'm not much of a CoreData ninja but I hope that helps.

Jim

On Thu, Sep 4, 2008 at 6:01 AM, Jon Buys [EMAIL PROTECTED] wrote:
 Hello,

 I have a problem with my app that is most likely very simple for someone
 with experience.  I have a core data based application that I would like to
 be able to add objects to via a sheet that drops down when the user clicks
 the + button (or presses Command-N).  On the sheet are two NSTextField and
 one NSPopUpButton, an add button and a cancel button.  From my AppController
 class I can get the values as strings that are entered into the sheet here:

 - (IBAction)endAddHostSheet:(id)sender
 {

NSString *hostString = [hostNameTextField stringValue];
if ([hostString length] == 0)
{
return;
}
NSLog(@the host name is %@, hostString);

NSString *ipAddressString = [ipAddressTextField stringValue];
if ([ipAddressString length] == 0)
{
return;
}
NSLog(@the ip address  is %@, ipAddressString);

NSString *selectedGroupString = [groupsPopUpButton
 titleOfSelectedItem];
NSLog(@the selected Group is %@, selectedGroupString);

// Do stuff to add to core data here...


[NSApp endSheet:addHostSheet];

//Goodbye, sheet
[addHostSheet orderOut:sender];
 }

 But my question is, how can I add these strings to core data?

 My core data setup is very simple.  I have three entities (one which I'm not
 yet using) named Host, Group, and ContactGroup. Host has attributes
 hostName, ipAddress, and a to-one relationship named group.  Group has an
 attribute groupName, and a to-many relationship named hosts.

 I've defined classes for the Entities, like this:

 #import Cocoa/Cocoa.h
 @class ContactGroup;
 @class Group;

 @interface Host : NSManagedObject {

 }

 @property (readwrite, retain) ContactGroup *contactGroup;
 @property (readwrite, retain) Group *group;
 @property (readwrite, copy) NSString *hostName, *ipAddress, *CPU, *RAM,
 *hardDrive;

 +(id)addHostFromPanel:(NSString *)newHostPanel withName:(NSString
 *)newHostName andIpAddress:(NSString *)newIpAddress inGroup:(NSString
 *)newGroup;


 @end


 I've was trying to add the hosts with the addHostFromPanel method defined
 above (with a + instead of a -), and implemented like this:

 +(id)addHostFromPanel:(NSString *)newHostPanel withName:(NSString
 *)newHostName andIpAddress:(NSString *)newIpAddress inGroup:(NSString
 *)newGroup
 {
// Debug stuff
NSLog(@host name passed over is  is %@, newHostName);
NSLog(@ip address  passed over is is %@, newIpAddress);
NSLog(@the group passed over  is %@, newGroup);

// Here we go!
Host *host = [NSEntityDescription
 insertNewObjectForEntityForName:@Host inManagedObjectContext:[self
 managedObjectContext]];
host.hostName = newHostName;
host.ipAddress = newIpAddress;
host.group = newGroup;

return host;
 }


 But, when I try to call this method from my AppController to add the strings
 to core data, I get an error (as well as a compile warning) that Host can't
 find it's managedObjectContext.

 I'm very new to Cocoa (obviously), and right now I'm very confused.  I'm
 sure I'm missing something very basic here, but I've been stuck on this for
 a couple of weeks now.

 Thanks in advance for any advice.

 Jon
 ___

 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com

 Help/Unsubscribe/Update 

Re: Core Data Form

2008-09-05 Thread Jon Buys
 NSManagedObject *hostObj = [NSEntityDescription
 insertNewObjectForEntityForName:@Host inManagedObjectContext:[[NSApp
 delegate] managedObjectContext]];


Ahhh... there's what I was missing:  the [NSApp delegate] part of
declaring a new entity.  I'm not at my Mac right now, but I'll give
that a try tonight.

 then set the values on that object with:

 [hostObj setValue: hostString forKey:@hostStringKey];
 etc...

Yep, that's what I've been doing, but I've been chasing the
ManagedObjectContext around.  I'm thinking that the example above will
fix that.


 Second, your +(id)addHostFromPanel: method is a class method yet
 you're asking for [self managedObjectContext], which is an instance
 method.  Basically, you don't have a properly initialized managed
 object at the time you asked for the context it's in.

Yea, I figured that wasn't right.


 If you're set on sticking with the class method, you could do
 something like the following:

 // Written in TextEdit, forgive the formatting
 +(id)addHostFromPanel:(NSString *)hostPanel name:(NSString *)hostName
 ipAddress:(NSString *)ipAddress group:(NSString *)group
 inManagedObjectContext:(NSManangedObjectContext *)objContext
 {
self = [super insertNewObjectForEntityForName:@Host
 inManagedObjectContext:objContext];
if( self != nil )
{
[self setValue:hostName forKey:@hostNameKey];
etc...
}

return( self );
 }


 I'm not much of a CoreData ninja but I hope that helps.

 Jim


Awesome, thanks!  I think this will help out a lot.

Jon
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Core Data Form

2008-09-04 Thread Jon Buys

Hello,

I have a problem with my app that is most likely very simple for  
someone with experience.  I have a core data based application that I  
would like to be able to add objects to via a sheet that drops down  
when the user clicks the + button (or presses Command-N).  On the  
sheet are two NSTextField and one NSPopUpButton, an add button and a  
cancel button.  From my AppController class I can get the values as  
strings that are entered into the sheet here:


- (IBAction)endAddHostSheet:(id)sender
{

NSString *hostString = [hostNameTextField stringValue];
if ([hostString length] == 0)
{
return;
}   
NSLog(@the host name is %@, hostString);

NSString *ipAddressString = [ipAddressTextField stringValue];
if ([ipAddressString length] == 0)
{
return;
}   
NSLog(@the ip address  is %@, ipAddressString);

	NSString *selectedGroupString = [groupsPopUpButton  
titleOfSelectedItem];

NSLog(@the selected Group is %@, selectedGroupString);

// Do stuff to add to core data here... 


[NSApp endSheet:addHostSheet];

//Goodbye, sheet
[addHostSheet orderOut:sender]; 
}

But my question is, how can I add these strings to core data?

My core data setup is very simple.  I have three entities (one which  
I'm not yet using) named Host, Group, and ContactGroup. Host has  
attributes hostName, ipAddress, and a to-one relationship named  
group.  Group has an attribute groupName, and a to-many relationship  
named hosts.


I've defined classes for the Entities, like this:

#import Cocoa/Cocoa.h
@class ContactGroup;
@class Group;

@interface Host : NSManagedObject {

}

@property (readwrite, retain) ContactGroup *contactGroup;
@property (readwrite, retain) Group *group;
@property (readwrite, copy) NSString *hostName, *ipAddress, *CPU,  
*RAM, *hardDrive;


+(id)addHostFromPanel:(NSString *)newHostPanel withName:(NSString  
*)newHostName andIpAddress:(NSString *)newIpAddress inGroup:(NSString  
*)newGroup;



@end


I've was trying to add the hosts with the addHostFromPanel method  
defined above (with a + instead of a -), and implemented like this:


+(id)addHostFromPanel:(NSString *)newHostPanel withName:(NSString  
*)newHostName andIpAddress:(NSString *)newIpAddress inGroup:(NSString  
*)newGroup

{
// Debug stuff
NSLog(@host name passed over is  is %@, newHostName);
NSLog(@ip address  passed over is is %@, newIpAddress);
NSLog(@the group passed over  is %@, newGroup);

// Here we go!
	Host *host = [NSEntityDescription  
insertNewObjectForEntityForName:@Host inManagedObjectContext:[self  
managedObjectContext]];

host.hostName = newHostName;
host.ipAddress = newIpAddress;
host.group = newGroup;

return host;
}


But, when I try to call this method from my AppController to add the  
strings to core data, I get an error (as well as a compile warning)  
that Host can't find it's managedObjectContext.


I'm very new to Cocoa (obviously), and right now I'm very confused.   
I'm sure I'm missing something very basic here, but I've been stuck on  
this for a couple of weeks now.


Thanks in advance for any advice.

Jon
 
 
___


Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]