2013-02-22 49 views
0

崩潰我在這樣的頭文件設置好的NSString *oldChat應用與EXC_BAD_ACCESS上背景

@interface CTFChatViewController : UIViewController { 
    NSString *oldChat; 
} 
- (void)updateChat; 
@property (nonatomic, retain) NSString *oldChat; 

@end 

,然後我使用它:

- (void)updateChat 
{ 
    NSString *chat = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://theshay.byethost7.com/chat.php"] encoding:NSUTF8StringEncoding error:nil]; 
    if (![chat isEqual:oldChat]) 
    { 
     [webView loadHTMLString:chat baseURL:nil]; 
     oldChat = chat; 
    } 

    [self performSelectorInBackground:@selector(updateChat) withObject:nil]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    NSString *chat = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://theshay.byethost7.com/chat.php"] encoding:NSUTF8StringEncoding error:nil]; 
    oldChat = chat; 
    [webView loadHTMLString:chat baseURL:nil]; 

    [self performSelectorInBackground:@selector(updateChat) withObject:nil]; 
} 

應用墜毀if (![chat isEqual:oldChat])EXC_BAD_ACCESS錯誤。 它爲什麼墜毀?

(的XCode 4.5.2,iPhone模擬器6.0 SDK基地6.0)

+0

如果你不打算使用它,你爲什麼要創造oldChat的財產? – 2013-02-22 12:30:51

回答

1

oldChat是autorelease對象。保留它並使用isEqualToString進行字符串比較。

NSString *chat = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://theshay.byethost7.com/chat.php"] encoding:NSUTF8StringEncoding error:nil]; 
oldChat = chat; 
[oldChat retain]; 
+0

工作!謝謝。 – theShay 2013-02-22 12:24:58

+0

別忘了在dealloc中釋放oldChat。 – 2013-02-22 12:26:10

0

你必須檢查你的NSString等於或不使用isEqualToString:方法。您的代碼應該是這樣的

if (![chat isEqualToString:oldChat]){ 
    //Do something 
}