调用followLink时,我得到了错误的访问权限。如果我将openURL行粘贴到textContainsURL中,这不会发生,所以我假设一旦方法完成后对象不再存在?我是新手,但我虽然ARC应该为你处理这类事情?
@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;
}
答案 0 :(得分:1)
您应该将匹配的网址复制到您的newsURL ivar或将您的newsURL ivar复制属性并通过访问者方法设置值。在您当前的代码中,该URL是自动释放的。