Skip navigation.
 
mlRe: Convenience initializer not working: "unrecognized selector"
FROM : Adam Leonard
DATE : Thu Mar 13 04:48:36 2008

Read up on the difference between class and instance methods
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_3_section_3.html#/
/apple_ref/doc/uid/TP30001163-CH12-TPXREF122

And the how memory allocation/initiation works http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/AllocInitObjects.html

+alloc is a class method that is sent to the meta class instance 
(Fraction)
Your -initWith:over: is an instance method that can only be sent to a 
normal instance of your class. +alloc will return this instance, so 
you will almost always be using a [[MyClass alloc]init....] pattern.

So, the Fraction class does not respond to the selector -
initWith:over:, but the object returned by [Fraction alloc] will.

Hope that makes sense! Those little +'s and -'s can be confusing.


Adam Leonard


On Mar 12, 2008, at 8:25 PM, K. Darcy Otto wrote:

> I'm trying to create an initializer that allows me to combine 
> initialization and some setting of variables, but I get an error 
> that an "unrecognized selector" has been sent to the object. Here is 
> what I have so far:
>
> Fraction.h:
> #import <Cocoa/Cocoa.h>
>
> @interface Fraction : NSObject {
>
>     NSInteger numerator;
>     NSInteger denominator;
>     
> }
>
> @property(readwrite) NSInteger numerator;
> @property(readwrite) NSInteger denominator;
>
> -(id)initWith:(NSInteger)aNumerator over:(NSInteger)aDenominator;
>
> @end
>
> Fraction.m:
> #import "Fraction.h"
>
> @implementation Fraction
>
> @synthesize numerator,denominator;
>
> -(id)init
> {
>     return [self initWith:1 over:1];
> }
>
> -(id)initWith:(NSInteger)aNumerator over:(NSInteger)aDenominator
> {
>     if (self = [super init]){
>         self.numerator = aNumerator;
>         self.denominator = aDenominator;
>     }
>     return self;
> }
>
> @end
>
> Now, I can do the following with no ill effects:
> Fraction *frac = [[Fraction alloc] init];
> frac.numerator = 2;
> frac.denominator = 2;
>
> But if I do this, I get the unrecognized selector error:
> Fraction *frac1 = [Fraction initWith:2 over:2];
>
> Can anyone explain the error or suggest a solution? Thanks.
> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>
>

Related mailsAuthorDate
mlConvenience initializer not working: "unrecognized selector" K. Darcy Otto Mar 13, 04:25
mlRe: Convenience initializer not working: "unrecognized selector" Chris Suter Mar 13, 04:32
mlRe: Convenience initializer not working: "unrecognized selector" Adam Leonard Mar 13, 04:48