不断收到无法分配的错误
Keep getting unassignable error
在下面的函数中,我不断得到:
Variable is not assignable (missing __block type specifier)
我尝试通过将 __block
添加到 twitterUsername
来修复它,然后添加 returns null
函数。我究竟做错了什么?我真的很想了解这背后的逻辑,而不仅仅是解决方案。
- (NSString *) getTwitterAccountInformation
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *twitterUsername = [[NSString alloc] init];
[accountStore requestAccessToAccountsWithType:accountType
options:nil
completion:^(BOOL granted, NSError *error)
{
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSLog(@"%@",twitterAccount.username);
NSLog(@"%@",twitterAccount.accountType);
twitterUsername = [NSString stringWithFormat:@"%@", twitterAccount.username];
}
}
}];
NSLog(@"Twitter username is: %@", twitterUsername);
return twitterUsername;
}
requestAccessToAccountsWithType:options:completion:
方法是异步的,这意味着它不等待对网络调用的响应,returns 立即。
相反,它会在调用返回后排队执行一个块,并在加载数据后执行它。
一个可能的解决方案是让您的 getTwitterAccountInformation
也将完成块作为参数,它可能看起来像这样:
- (void) getTwitterAccountInformation:(void(^)(NSString *userName, NSError *error))completion
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if(error) {
completion(nil, error);
}
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSLog(@"%@",twitterAccount.username);
NSLog(@"%@",twitterAccount.accountType);
NSString *twitterUsername = twitterAccount.username;
NSLog(@"Twitter username is: %@", twitterUsername);
completion(twitterUsername, nil);
}
}
}];
}
在下面的函数中,我不断得到:
Variable is not assignable (missing __block type specifier)
我尝试通过将 __block
添加到 twitterUsername
来修复它,然后添加 returns null
函数。我究竟做错了什么?我真的很想了解这背后的逻辑,而不仅仅是解决方案。
- (NSString *) getTwitterAccountInformation
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *twitterUsername = [[NSString alloc] init];
[accountStore requestAccessToAccountsWithType:accountType
options:nil
completion:^(BOOL granted, NSError *error)
{
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSLog(@"%@",twitterAccount.username);
NSLog(@"%@",twitterAccount.accountType);
twitterUsername = [NSString stringWithFormat:@"%@", twitterAccount.username];
}
}
}];
NSLog(@"Twitter username is: %@", twitterUsername);
return twitterUsername;
}
requestAccessToAccountsWithType:options:completion:
方法是异步的,这意味着它不等待对网络调用的响应,returns 立即。
相反,它会在调用返回后排队执行一个块,并在加载数据后执行它。
一个可能的解决方案是让您的 getTwitterAccountInformation
也将完成块作为参数,它可能看起来像这样:
- (void) getTwitterAccountInformation:(void(^)(NSString *userName, NSError *error))completion
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if(error) {
completion(nil, error);
}
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSLog(@"%@",twitterAccount.username);
NSLog(@"%@",twitterAccount.accountType);
NSString *twitterUsername = twitterAccount.username;
NSLog(@"Twitter username is: %@", twitterUsername);
completion(twitterUsername, nil);
}
}
}];
}