0

私のコードは正常に動作するようですが、Twitter アカウントがデバイスに保存されていない場合、アプリはこのエラーでクラッシュします Terminating app due to uncaught exception 'NSRangeException', reason: ' * -[__NSArrayI objectAtIndex:]: index 0 beyond bounds空の配列の場合'

- (void)viewDidLoad
{
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    if (accountStore != nil)
    {
        ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
        if (accountType != nil)
        {
            [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
                if (granted)
                {
                    NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
                    if (twitterAccounts != nil)
                    {

                        ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];
                        if (currentAccount != nil)
                        {
                            NSString *friendListString = @"https://api.twitter.com/1.1/friends/list.json?cursor=-1&skip_status=true&include_user_entities=false";


                            SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:friendListString] parameters:nil];
                            if (request != nil)
                            {
                                // 1.1 api requires a user to be logged in.
                                [request setAccount:currentAccount];
                                [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                                    NSInteger responseCode = [urlResponse statusCode];
                                    if (responseCode == 200)
                                    {
                                    // ADD STUFFS HERE

                                            }

                                            [theCollectionView reloadData];

                                        }
                                    } 
                                }];
                            }
                        }
                    }
                }
                else
                {
                    NSLog(@"User did not grant access.");
                }

            }];
        } 
    } 
    [super viewDidLoad];
}

作成後に上記のように nil チェックがあるため、理由はわかりません。デバイスがクラッシュするのではなく、デバイスにアカウントが見つからない場合、または同様のものACAccount *currentAccount = [twitterAccounts objectAtIndex:0]; を表示するにはどうすればよいですか?UIAlertView

4

3 に答える 3

2

配列にオブジェクトが含まれている場合にのみ実行するように変更ifして、確認できるようにしますcount

if (twitterAccounts.count)
于 2013-10-10T04:10:54.530 に答える
0

元のコードを変更することを検討してください

 if (twitterAccounts != nil)

if (twitterAccounts != nil && twitterAccounts.count)
于 2013-10-10T04:31:58.720 に答える
0

ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];最初にアカウントがあるかどうかを確認せずに呼び出します。

1 つのオプションは、次のように変更することです。

if (twitterAccounts != nil)

に:

if (twitterAccounts.count) {
    // process the account
} else {
    // show alert
}
于 2013-10-10T04:10:21.950 に答える