Copy and paste challenge?


#1

So, I just finished the challenge, but I feel sort of like I cheated. I reviewed the “Begin Editing on Insert” section, as directed, and then created the required outlets and action. Then I just copy and pasted the whole contents of the createEmployee: method into my createCar: method, changed all of the employeeController references to carController, changed Person to id for the type of object, changed the p variable to a c (because the p looked dumb there when there wasn’t anything starting with a p anymore, and I used c for car although I guess I could have used i for id, but i alone is usually a loop variable) and connected everything up. I mean, I copy and paste stuff all the time when I’m building programs and scripts, but it’s generally my own stuff and I know how it works. I’m not sure what some of this does, like the makeFirstResponder message (although I can guess that it probably pulls focus off any text being edited) and the grouping stuff (less sure about that but guessing it is a set of tied together events that have to be set up for undo/redo). It makes me feel like a bit of a stinker to just slap it in like that. Although, I don’t know what is going on behind the GUI for CoreData either, so maybe I should get more comfortable with that.


#2

If I remember right the book did say something to the effects of: ‘looking back at your code in…’… The copy and paste route was your prerogative!

I actually wound up with ALOT less code than the RaiseMan stuff :slight_smile: To be exact, I created a ‘Car’ NSManagedObject Subclass (the lazy way) and wet about it this way (read it,don’t copy and paste it :stuck_out_tongue:):

Car.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface Car : NSManagedObject

@property (nonatomic, retain) NSNumber * condition;
@property (nonatomic, retain) NSDate * datePurchased;
@property (nonatomic, retain) NSString * makeModel;
@property (nonatomic, retain) NSNumber * onSpecial;
@property (nonatomic, retain) id photo;
@property (nonatomic, retain) NSDecimalNumber * price;

@end

Car.m

#import "Car.h"


@implementation Car

@dynamic condition;
@dynamic datePurchased;
@dynamic makeModel;
@dynamic onSpecial;
@dynamic photo;
@dynamic price;

@end

CarArrayController.m

#import "CarArrayController.h"
#import "Car.h"

@implementation CarArrayController
@synthesize tableView;


-(id)newObject{
    id newObj = [super newObject];
    NSDate *now = [NSDate date];
    [newObj setValue:now forKey:@"datePurchased"];
    [newObj setValue:[NSNumber numberWithInt:5] forKey:@"condition"];
    return newObj;
}

-(IBAction)add:(id)sender{
    NSWindow *w = [tableView window];
    BOOL editingEnded = [w makeFirstResponder:w];
    if (!editingEnded){
        NSLog(@"There was an error ending the editing!");
        return;
    }
    
    Car *c = [self newObject];
    [self addObject:c];
    
    [self rearrangeObjects];
    
    NSArray *a = [self arrangedObjects];
    
    NSUInteger row = [a indexOfObjectIdenticalTo:c];
    
    [tableView editColumn:0 row:row withEvent:nil select:YES];
}


@end

It might be a long winded way of doing it rather than just using @Class Car, but I was experimenting with other things too. Worked first time :slight_smile: