0

次のようにヘッダーNSString *oldChatファイルに設定しました:

@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)

4

2 に答える 2

1

oldChat はautoreleaseオブジェクトです。それを保持し、文字列比較に isEqualToString を使用します。

 NSString *chat = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://theshay.byethost7.com/chat.php"] encoding:NSUTF8StringEncoding error:nil];
 oldChat = chat;
[oldChat retain];
于 2013-02-22T12:23:27.303 に答える
0

NSStringが等しいか、isEqualToString:メソッドを使用していないかを確認する必要があります。コードは次のようになります

if (![chat isEqualToString:oldChat]){
    //Do something
}
于 2013-02-22T12:22:20.200 に答える