No visible @interface for 'BNRItem' declares the selector


#1

Hi all,

I am getting an error " No visible @interface for ‘BNRItem’ declares the selector ‘itemName’ ", when trying to do Chapter 2,
its the end of sub-chapter “accessor methods”, and I did import via #import “BNRItem.h” inside main.m, but it shows
me the error, how can I correct that?

main.m

[code]//
// main.m
// RandomPosessions
//
// Created by John on 25/09/2013.
// Copyright © 2013 Big Nerd Ranch. All rights reserved.
//

#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 NSMutableArray pointed to
    // by the variable items, passing a string each time.
    [items addObject:@"One"];
    [items addObject:@"Two"];
    [items addObject:@"Three"];
    [items addObject:[NSNull null]];
            
    // Send another message, insertObject:atIndex, to the same array object
    [items insertObject:@"Zero" atIndex:0];
    
    // For every item in the array as determined by sending count to items
    for (int i = 0; i < [items count]; i++)
    {
        // We get the ith object of from the array and pass it as an argument to
        // NSLog, which implicitly send the description message to that object
        NSLog(@"%@", [items objectAtIndex:i]);
        
    }

    BNRItem *p = [[BNRItem alloc] init];
    
    // This creates a new NSString , "Red Sofa" and gives it to the BNRItem
    [p setItemName:@"Red Sofa"];
    
    // This creates a new NSString, "A1B2C" and gives it to the BNRItem
    [p setSerialNumber:@"A1B2C"];
    
    // We send the value 100 to be used as the valueInDollars of this BNRItem
    [p setValueInDollars:@"100"];
    NSLog(@"%@ %@ %@ %d", [p itemName], [p dateCreated], [p serialNumber], [p valueInDollars]);

    items = nil;

// NSLog(@“Items have been reset to zero”);
//
//
// NSString *myString = @“Hello, World!”;
// int len1 = [myString length];
// NSLog(@“Integer Length of myString is: %d”, len1);
// int len2 = [@“Hello, World!” length];
// NSLog(@“Integer length of myString literally is: %d”, len2);
// myString = [[NSString alloc] initWithString:@“Hello, World!”];
// int len3 = [myString length];
// NSLog(@“Integer length of myString via alloc and init is: %d”, len3);
//
}
return 0;
}[/code]

BNRItem.h

[code]//
// BNRItem.h
// RandomPosessions
//
// Created by John on 25/09/2013.
// Copyright © 2013 Big Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BNRItem : NSObject
{
NSString *itemName;
NSString *serialNumber;
int valueInDollars;
NSDate *dateCreated;
}
@end[/code]

BNRItem.m

[code]//
// BNRItem.m
// RandomPosessions
//
// Created by John on 25/09/2013.
// Copyright © 2013 Big Nerd Ranch. All rights reserved.
//

#import “BNRItem.h”

@implementation BNRItem

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

  • (NSString *)itemName
    {
    return itemName;
    }

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

  • (void) setValueInDollars:(int)i
    {
    valueInDollars = i;
    }

  • (int)valueInDollars
    {
    return valueInDollars;
    }

  • (NSDate *)dateCreated
    {
    return dateCreated;
    }

@end[/code]
Help is appreciated,
thanks.


#2

Declare two methods in the interface file:

@interface BNRItem : NSObject
{
    NSString *itemName;
    NSString *serialNumber;
    int valueInDollars;
    NSDate *dateCreated;
}

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

@end

#3

This must be a book error, I think it should be included in the errata topic within this forum.
Cheers for the quick reply, it solved the error, I had to add all of them:

[code]- (void) setItemName:(NSString *)itemName;

  • (void) setSerialNumber:(NSString *)serialNumber;
  • (void) setValueInDollars:(int)i;
  • (NSString *)itemName;
  • (NSString *)serialNumber;
  • (int)valueInDollars;
  • (NSDate *)dateCreated;[/code]

BUT: Now I get the sig term error:

2013-09-26 17:35:31.390 RandomPosessions[55367:303] Zero 2013-09-26 17:35:31.394 RandomPosessions[55367:303] One 2013-09-26 17:35:31.395 RandomPosessions[55367:303] Two 2013-09-26 17:35:31.396 RandomPosessions[55367:303] Three 2013-09-26 17:35:31.398 RandomPosessions[55367:303] <null> 2013-09-26 17:35:31.399 RandomPosessions[55367:303] -[BNRItem serialNumber]: unrecognized selector sent to instance 0x100117230 2013-09-26 17:35:31.402 RandomPosessions[55367:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BNRItem serialNumber]: unrecognized selector sent to instance 0x100117230' *** First throw call stack: ( 0 CoreFoundation 0x00007fff8485bf56 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff8215bd5e objc_exception_throw + 43 2 CoreFoundation 0x00007fff848e81be -[NSObject doesNotRecognizeSelector:] + 190 3 CoreFoundation 0x00007fff84848e23 ___forwarding___ + 371 4 CoreFoundation 0x00007fff84848c38 _CF_forwarding_prep_0 + 232 5 RandomPosessions 0x00000001000016c2 main + 658 6 RandomPosessions 0x0000000100001424 start + 52 7 ??? 0x0000000000000001 0x0 + 1 ) terminate called throwing an exception(lldb)

It seems to be an error with BNRItem serialNumber, looking at the debug log.
And I cleaned the project as well, still no dice.


#4

It’s frustrating to see a purchase of a book being ruined by already the second chapter,
although I can understand its a steep learning curve and therefore probably the hardest
to grasp initially, but still getting stuck in the first chapters is not such a good start for a book sadly.

And ibex10, could you please look at my code again, its not still not working right, giving me sig term error at main.m, thanks.


#5

2 things:

1.) You need to add the getter for serialNumber in BNRItem.m like you did for itemName

-(NSString *)serialNumber{ return serialNumber; }

2.) Go ahead and change where you are setting the valueInDollars to use an int instead of a string in main.m


#6

Ahh thanks so much jonesy!
I love this forum already, so I cleared that sig term error, its not a fault from the book I just skipped two pages, which were stuck together, all is fine.