It looks like you just created an Xcode workspace without an Xcode project. Here is a project you could use:
http://www.markdouma.com/developer/CarApp.zip
Typically, you simply select File> New Project to create a new project. You will probably need a Foundation-based command-line tool for this particular project.
Unfortunately, the guide did not have a better implementation of the SimpleCar class. The following is one possible rewrite:
#import "SimpleCar.h" @implementation SimpleCar -(void) dealloc { [vin release]; [make release]; [model release]; [super dealloc]; }
The commented code above is the source code. This is both potentially dangerous and a memory leak. The first line of source code releases vin before doing anything else, which is potentially dangerous. For example, what if in your CarApp.m you did this:
NSNumber *vinValue = [car vin]; [car setVin:vin]; NSLog(@"car vin == %@", [car vin]);
The source code would release the existing vin value without bothering to ensure that the value passed was not actually vin . By setting vin = newVin , he set vin to indicate to himself, but after it was released. Any subsequent attempts to send messages to vin will lead to failure or unpredictable results.
Source code also leaks memory. NSNumber instances NSNumber immutable, so creating a number via alloc/init does not make much sense (since it will always be zero and can never be changed). My replacement code first saves the value that is passed if it is vin . Then it releases vin and then assigns vin newVin .
The setMake: and setModel: are problematic for the same reasons.
- (void) setMake: (NSString*)newMake { [newMake retain]; [make release]; make = newMake; // [make release]; // make = [[NSString alloc] initWithString:newMake]; } - (void) setModel: (NSString*)newModel { [newModel retain]; [model release]; model = newModel; // [model release]; // model = [[NSString alloc] initWithString:newModel]; } // convenience method - (void) setMake: (NSString*)newMake andModel: (NSString*)newModel { // Reuse our methods from earlier [self setMake:newMake]; [self setModel:newModel]; } - (NSString*) make { return make; } - (NSString*) model { return model; } - (NSNumber*) vin { return vin; } @end
NSGod Jun 04 2018-11-11T00: 00Z
source share