【发布时间】:2023-03-15 12:00:01
【问题描述】:
这里的严重问题...如果我尝试 NSLog 我的自定义对象的实例变量,我会得到 ECX_BAD_ACCESS。在我的 ViewController 中调用了以下函数,payload 包含从 url 中提取的字符串数据。
- (void) initVcardWithData:(NSString *)payload {
NSLog(@"1. initVcardWithData");
aVCard = [[vcardItem alloc] initWithPayload:payload];
VCardViewController *aVCardViewController = [[VCardViewController alloc] initWithVCard:aVCard];
[self presentModalViewController:aVCardViewController animated:YES];
[aVCard release];
}
到目前为止一切顺利。 initWithWithVCard函数如下,theVCard和theVCardN在@implementation中定义,也在(.h)中设置为@property(nonatomic,retain)。:
-(id)initWithVCard:(vcardItem *)aVCard {
if(self = [super init]) {
theVCard = [aVCard retain];
theVCardN = [theVCard.PersonName retain];
}
NSLog(@"---- vCardViewController :: initWithVcard :: FirstName: %@", theVCard.PersonName.FirstName);
return self;
}
如果我在 ViewDidLoad 内的 ViewController aVCardViewController 中访问 theVCardN 对象,一切都会像魅力一样。我用该对象的数据设置了一些标签。
如果我随后尝试在从连接到 View 中的按钮的 IBAction 调用的函数中从 theVCardN 访问实例变量,我会在调试器控制台上收到 EXC_BAD_ACCESS 错误。尝试从实例变量中提取数据的函数如下:
-(IBAction)addressbookButtonTapped {
NSLog(@"RETAIN COUNT FOR theVCard: %i", [theVCard retainCount]);
NSLog(@"RETAIN COUNT FOR theVCardN: %i", [theVCardN retainCount]);
NSLog(@"Save to Adressbook: %@", theVCardN.FirstName);
//[self dismissModalViewControllerAnimated:YES];
}
theVCardN 在调用 NSLog 之前的 RetainCounter 输出“1”。然后 NSLog 行在调试器控制台中返回 EXC_BAD_ACCESS。
有什么想法吗?
【问题讨论】:
-
我猜是 vCard 设置不正确。
-
在 NSLog 执行之前打印 gdb 中的值。你会知道真正的问题是什么。在调试模式下,将光标放在该行上。左键单击出现的黄色标签,然后单击打印说明。你可以看到值是多少。
-
输出是:
Printing description of theVCardN.FirstName:仅此而已
标签: iphone exc-bad-access retain retaincount