这段代码为什么给我EXC_BAD_ACCESS?
问题描述:
当followLink被调用时,我得到了不好的访问。如果我将openURL行粘贴到textContainsURL中,就不会发生这种情况,所以我假设方法完成后该对象不再存在?我是新手,但是我虽然ARC应该为你处理这种事情?这段代码为什么给我EXC_BAD_ACCESS?
@interface MyViewController : UIViewController
{
NSURL *newsURL;
}
@end
以下是在执行:
- (void)followLink
{
[[UIApplication sharedApplication]openURL:newsURL];
}
- (BOOL)textContainsURL:(NSString*)text
{
NSError *error = NULL;
//scan text to see if there is a link and only set this up if there is
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
NSArray *matches = [detector matchesInString:text
options:0
range:NSMakeRange(0, [text length])];
for (NSTextCheckingResult *match in matches)
{
//NSRange matchRange = [match range];
if ([match resultType] == NSTextCheckingTypeLink)
{
newsURL = [[NSURL alloc] init];
newsURL = [match URL];//what's the void state? retain it
return YES;
}
}
return NO;
}
答
你应该匹配的网址复制到newsURL伊娃或者让你的newsURL伊娃副本属性,并通过访问方法设置的值。在你当前的代码中,URL是自动发布的。
'newsURL = [[NSURL alloc] init]是什么意思;'?你在哪里定义newsURL? – hypercrypt 2012-01-31 12:39:52
之后您再次分配它时,不需要alloc-init'newsURL'。这只是一个方面的说明,而不是解决方案(假设ARC不是越野车)。 – 2012-01-31 12:40:39
好的,我其实只是在一个不知情的情况下补充说明了解决问题的方法。我将编辑该问题。并且newsURL位于标题中。我也会这样做。 – SirYakalot 2012-01-31 12:41:41