tags:

views:

24

answers:

2

While writing an iPad application, I seem to be running into a lot of weird errors. Basically, I have a parent class "Ticker" that checks to see if stock ticker data is cached and if it isn't, it creates an instance of NetworkTickerData, passing itself as an argument and adding the returned data to itself.

Here is the code for [Ticker getData]:

-(void)getData
{   
    // some check here to see if the data is locally cached
    // if not
    NetworkTickerData* tick = [[NetworkTickerData alloc] initWithTicker: self];
    [tick getHistoricalTickerData];
    self.tickerData = tick.tickerData;
}

and the code for [NetworkTickerData initWithTicker:]:

+(NetworkTickerData*)initWithTicker: (Ticker*)tick
{
    NSLog(@"Doing Setup");
    NetworkTickerData* t = [[NetworkTickerData alloc] init];
    t.ticker = tick;
    t.net = [[NetworkOp alloc] init];

    return t;
}

I am getting the error: *** -[NetworkTickerData initWithTicker:]: unrecognized selector sent to instance

Is there a problem passing the variable self to initWithTicker? For what its worth, the NSLog, only there for debug purposes, never prints out.

If I had to guess, the problem must be with using self, maybe it doesn't refer to the current class?

+3  A: 

You are defining initWithTicker: as a class method not an instance method as it should be.

EDIT after OP's comment:

A class method is one which is called on a class object (i.e. [MyClass alloc]).

An instance method is one which is called on an already allocated instance of a class.

The distinction has nothing to do with whether a new object is returned. Also, init methods do not return a new object, they initialize one that was just allocated via alloc, and then return that same object (self).

imaginaryboy
Thanks for the quick answer! I was under the impression that a method that returned a new instance of a class was a `+`, where if a method acted on the class, it was a `-` (class vs instance).
Jud Stephenson
+3  A: 

The initWith... methods are instance methods, not class methods. Change

+(NetworkTickerData*)initWithTicker: (Ticker*)tick

to

-(NetworkTickerData*)initWithTicker: (Ticker*)tick

Note that the + changes to a -.

Alexsander Akers
Thanks alex. If you wouldn't mind, I am curious as to why this isn't considered a class method? (the change worked, btw)
Jud Stephenson
@Jud Stephenson: `[NSObject alloc]` allocates and returns an instance. That instance is what you're sending `initWithTicker:` to. If `init` were a class method, you couldn't write `[[SomeClass alloc] init]`, because the instance returned by `alloc` wouldn't respond to `init`.
Chuck
@Chuck, thanks for that. Learn something new every day.
Jud Stephenson