ARC Semantic Issue RandomItems normal


#1

hi guys,
This is my first time here :slight_smile:
But with all my code everything works besides the “main.m” file I put the code below and highlight the error below.


#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"];
        
        // Send another message, insertObject:atIndex, to that array object
        [items insertObject:@"zero" atIndex:0 ];
        
        // For every item in the items array
        for (NSString *item in items) {
            // Log the description of item
            NSLog(@"%@", item);
        }
    
       [color=#FF0000] [b]BNRItem *item = [[BNRItem alloc] initWithItemName:@"Red Sofa"[/b][/color]
                                           valueInDollars:100
                                             serialNumber:@"A1B2C"];
                         
        
        // The %@ token is replaced with the result of sending
        // the discription message to the correpsonding arguement
        NSLog(@"%@", item);
        
        BNRItem *itemWithName = [[BNRItem alloc] initWithItemName:@"Blue Sofa"];
        
        NSLog(@"%@", itemWithName);
        
        BNRItem *itemWithNoName = [[BNRItem alloc] init];
        
        NSLog(@"%@", itemWithNoName);
        
        items = nil;
    }
    return 0;
}


#2

It was really hard to pick out the line being flagged as an error, but eventually I found it.

       [color=#FF0000] [b]BNRItem *item = [[BNRItem alloc] initWithItemName:@"Red Sofa"[/b][/color]
                                           valueInDollars:100
                                             serialNumber:@"A1B2C"];

(If you Previewed the post, you noticed that color and bold tags aren’t supported in code listings.)

It’s hard to say “everything works” when the code doesn’t compile. There doesn’t appear to be a typo here, so the issue is likely elsewhere.

Has initWithItemName:valueInDollars:serialNumber: been declared in BNRItem.h?

Has the method been implemented?


#3

Hey,

Thanks for your reply!

Funnily enough it is not there if that is the case can you tell me where it should be implemented and how?

Cheers again!


#4

The section on initializers – from pages 49 - 55 – is really important. It includes the code you’re missing, together with much explanation for exactly what it’s doing and why it’s doing it.


#5

I had a look at the book and it is not clear on how it is meant to be formatted.
If you can post the code below it would be a great help.


#6

Page 49: “In BNRItem.h, declare two initializers.”

- (instancetype) initWithItemName: (NSString *) name
                   valueInDollars: (int) value
                     serialNumber: (NSString *) sNumber;
                
- (instancetype) initWithItemName: (NSString *) name;

Page 51: “In BNRItem.m, implement the designated initializer within the implementation block.”

@ implementation BNRItem

- (instancetype) initWithItemName: (NSString *) name
                   valueInDollars: (int) value
                     serialNumber: (NSString *) sNumber
{
	// Call the superclass's designated initializer
	self = [super init];
	
	...
	
	return self;
}

Page 52-53: “In BNRItem.m, implement initWithItemName:.”

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

#7

Wow! it was just a miss-spelling in one of the implementations. It looks like I gotta check the spelling of my code in future! Thanks for your help though!