Again i need help with my code


#1

so every thing is typed right and all that. the line in BNRItem that says + (id)randomItem when i go to build the program is giving me problems though it says that i need a ; but then when i put a ; then the line below it with the curly braces ({) says that it needs a (() instead whats the problem here

here are my codes main.m

[code]//
// main.m
// RandomPossessions
//
// Created by Shania Bell on 10/29/13.
// Copyright © 2013 Shania Bell. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “BNRItem.h”

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

@autoreleasepool {
    NSMutableArray *items = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 10; i++) {
        BNRItem *p = [BNRItem randomItem];
        [items addObject:p];
    }
    
    for (int i = 0; i < [items count]; i++) {
        NSLog(@"@%@", [items objectAtIndex:i]);
    }
    
    items = nil;
}   

return 0;

}[/code]

BNRItem.h

[code]//
// BNRItem.h
// RandomPossessions
//
// Created by Shania Bell on 10/29/13.
// Copyright © 2013 Shania Bell. All rights reserved.
//

#import <Foundation/Foundation.h>

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

}

  • (id)randomItem;
  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber;
  • (id)randomItem -------------------------> expected ; after prototype then after i put that in
    { -------------------------> expected identifier or ‘(’
    // Create an array of three acjectives
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@“Fluffy”, @“Rusty”, @“Shiny”, nil];

    // Create an array of three nouns
    NSArray *randomNounList = [NSArray arrayWithObjects:@“Bear”, @“Spork”, @“Mac”, nil];

    // Get the index of a random adjective/noun from the lists
    // Note: The % operator, called the modulo operator, gives
    // you the remainder. So adjectiveIndex is a random number
    // from 0 to 2 inclusive.
    NSInteger adjective Index = rand() % [randomAdjectiveList count];
    NSInteger nounIndex = rand() % [randomNounList count];

    // Note that NSInteger is not an object, nut a type definition for “unsigned long”

    NSString *randomName = [NSSring stringWithFormat:@"@%@", [randomAdjectiveList objectAtIndex:adjectiveIndex], [randomNounList objectAtIndex:nounIndex]];

    int randomValue = rand() % 100;

    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c", ‘0’ + rand() % 10, ‘A’ + rand() % 26, ‘0’ + rand() % 10, ‘A’ + rand() % 26, ‘0’ + rand() %10,];

    BNRItem *newItem = [[self alloc] initWithItemName:randomName
    valueInDollars:randomValue
    serialNumber:randomSerialNumber];
    return newItem;
    }

  • (void)setItemName:(NSString *)str;

  • (NSString *)itemName;

  • (void)setSerialNumber:(NSString *)str;

  • (NSString *)serialNumber;

  • (void)setValueInDollars:(int)i;

  • (int)valueInDollars;

  • (NSDate *)dateCreated;

@end[/code]

BNRItem.m

[code]//
// BNRItem.m
// RandomPossessions
//
// Created by Shania Bell on 10/29/13.
// Copyright © 2013 Shania Bell. All rights reserved.
//

#import “BNRItem.h”

@implementation BNRItem

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

    // Did superclass’s designated initalizer succeed?
    if (self) {
    // Give the instance variables inital values
    [self setItemName:name];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    dateCreated = [[NSDate alloc] init];
    }

    // Return the address of the newly initialized object
    return self;
    }

  • (id)init
    {
    return [self initWithItemName:@“Item” 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)i
    {
    valueInDollars = i;
    }

  • (int)valueInDollars
    {
    return valueInDollars;
    }

  • (NSDate *)dateCreated
    {
    return dateCreated;
    }

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

    return descriptionString;
    }
    @end[/code]

thanks


#2

The implementation for your randomItem: method should be in BNRItem.m not BNRItem.h, just as you have done for the other methods.


#3

so i did that and 6 more errors came up. ccan you post what my codes should look like please so i can compare because i am like just leaning and this is really really difficult and confusing when its not working and im getting frustrated


#4

You can download the sample code for all of the chapters direct from BNR at: http://www.bignerdranch.com/book/ios_programming_the_big_nerd_ranch_guide_rd_edition_

Nick


#5

so the difference between my program and the original program is mine is QuizApp not BNRApp is that going to make that much of a difference??