"Unrecognise selector sent to instance"


#1

Hey everyone, hope everyone is having a great day. Currently on chapter 2 of the book, just finished the Objective C book. I have been looking for my mistake and I just cannot find it. When I run it, I get this error. I triplechecked for typos, went back a couple of pages and it still doesnt work:(. Any help would be really appreciated. Thanks a lot!! Charles

In BNRItem.m, I get a yellow triangle with the following warning:

“Method definition for ‘initWithItemName:valueInDollard:serialNumber:’ not found”

Here is the message I get:

2014-08-11 11:59:34.906 RandomItems[2622:322876] -[BNRItem initWithItemName:valueInDollars:serialNumber:]: unrecognized selector sent to instance 0x100207b70
2014-08-11 11:59:34.908 RandomItems[2622:322876] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[BNRItem initWithItemName:valueInDollars:serialNumber:]: unrecognized selector sent to instance 0x100207b70’
*** First throw call stack:
(
0 CoreFoundation 0x00007fff9527bf3c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff96d206de objc_exception_throw + 43
2 CoreFoundation 0x00007fff9527ee6d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff951c6384 forwarding + 1028
4 CoreFoundation 0x00007fff951c5ef8 _CF_forwarding_prep_0 + 120
5 RandomItems 0x00000001000015c9 main + 329
6 libdyld.dylib 0x00007fff965c15c9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

Here is my code:

main.m

[code]#import <Foundation/Foundation.h>
#import “BNRItem.h”

int main(int argc, const char * argv[]) {
@autoreleasepool {

    //create a mutable array object, store its address in items variable
    
    NSMutableArray *items = [[NSMutableArray alloc]init];
    
    //Send the message addObject: to the NSMutableArrat pointed to by the variable items, passing a string each time
    
    [items addObject:@"One"];
    [items addObject:@"Two"];
    [items addObject:@"Three"];
    
    //Send another message, insertObject:atIndex:, to that same array object
    
    [items insertObject:@"Zero" atIndex:0];
    
    //for every item in the array...
    
    for (NSString *item in items){
        NSLog(@"%@", item);
    }
    
    BNRItem *item = [[BNRItem alloc]initWithItemName:@"Red Sofa"
                                      valueInDollars:100
                                        serialNumber:@"A1B2C"];
    
    NSLog(@"%@", item);
    //destroy items array
    
    items = nil;
     
}
return 0;

}
[/code]

BNRItem.h

#import <Foundation/Foundation.h>

@interface BNRItem : NSObject
{
    NSString *_itemName;
    NSString *_serialNumber;
    int _valueInDollars;
    NSDate *_dateCreated;
}

- (void)setItemName:(NSString *)str;
- (NSString *) itemName;

- (void)setSerialNumber:(NSString *)str;
- (NSString *) serialNumber;

- (void)setValueInDollars:(int)v;
-(int)valueInDollars;

-(NSDate *)dateCreated;

//designated initializer for the BNRItem class
- (instancetype)initWithItemName:(NSString *)name
                  valueInDollars:(int)value
                    serialNumber:(NSString*)sNumber;

- (instancetype)initWithItemName:(NSString *)name;


@end

BNRItem.m

[code]//
// BNRItem.m
// RandomItems
//
// Created by Charles Vinette on 2014-08-08.
// Copyright © 2014 LittleBlackApp. All rights reserved.
//

#import “BNRItem.h”

@implementation BNRItem

  • (instancetype)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialnumber:(NSString *)sNumber
    {
    //Call the superclass’s designated initializer
    self = [super init];

    //Did the superclass’s designated initializer succeed?
    if (self){
    //give the instance variables initial values
    _itemName= name;
    _serialNumber= sNumber;
    _valueInDollars= value;

      //set date created to current date and time
      
      _dateCreated= [[NSDate alloc]init];
    

    }
    //return the address of the newly initialized object

    return self;
    }

  • (instancetype)initWithItemName:(NSString *)name
    {
    return [self initWithItemName:name
    valueInDollars:0
    serialNumber:@""];
    }

  • (void)setItemName:(NSString *)str
    {
    _itemName = str;
    }

  • (NSString *)itemName
    {
    return _itemName;
    }

  • (void)setSerialNumber:(NSString *)str
    {
    _serialNumber = str;
    }

-(NSString *)serialNumber
{
return _serialNumber;
}

-(void)setValueInDollars:(int)v
{
_valueInDollars = v;
}

-(int)valueInDollars
{
return _valueInDollars;
}

  • (NSDate *)dateCreated
    {
    return _dateCreated;
    }

  • (NSString *)description
    {
    NSString *descriptionString=
    [[NSString alloc]initWithFormat:@"%@ (%@): Worth %d, recorded on %@", self.itemName, self.serialNumber, self.valueInDollars, self.dateCreated];

    return descriptionString;
    }

  • (instancetype)init
    {
    return [self initWithItemName:@“Item”];
    }

@end
[/code]


#2

[quote]In BNRItem.m, I get a yellow triangle with the following warning:

“Method definition for ‘initWithItemName:valueInDollar[color=#FF0000]d[/color]:serialNumber:’ not found”[/quote]
And then:

Yep; exactly what it says.

Sometimes you just gotta step away for a bit to see what’s really there instead of what you expect to see. Happens to everyone…


#3

Thanks a lot for the quick reply, I appreciate the help!

Thanks for your time

Charles


#4

Hi again. unfortunately, the typo was made by me retyping the warning, it is not what I have on xcode:(


#5

Right. This was a little tougher to find because I could not see the CONTEXT for the yellow warning triangle in Xcode. But here it is.

In BNRItem.h:

[quote]- (instancetype)initWithItemName:(NSString )name
valueInDollars:(int)value
serial[color=#FF0000]N[/color]umber:(NSString
)sNumber;
[/quote]
In BNRItem.m:

[quote]- (instancetype)initWithItemName:(NSString *)name
valueInDollars:(int)value
serial[color=#FF0000]n[/color]umber:(NSString *)sNumber
[/quote]
It’s really important to get these correct, because later on, when you’re implementing delegate methods, you won’t even get a warning (because you’re entering the code only in the .m file, not in a .h as well); you just won’t see the expected behaviour.

UPDATE: Actually, the yellow warning likely occurred for the method declaration in the header, where it was entered perfectly. So that wouldn’t have helped anyway, except to confirm that SOMETHING was amiss in the signature of the implementation for the method.

When that happens, sometimes it’s just better to copy-and-paste from the header to the method, to guaranty that they match.


#6

I swear 2 hours passed while I was trying to figure it out. Thanks for your time now I can continue:)

Thanks a lot and have a great day!

Charles