NSInvalidArgumentException

时间:2010-07-04 06:57:19

标签: iphone objective-c xcode

我正在研究“iPhone开发:大书呆子牧场指南”。该项目是“随机占有”。我忠实地遵循书中的代码并且它编译好但是在运行时我得到了这个例外:

    **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'**

这是我执行“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

我把头发拉出来! 我很感激任何帮助 干杯

1 个答案:

答案 0 :(得分:1)

您需要更好地格式化输入....

但是你的问题是你错误地定义了方法

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

应该是

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

你有一个额外的's'

我几乎可以保证Xcode会给你关于此的警告。在Objective-C中,警告通常会导致运行时崩溃,例如这种情况。在放弃某个问题之前清理所有警告是一个好习惯。