看来我的代码运行良好,但如果设备中没有存储 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