initWithPossessionName Error


#1

I’ve finished writing the entire source code required in this chapter and have gone over it several times, but still can’t seem to get rid of an error that is keeping the whole code from building. On page 38 it asked me to declare and implement a method called “- (id)initWithPossessionName:(NSString *)pName”. I have it coded exactly as the books asks, but am getting the error. I’ve been going over it for hours now and I’m sure I’m just being an idiot, but I’m lost. The errors are: “initWithPossessionName undeclared” (even though it looks just as the book shows) and “Expected ‘;’ before ‘:’ token”. Please help!


#2

It would help if you pasted your source code in the forum.

The compiler thinks you’re missing a semicolon terminating a line prior to whatever line is showing the error. This could mean you left off a semicolon. It could mean you’ve got a bracket in the wrong place, or some other “punctuation” error. Without seeing the code it’s hard to say.

Also, did you put the body of the method in the .m file? Forgive me if that’s a basic question, but I’m not sure what your level of experience is with Obj-C.


#3

I did separate it, but thanks for not assuming me a pro at this. I’m still even adding in the books comments to my code. Sorry that this is so long, but here’s the .h:

#import <Foundation/Foundation.h>

@interface Posession : NSObject {
NSString *posessionName;
NSString *serialNumber;
int valueInDollars;
NSDate *dateCreated;
}

@property (nonatomic, copy) NSString *posessionName;
@property (nonatomic, copy) NSString *serialNumber;
@property (nonatomic) int valueInDollars;
@property (nonatomic, readonly) NSDate *dateCreated;

  • (id) randomPosession;
  • (id) initWithPosessionName: (NSString *) pName
    valueInDollars: (int)value
    serialNumber: (NSString *)sNumber;

  • (id)initWithPosessionName:(NSString *)pName;

@end

And the .m:

#import “Posession.h”

@implementation Posession
@synthesize posessionName, serialNumber, valueInDollars, dateCreated;

  • (id) randomPosession
    {
    static NSString *randomAdjectiveList[3] =
    {@“Fluffy”,
    @“Rusty”,
    @“Shiny”
    };
    static NSString *randomNounList[3] =
    {
    @“Bear”,
    @“Spork”,
    @“Mac”,
    };

    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
    randomAdjectiveList[random() % 3],
    randomNounList[random() % 3]];

    int randomValue = random() % 100;

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

    // Once again ignore the memory problems with this method
    // We use “self” instead of the name of the class in class methods…
    // Keep reading to find out why
    Posession *newPosession =
    [[self alloc] initWithPosessionName:randomName
    valueInDollars:randomValue
    serialNumber:randomSerialNumber];
    return newPosession;
    }

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

  • (id) initWithPosessionName:(NSString *)pName valueInDollars:(int)value serialNumber:(NSString *)sNumber
    {
    // Call the superclass’s designated intializer
    self = [super init];

    // Did the superclass’s designated initializer fail?
    if (!self) {
    return nil;
    }

    // Give the instance variables initial values
    [self setPosessionName:pName];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    dateCreated = [[NSDate alloc] init];

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

    • (id) initWithPosessionName: (NSString *) pName
      {
      return [self initWithPosessionName:pName
      valueInDollars:0
      serialNumber:@""];
      }
  • (id) init
    {
    return [self initWithPosessionName:@“Posession"
    valueInDollars:0
    serialNumber:@”"];
    }

@end

I know the word “Possession” is spelled wrong, but after realizing it I just used the misspelling throughout. I thought that could be the issue, but I just can’t seem to find it. Thanks so much for your help!!


#4

The error is coming up on the second to last method in the .m file. - (id) initWithPosessionName


#5

I don’t see a “}” to end the previous method in your class listing, after returning self. See my comment in the excerpt below.

Hopefully that will do the trick. :slight_smile:

[code]

  • (id) initWithPosessionName:(NSString *)pName valueInDollars:(int)value serialNumber:(NSString *)sNumber
    {
    //… stuff

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

/***** try adding this closing brace on the next line, you need it to end this method ************/
}

  • (id) initWithPosessionName: (NSString *) pName
    {
    return [self initWithPosessionName:pName
    valueInDollars:0
    serialNumber:@""];
    }[/code]

#6

THANKS!!! I can’t believe I missed that! :smiley: Changed it and it’s all fixed up. I feel like an idiot, but I’m glad it wasn’t more complicated.


#7

If you learned something, then no need to feel like an idiot. :slight_smile:


#8

So I fixed it and it’s running without any errors. Now I’m getting this in the console, which is not what I think I’m supposed to be getting. I guess these kinds of problems are what’s really gunna teach me! :wink: Any ideas?
http://grab.by/5FHw


#9

Looks like it ran normally, without error. It isn’t doing what you intended, but it seems to be running and not crashing. :slight_smile:

Did you implement the full main method? If so, want to post it?

(If you use the Code button, you can enclose your code in, er, Code tags. That makes it a little easier to read.)


#10

I hope this is what you were talking about. The book seems to say that “Console” should read some randomly generated adjectives and nouns. Instead, I get that warranty information that you saw. I don’t even know where that came from! :confused:

[code]
#import “Posession.h”

@implementation Posession;
@synthesize posessionName, serialNumber, valueInDollars, dateCreated;

  • (id) randomPosession
    {
    static NSString *randomAdjectiveList[3] =
    {@“Fluffy”,
    @“Rusty”,
    @“Shiny”
    };
    static NSString *randomNounList[3] =
    {
    @“Bear”,
    @“Spork”,
    @“Mac”,
    };

    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
    randomAdjectiveList[random() % 3],
    randomNounList[random() % 3]];

    int randomValue = random() % 100;

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

    // Once again ignore the memory problems with this method
    // We use “self” instead of the name of the class in class methods…
    // Keep reading to find out why
    Posession *newPosession =
    [[self alloc] initWithPosessionName:randomName
    valueInDollars:randomValue
    serialNumber:randomSerialNumber];
    return [newPosession autorelease];
    }

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"%@ (%@): Worth $%d, Recorded on %@",
    posessionName,
    serialNumber,
    valueInDollars,
    dateCreated];
    }

  • (id) initWithPosessionName:(NSString *)pName valueInDollars:(int)value serialNumber:(NSString *)sNumber
    {
    // Call the superclass’s designated intializer
    self = [super init];

    // Did the superclass’s designated initializer fail?
    if (!self)
    {
    return nil;
    }

    // Give the instance variables initial values
    [self setPosessionName:pName];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    dateCreated = [[NSDate alloc] init];

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

  • (id) initWithPosessionName: (NSString *) pName
    {
    return [self initWithPosessionName:pName
    valueInDollars:0
    serialNumber:@""];
    }

  • (id) init
    {
    return [self initWithPosessionName:@“Posession"
    valueInDollars:0
    serialNumber:@”"];
    }

  • (void)dealloc
    {
    [posessionName release];
    [serialNumber release];
    [dateCreated release];
    [super dealloc];
    }

@end[/code]


#11

I was thinking of your RandomPossessions.m file, which has the main function in it.

Inside of main, you should have the following:

for (Possession *item in items) { NSLog(@"%@", item); }

The NSLog call is what prints your items out, it uses your Possession -description method to produce a string (NSString) representation.

Because your debugger console (where you noticed the warranty) doesn’t show any output, and your program exits with a 0, I am assuming there is something wrong in your main function.


#12

[code]#import <Foundation/Foundation.h>

#import “Posession.h”

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSMutableArray *items = [[NSMutableArray alloc] init];

for (Posession *item in items)
{
	NSLog(@"%@", item);
}
[items release];
items = nil;

[pool drain];
return 0;

}[/code]

I have reached chapter ten and am now rereading the code because I need to implement it in this chapter. Hoping the skills I’ve been racking up will help me find the mistake!


#13

Finished Chapter Ten and whatever is causing the issue with the Possessions. is causing the built app to crash. :frowning:


#14

I don’t have the book in front of me right now, but I don’t see where you’re populating your array.

You create the NSMutableArray with alloc and init, and then you start looping through it to print out the Possession items. But in between there, the array should be populated.

In this code, the array is empty and so there is nothing to print in the loop.

Hence the empty debug output.


#15

At first glance, you are spelling an awful lot of stuff as “Posession” with only one ‘s’, which will cause a problem.

When chapter ten crashes, what are you seeing in the debug console?


#16

The link to the console result is in one of my previous posts, but here it is again: cl.ly/1d12804094c39d6a9522 As for all the "Posession"s, as the discussion has been going on for a while, I have since had time to change all of the misspellings. :smiley: I’m looking into the array thing now…


#17

ALL IS WELL! Turns out main function was wayyyyyyyyy off. I don’t know if it was just the default that was in there or what, but anything I had typed was wrong. Went back through the book and fixed the main, re-added Possessions .m and .h to Homepwner and everything worked out fine. Thanks so much for the help!!! :smiley: :smiley: :smiley: :smiley: