开发者

NSInvalidArgumentException

I'm working through "iPhone Development: The Big Nerd Ranch Guide". The project is "Random Possessions". I am faithfully following the code in the book and it compiles OK but I get this exception when running it:

    **2010-07-04 11:43:48.511 RandomPosessions[14828:a0f] -[Posession initWithPosessionName:valueInDollars:serialNumber:]: unrecognized selector sent to instance 0x10010d1f0
2010-07-04 11:43:48.514 RandomPosessions[14828:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Posession initWithPosessionName:valueInDollars:serialNumber:]: unrecognized selector sent to instance 0x10010d1f0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00007fff80b4bcc4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff8044b0f3 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff80ba5140 +[NSObject(NSObject) doesNotRecognizeSelector:] + 0
    3   CoreFoundation                      0x00007fff80b1dcdf ___forwarding___ + 751
    4   CoreFoundation                      0x00007fff80b19e28 _CF_forwarding_prep_0 + 232
    5   RandomPosessions                    0x00000001000016c2 +[Posession randomPosession] + 995
    6   RandomPosessions                    0x0000000100001196 main + 170
    7   RandomPosessions                    0x00000001000010e4 start + 52
)
terminate called after throwing an instance of 'NSException'**

THis is my code for the implementation for "RandomPosession":

#import "Posession.h"


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

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

}

+(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];

    Posession *newPosession = 
        [[self alloc] initWithPosessionName:randomName
                            valueInDollars:randomValue
                          serialNumber:randomSerialNumber];

    return newPosession;

}

-(id)init{
    return [self initWithPosessionName:@"Posession"
                        valueInDollars:0
                          serialNumber:@""];

}
-(id)initWithPosessionName: (NSString *)pName
           valuesInDollars:(int)value
              serialNumber:(NSString *)sNumber{
    [super init];

    if(!self)
        return nil;

    [self setPosessionName:pName];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    dateCreated = [[NSDate alloc] init];

    return self;

}

-(id)initWithPosessionName:(NSString *)pName {
        return [self initWithPosessionName:pName
                           valuesInDollars:0 
                              serialNumber:@""];

}



@end

I'm pulling my hair out by clumps! I'd appreciate any help Cheers


You need to format your input better....

But your issue is you defined the method incorrectly

-(id)initWithPosessionName: (NSString *)pName valuesInDollars:(int)valueserialNumber:(NSString *)sNumber

that should be

-(id)initWithPosessionName: (NSString *)pName valueInDollars:(int)valueserialNumber:(NSString *)sNumber

You had an extra 's'

I can almost guarantee Xcode was giving you warnings about this. In Objective-C warnings are generally going to cause you runtime crashes, such as this. It is a good habit to clean up all your warnings before giving up on an issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜