-2

Twitter フィードを含むテーブルビューを iOS アプリに実装しようとしています。チュートリアルに従って、ユーザーの Twitter フィードを取得できました。ハッシュ タグの URL を見つけましたが、実装するとエラーが発生します。

'NSInvalidArgumentException'、理由: '-[__NSCFString objectForKeyedSubscript:]: 認識されないセレクターがインスタンス 0xa0b7e20 に送信されました'

いくつかの調査を試みましたが、すべての試みが失敗しました。どんなアドバイスも素晴らしいでしょう。ありがとうございました。

.h

#import <UIKit/UIKit.h>
#import <Social/Social.h>
#import <Accounts/Accounts.h>
@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tweetTableView;
@property (nonatomic, copy) NSArray *dataSource;
-(IBAction)refresh:(id)sender;
@end

.m

@implementation ViewController

- (void)getTimeLine {
    ACAccountStore *account = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [account
                                  accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [account requestAccessToAccountsWithType:accountType
                                     options:nil completion:^(BOOL granted, NSError *error)
     {
         if (granted == YES)
         {
             NSArray *arrayOfAccounts = [account
                                         accountsWithAccountType:accountType];

             if ([arrayOfAccounts count] > 0)
             {
                 ACAccount *twitterAccount = [arrayOfAccounts lastObject];
                 /*                 NSString * kTwitterHashtag = @"#nasa";
                  NSString * kTwitterUsername = @"";

                  // Looking for #kTwitterHashtag or @kTwitterUsername
                  NSString *urlString = [[[@"http://search.twitter.com/search.json?q=%23" stringByAppendingString:(NSString *)kTwitterHashtag]
                  stringByAppendingString:@"+OR+%40"] stringByAppendingString:(NSString *)kTwitterUsername];



                  NSURL *requestURL = [NSURL URLWithString:urlString];
                 */ 
                 NSURL *requestURL = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.json"];

                 NSMutableDictionary *parameters =
                 [[NSMutableDictionary alloc] init];
                 [parameters setObject:@"20" forKey:@"count"];
                 [parameters setObject:@"1" forKey:@"include_entities"];

                 SLRequest *postRequest = [SLRequest
                                           requestForServiceType:SLServiceTypeTwitter
                                           requestMethod:SLRequestMethodGET
                                           URL:requestURL parameters:parameters];

                 postRequest.account = twitterAccount;

                 [postRequest performRequestWithHandler:
                  ^(NSData *responseData, NSHTTPURLResponse
                    *urlResponse, NSError *error)
                  {
                      self.dataSource = [NSJSONSerialization
                                         JSONObjectWithData:responseData
                                         options:NSJSONReadingMutableLeaves
                                         error:&error];

                      if (self.dataSource.count != 0) {
                          dispatch_async(dispatch_get_main_queue(), ^{
                              [self.tweetTableView reloadData];
                              NSLog(@"_dataSource.count %d",_dataSource.count);
                              for(NSDictionary * tweet in _dataSource){
                                  NSLog(@"tweet : %@",tweet[@"text"]);
                              }
                          });
                      }
                  }];
             }
         } else {
             // Handle failure to get account access
         }
     }];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    _tweetTableView = [[UITableView alloc] init];
    _dataSource = [[NSArray alloc] init];
    [self getTimeLine];
}
-(IBAction)refresh:(id)sender{
    [self getTimeLine];
    [self.tweetTableView reloadData];
}
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tweetTableView
                             dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *tweet = _dataSource[[indexPath row]];
    NSLog(@"tweet : %@",tweet);
    cell.textLabel.text = tweet[@"text"];
    return cell;
}


@end
4

1 に答える 1

0
  self.dataSource = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];

これは文字列を返しています...しかし、あなたはそれをNSArrayに割り当てています!

于 2013-03-19T02:23:04.740 に答える