2012-03-19 53 views
0

我試圖讓使用Twitter的API鳴叫,但比較兩個字符串的麻煩出於某種原因這個問題就要到了,每當我嘗試刷新微博:已經從Twitter API的iPhone

[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x6c22e70 

獲取第一次發佈tweets並填充「tweetssofar」正確地發生,當我NSLog fromtwitternew和現存的問題時,兩者都顯示相同的事情。兩者都是實際上是字典的字符串,但爲什麼我不能以這種方式比較它們?謝謝!

- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier { 
NSString *fromtwitternew = [statuses objectAtIndex:0]; 
    NSString *existingtwitter = [tweetssofar objectAtIndex:0]; 
    NSLog(@"No problem so far"); 
    if ([fromtwitternew isEqualToString:existingtwitter]) {  //No new tweets 
     NSLog(@"No new tweets"); 
     contents = [[NSDictionary alloc] init]; 

     contents = [tweetssofar objectAtIndex:counter]; 
} 

回答

0

@Prajoth用下面的代碼,我相信你將能夠解決您的問題

- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier { 
    if ([statuses count] > 0 && [tweetssofar count] > 0) { 

     id fromtwitternew = [statuses objectAtIndex:0]; 
     if ([fromtwitternew isKindOfClass:[NSDictionary class]]) { 
      //Handle code accroding to Dictionary Results 

      contents = [[NSDictionary alloc] init]; 

      contents = [tweetssofar objectAtIndex:counter];    
     } 
     else if ([fromtwitternew isKindOfClass:[NSString class]]) { 
      //Make Sure that you have added Only Strings to tweetssofar , in case You have added dictionary you can not use isEqualToString 
      NSString *existingtwitter = [tweetssofar objectAtIndex:0]; 
      if ([fromtwitternew isEqualToString:existingtwitter]) {  //No new tweets 
       NSLog(@"No new tweets"); 
      } 

     } 
    } 
} 

崩潰很可能是因爲您正在比較isEqualToString:方法來比較您不應該做的兩個字典對象。如果你需要比較兩個推文,你應該首先從字典中提取字符串,然後比較它們。

1

isEqualToString:僅適用於字符串,以及那些微博可能是由解析器解析成字典JSON或XML字符串。如果你想知道兩個解析的鳴叫是否相等,你可以這樣對它們進行比較:

[[tweetA valueForKey:@"id"] isEqualToNumber:[tweetB valueForKey:@"id"]];