在已定义的函数上获取错误访问

时间:2014-01-11 02:50:33

标签: ios objective-c cocoa-touch

我一直在某个类的某些函数上获得EXC_BAD_ACCESS。如果我改变了函数的名称,错误似乎就消失了......但我想知道为什么会发生这种情况,因为我碰巧喜欢我正在给我的函数的名字。

这是我的文件 -

@property(strong, nonatomic)NSString* month;
@property(strong,nonatomic)NSString* day;
@property(strong,nonatomic)NSString* description;
@property(strong,nonatomic)NSString* type;
@property(strong,nonatomic)NSString* venue;

@property(weak,nonatomic)IBOutlet UIImageView* imageView;
@property(weak,nonatomic)IBOutlet UILabel* descriptionLabel;
@property(weak,nonatomic)IBOutlet UILabel* venueLabel;
@property(weak,nonatomic)IBOutlet UILabel* titleLabel;
@property(weak,nonatomic)IBOutlet UILabel* typeLabel;
@property(weak,nonatomic)IBOutlet UILabel* dateLabel;

@property(weak,nonatomic)IBOutlet UILabel* monthLabel;
@property(weak,nonatomic)IBOutlet UILabel* dayLabel;

-(void)setDate:(NSString*)month withDay:(NSString*)day;
-(void)setImage:(UIImage*)image;

这些是我的二传手 -

-(void)setDescription:(NSString *)description{
    self.description = description;
    self.descriptionLabel.text = description;
}

-(void)setTitle:(NSString *)title{
    self.title = title;
    self.titleLabel.text = title;
}

-(void)setType:(NSString *)type{
    self.type = type;
    self.typeLabel.text = type;
}


-(void)setVenue:(NSString *)venue{
    self.venue = venue;
    self.venueLabel.text = venue;
}

-(void)setDate:(NSString *)month withDay:(NSString *)day{
    self.month = month;
    self.day = day;
}

-(void)setImage:(UIImage*)image{
    self.imageView.image = image;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    self.monthLabel.text = self.month;
    self.dayLabel.text = self.day;
}

如果我运行它 - 我在setTitle上得到一个EXC_BAD_ACCESS。如果我将其更改为setEventTitle,则错误消失,我在setVenue上获得EXC_BAD_ACCESS。

这就是我调用这些函数的方法 -

-(UIView*)getEventResultView:(NSDictionary*)component{
EventViewController* eventVC = [[EventViewController alloc] initWithNibName:@"EventResultView" bundle:nil];
NSDictionary* dateDictionary =  someDate;
NSString* month= [self findMonth:dateDictionary];
[eventVC setDate:month withDay:someDay];
[eventVC setTitle: someTitle];
[eventVC setVenue: someVenue];
[eventVC setDescription:someDescription];

NSString* titleImageUrl = someUrl;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

dispatch_async(queue, ^{
    NSData *titleImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:titleImageUrl]];
    UIImage* titleImage = [UIImage imageWithData:titleImageData];
    [eventVC setImage: titleImage];
});

return eventVC.view;

}

为什么会这样?

1 个答案:

答案 0 :(得分:3)

你有一个无休止的递归:

-(void)setType:(NSString *)type
{
    self.type = type;
    …
}

就在这里:

self.type = …;

的缩写形式
[self setType:…];

因此,在执行时会调用该方法(代码中没有函数)。

这样做:

-(void)setType:(NSString *)type
{
    _type = type;
    …
}