如何修复NSInvalidArgumentException和UncaughtException?

时间:2014-09-07 01:23:45

标签: ios objective-c nsstring

请接受我,因为我是Objective-C语言的新手。我正在尝试初始化initWithName()方法,但它不起作用。我不确定为什么当我在这里查看其他答案时,它表示.m& .h文件必须与它们所使用的方法相匹配。不确定问题的根源在哪里。

错误:

2014-09-06 18:14:17.832 NPF-1[3788:60b] -[Park initWithName:location:formed:area:link:]: unrecognized selector sent to instance 0x8dbf6d0
2014-09-06 18:14:17.838 NPF-1[3788:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Park initWithName:location:formed:area:link:]: unrecognized selector sent to instance 0x8dbf6d0'

Park.m文件中@implementation Park的位置,表示initWithName的方法定义:location:formed:area:link not found。

.m 档案:

#import "Park.h"

@implementation Park

// implement all getter/setter methods

-(id) init{
/*self = [super init];
if(self){
    //initalization code goes here
    [self setParkName:parkName]; // calling mutator
    [self setParkLocation:parkLocation]; // calling mutator
    [self setDateFormed:dateFormed]; // calling mutator
    [self setArea:area]; // calling mutator
    [self setLink:link]; // calling mutator
}
return self;*/
// init method that will call the designated initializer with default values
return[self initWithName:@"unknown" location:@"unknown" formed:@"unknown" area:@"unknown" link:@"unknown"];
}

-(NSString *) parkName{
return parkName;
}

-(void) setParkName:(NSString *) value{
parkName = value;
}

-(NSString *) parkLocation{
return parkLocation;
}

-(void) setParkLocation:(NSString *) value{
parkLocation = value;
}

-(NSString *) dateFormed{
return dateFormed;
}

-(void) setDateFormed:(NSString *) value{
dateFormed = value;
}

-(NSString *) area{
return area;
}

-(void) setArea:(NSString *) value{
area = value;
}

-(NSString *) link{
return link;
}

-(void) setLink:(NSString *) value{
link = value;
}

// format and prints out all ivars, returning their values
-(NSString *) description{
return [NSString stringWithFormat:@"parkName=%@, parkLocation=%@, dateFormed=%@, area=%@, link=%@",
        parkName, parkLocation, dateFormed, area, link];
}

@end

.h 档案:

#import <Foundation/Foundation.h>

@interface Park : NSObject{
// declare ivars
NSString *parkName;
NSString *parkLocation;
NSString *dateFormed;
NSString *area;
NSString *link;
}

// designated initializer
-(id)initWithName:(NSString *)p_name
     location:(NSString *)p_location
       formed:(NSString *)p_formed
         area:(NSString *)p_area
         link:(NSString *)p_link;

-(NSString *)parkName; //getter/accessor
-(void) setParkName:(NSString *) value; //setter/mutator

-(NSString *)parkLocation;
-(void) setParkLocation:(NSString *) value;

-(NSString *)dateFormed;
-(void) setDateFormed:(NSString *) value;

-(NSString *)area;
-(void) setArea:(NSString *) value;

-(NSString *)link;
-(void) setLink:(NSString *) value;

// accessor method (getter)
//mutator method (setter)
/*-(NSString *) parkName;
-(void) setParkName:(NSString *) value;

-(NSString *) parkLocation;
-(void) setParkLocation:(NSString *) value;

-(NSString *) dateFormed;
-(void) setDateFormed:(NSString *) value;

-(NSString *) area;
-(void) setArea:(NSString *) value;

-(NSString *) link;
-(void) setLink:(NSString *) value;*/

@end

NPF-1AppDelegate.m 文件:

#import "NPF_1AppDelegate.h"
#import "Park.h" // import the class to use Park object

@implementation NPF_1AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

// added to test the following class
Park *p1 = [[Park alloc] init];
NSLog(@"p1 is %@", p1);

Park *p2 = [[Park alloc] initWithName:@"Acadia National Park"
                               location:@"Maine"
                                 formed:@"1919-02-26"
                                   area:@"47,389.67 acres (191.8 square km)"
                                   link:@"TBD"];
NSLog(@"p2 is %@", p2);

return YES;
}

1 个答案:

答案 0 :(得分:1)

您已定义方法但尚未实施。您的 .m 文件需要initWithName:location:formed:area:link:的实施。类似的东西:

-(id)initWithName:(NSString *)p_name
 location:(NSString *)p_location
   formed:(NSString *)p_formed
     area:(NSString *)p_area
     link:(NSString *)p_link {
    self = [super init];
    if(self){
        //initalization code goes here
        [self setParkName:p_name]; // calling mutator
        [self setParkLocation:p_location]; // calling mutator
        [self setDateFormed:d_formed]; // calling mutator
        [self setArea:p_area]; // calling mutator
        [self setLink:p_link]; // calling mutator
    }
    return self;
}

此外,您可能希望将parkName等声明为属性而非iVars,以便它们自动合成 - 您不必编写所有的getter和setter。< / p>