0

I'm trying to subclass the UIImage class and add a custom init method with some added variables. However, when I try and create a new object using the new initializer the object is always nil. The new init method is below:

- (id)initWithLat:(int)latitude andLong:(int)longitude andSize:(int)size {
    self = [super initWithContentsOfFile:@"dot.png"];
    if (self) {
        self.lat = latitude;
        self.longi = longitude;
        self.dotScale = size;
    }
    return self;
}

I believe I'm subclassing correctly, here's my header file:

#import <UIKit/UIKit.h>

@interface Dot : UIImage {
    int lat;
    int longi;
    int dotScale;
}

@property  int lat;
@property  int longi;
@property  int dotScale;

- (id)initWithLat:(int)latitude andLong:(int)longitude andSize:(int)size;

@end

I'm getting the error when trying to add a Dot object to a NSMutableArray.

dots = [[NSMutableArray alloc] init];
[dots addObject:[[Dot alloc] initWithLat:0 andLong:0 andSize:1]];

My guess is there's something wrong in the new initializer. I was using this as a guide.

Community
  • 1
  • 1
Peter Kazazes
  • 3,600
  • 7
  • 31
  • 60
  • I don't think using a subclass is the right thing to do here. I think you would be better off making `Dot` be a subclass of `UIView`. Then the `Dot` class can add the image to itself. Ask yourself, is `Dot` really a `UIImage` or is `Dot` a `UIView` that has, among other things, a `UIImage` in it? This is a classic "is a" vs. "has a" relationship question in OOP. – rmaddy Nov 28 '12 at 05:32
  • @maddy What I've done is create `Dot` as a subclass of `UIImage` that loads `dot.png` by default and adds the extra params. I then create `UIImageViews` based off of those images. Functionally it's fine, should I go another route? – Peter Kazazes Nov 28 '12 at 12:57
  • Your `Dot` class shouldn't extend `UIImage`. The `Dot` isn't an image, it has an image. Just like it has a latitude and it has a longitude. Make `Dot` extend `NSObject` (earlier I said `UIView` but that was wrong). Then add a `UIImage` property to `Dot` along with the others. – rmaddy Nov 28 '12 at 17:01

2 Answers2

1

initWithContentsOfFile: returns nil if the file can't be found. Can it be found? Use logging/breakpoints to find out what's going on during your initalizer.

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

This is the documentation for initWithContentsOfFile:

initWithContentsOfFile:
Initializes and returns the image object with the contents of the specified file.

- (id)initWithContentsOfFile:(NSString *)path

Parameters
path
The path to the file. This path should include the filename extension that identifies the type of the image data.

Return Value
An initialized UIImage object, or nil if the method could not find the file or initialize the image from its contents.

Note that if the specified file cannot be found that the call will return nil. Its probably a safe bet that your 'dot.png' file is not being found, especially since you didn't specify a full path to it. I can't imagine why you would want to hard code this value anyway, you probably should be adding a parameter for the file name, to the initializer.

Perception
  • 79,279
  • 19
  • 185
  • 195