【问题标题】:getting a EXC_BAD_ACCESS (code=2, address=0x0) error收到 EXC_BAD_ACCESS (code=2, address=0x0) 错误
【发布时间】:2014-01-01 18:20:57
【问题描述】:

我的代码中出现错误的访问错误,但我不知道为什么。

这是我的代码

-(IBAction)datePickerValueChanged:(UIDatePicker *)sender{
    myDatePicker = [[myDatePickerView subviews] lastObject];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:sender.locale];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    activeTextField = (UITextField *) [self.view viewWithTag: *(tag)];
    [activeTextField setText:[dateFormatter stringFromDate:[sender date]]];
}

P.S:我在@interface 中创建了activeTextField 属性,就像这样

@property (nonatomic,strong) UITextField *activeTextField;

我合成了它

@synthesize activeTextField;

【问题讨论】:

  • 这是什么*(tag)
  • 在哪一行中断 excaclty 以及 *(tag) 是什么?
  • tag是UIView的一个属性,@property (nonatomic) NSInteger *tag;
  • 它中断:activeTextField = (UITextField *) [self.view viewWithTag: *(tag)];

标签: ios objective-c exc-bad-access


【解决方案1】:

尝试替换此行

activeTextField = (UITextField *) [self.view viewWithTag: *(tag)];

activeTextField = (UITextField *) [self.view viewWithTag: tag];//tag should be an integer

【讨论】:

  • 为了澄清@Bhumeshwer 的回答,在评估表达式前面放置一个星号会取消引用一个指针。由于标签很可能是一个整数(而不是指向整数的指针),取消引用它会产生一些讨厌的副作用。
  • 标签是 NSInteger 而不是 int
  • @iOSMaster:NSInteger 不是对象;它是 int 的 typedef 32 位或 64 位,具体取决于处理器。
猜你喜欢
  • 1970-01-01
  • 2014-09-19
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-10
相关资源
最近更新 更多