-2

I'm using the latest xcode. implementation file: SimpleCar.m:

#import "SimpleCar.h"

@implementation SimpleCar


// set methods
- (void) setVin: (NSNumber*)newVin {
[vin release];
vin = [[NSNumber alloc] init];
vin = newVin;
}
- (void) setMake: (NSString*)newMake {
[make release];
make = [[NSString alloc] initWithString:newMake];
}
- (void) setModel: (NSString*)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];
}

//get methods
- (NSString*) make; {
return make;
}
- (NSString*) model;{
return model;
}
- (NSNumber*) vin;{
return vin;
}

-(void) dealloc
{
[vin release];
[make release];
[model release];
[super dealloc];
}
@end

interface file: SimpleCar.h:

#import <Foundation/Foundation.h>

@interface SimpleCar : NSObject {
NSString* make;
NSString* model;
NSNumber* vin;

}
// set methods
- (void) setVin:   (NSNumber*)newVin;
- (void) setMake:  (NSString*)newMake;
- (void) setModel: (NSString*)newModel;
// convenience method
- (void) setMake: (NSString*)newMake
    andModel: (NSString*)newModel;
// get methods
- (NSString*) make;
- (NSString*) model;
- (NSNumber*) vin;

@end

I get an error in the implementation file when I type "[vin release], [model release], [make release]" and I cannot run the program.

CodeLover
  • 155
  • 1
  • 16
  • 3
    This is not only not related to Xcode but also lacks all research effort. If you google the error message (which I assume is "ARC forbids explicit call to release"), then you'll get several tens of answered SO-questions. –  May 12 '13 at 20:32
  • Additionally the setters are all implemented wrong. – yinkou May 12 '13 at 20:35
  • 3
    Wherever you got that code, throw out the source and don't look at it again. Not only are all the setters implemented incorrectly, but the overall pattern looks like standard fair from ~15 years ago. Toss it and get an up to date tutorial. (I found the tutorial site -- it is awful). – bbum May 12 '13 at 21:21
  • is this code a kind of joke? that couple of lines is full of serious memory leaks... – holex May 12 '13 at 21:27
  • @holex it is from a tutsplus.com "tutorial" on Objective-C. – bbum May 12 '13 at 21:31
  • I just found that tutorial. It literally includes this exact code. I don't know why the author thought they were even qualified to write a program, much less teach other people how to. – Chuck May 12 '13 at 21:38
  • @bbum, if you are really right, it would be the worst tutorial for Objective-C I've ever seen. – holex May 13 '13 at 10:03

1 Answers1

1

ARC is turned on, therefore memory management is automatic.

A modern definition of that class would be declared as:

 @interface SimpleCar : NSObject
 @property(copy) NSString *make;
 @property(copy) NSString *model;
 @property(copy) NSNumber *vin;

 - initWithMake:(NSString*)make model:(NSString*)model vin:(NSNumber*)vin;
 @end

And would be implemented as:

 @implementation SimpleCar
 - initWithMake:(NSString*)make model:(NSString*)model vin:(NSNumber*)vin
 {
     if (self = [super init]) {
         _make = [make copy];
         _model = [model copy];
         _vin = [vin copy];
     }
     return self;
 }
 @end

You wouldn't typically implement a convenience method like setMake:andModel:. It just adds API footprint without really buying much in the way of convenience. It also raises questions like "What happens when I observe either make or model?".

bbum
  • 162,346
  • 23
  • 271
  • 359
  • Why to putt the NSNumber property in copy ? thanks for your answer; and if i declare it with strong, can initialise it like this : _vin = vin; or ( self.vin = vin ). – samir May 13 '13 at 08:34
  • Habit; NSNumber is immutable, thus `strong` would be fine. But for any value class that responds to `NSCopying`, I always specific `copy` for safety's sake. That way, if some caller of my API were to ever pass a mutable subclass of `NSNumber`, then mutate that value later, my object's state will remain coherent. – bbum May 13 '13 at 15:56
  • great thanks, i will do like you :) – samir May 13 '13 at 16:58