2012-01-31 130 views
0

當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; 
} 
+1

'newsURL = [[NSURL alloc] init]是什麼意思;'?你在哪裏定義newsURL? – hypercrypt 2012-01-31 12:39:52

+0

之後您再次分配它時,不需要alloc-init'newsURL'。這只是一個方面的說明,而不是解決方案(假設ARC不是越野車)。 – 2012-01-31 12:40:39

+0

好的,我其實只是在一個不知情的情況下補充說明了解決問題的方法。我將編輯該問題。並且newsURL位於標題中。我也會這樣做。 – SirYakalot 2012-01-31 12:41:41

回答

1

你應該匹配的網址複製到newsURL伊娃或者讓你的newsURL伊娃副本屬性,並通過訪問方法設置的值。在你當前的代碼中,URL是自動發佈的。