0

わかりました、私はこれにかなり慣れていないので、皆さんが感じるかもしれないことに苦労してきましたが、これは非常に簡単なエクササイズです。高低を調べましたが、これを行う方法に関する適切なチュートリアルやウォークスルーが見つかりません。基本的に、以下のコードを使用してツイートを取得しています。ツイートの「テキスト」部分のみが必要です。テーブルビューで「テキスト」キーを使用するために、NSDictionary から抽出するにはどうすればよいですか? 試してみ[dict objectForKey:@"text"]ましたが、うまくいきません。'dict' に 'text' 属性が含まれていないようです。助けてくれてありがとう。

// Do a simple search, using the Twitter API
TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
   @"http://search.twitter.com/search.json?q=iOS%205&rpp=5&with_twitter_user_id=true&result_type=recent"] 
   parameters:nil requestMethod:TWRequestMethodGET];

// Notice this is a block, it is the handler to process the response
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
  if ([urlResponse statusCode] == 200) 
  {
    // The response from Twitter is in JSON format
    // Move the response into a dictionary and print
    NSError *error;        
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
    NSLog(@"Twitter response: %@", dict);                           
  }
  else
    NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
}];
4

1 に答える 1

2

はい、objectForKey@"text" がありますが、これはすべてのエントリ (ツイート) にテキスト (および他のいくつかの属性) があることを意味する配列です。そのため、ツイートをループして、すべてのツイートのテキストを取得する必要があります。

.h ファイル内

         NSMutableArray *twitterText;

.m ファイル内

これをviewdidloadのどこかで行う

         twitterText = [[NSMutableArray alloc] init];

これで、結果をループできます。このコードを NSLog(@"Twitter response: %@", dict); がある場所に貼り付けます。

          NSArray *results = [dict objectForKey@"results"];

         //Loop through the results

         for (NSDictionary *tweet in results)
         {
             // Get the tweet
             NSString *twittext = [tweet objectForKey:@"text"];

             // Save the tweet to the twitterText array
             [twitterText addObject:(twittext)];

そして、あなたのtableViewのあなたのセルのために

         cell.textLabel.text = [twitterText objectAtIndex:indexPath.row];

私はそれが機能するはずだと思います。

于 2012-07-02T22:33:15.047 に答える