"BOOL类型的集合元素"不是objective-c对象

jdo*_*dog 7 objective-c ios

任何人都知道为什么我得到这个?

-(void)postPrimaryEMWithEM:(EM *)em
              exclusive:(BOOL) isExclusive
                 success:(void (^)())onSuccess
                 failure:(void (^)())onFailure {


if(self.accessToken) {


    GenericObject *genObject = [[GenericObject alloc] init];

    [[RKObjectManager sharedManager] postObject:genObject
                                          path:@"users/update.json"
                                    parameters:@{
                                                  ...
                                                 @"em_id"  : ObjectOrNull(em.emID),
                                                 @"exclusive": isExclusive  <-- error message
Run Code Online (Sandbox Code Playgroud)

Rob*_*Rob 12

您不能将基本数据类型放在字典中.它必须是一个对象.但您可以使用[NSNumber numberWithBool:isExclusive]或使用@(isExclusive)语法:

[[RKObjectManager sharedManager] postObject:genObject
                                       path:@"users/update.json"
                                 parameters:@{
                                              ...
                                             @"em_id"  : ObjectOrNull(em.emID),
                                             @"exclusive": @(isExclusive), ...
Run Code Online (Sandbox Code Playgroud)

我也不怀疑你打算BOOL *用作你的参数.你可能想要:

- (void)postPrimaryEMWithEM:(EM *)em
                  exclusive:(BOOL) isExclusive
                    success:(void (^)())onSuccess
                    failure:(void (^)())onFailure {
    ...
}
Run Code Online (Sandbox Code Playgroud)

同样,a BOOL不是一个对象,所以*语法可能不是预期的.

  • 还要注意``isExclusive`是一个`BOOL*`. (2认同)

Chr*_*ris 7

从BOOL中删除指针('*'):

exclusive:(BOOL*) isExclusive
Run Code Online (Sandbox Code Playgroud)

并改变:

@"exclusive": isExclusive
Run Code Online (Sandbox Code Playgroud)

至:

@"exclusive": [NSNumber numberWithBool:isExclusive]
Run Code Online (Sandbox Code Playgroud)

要么:

// Literal version of above NSNumber
@"exclusive": @(isExclusive)
Run Code Online (Sandbox Code Playgroud)

需要注意的是,NSDictionary无法存储原始类型,包括布尔值.因此,您必须将值封装在对象中,在本例中为NSNumber.

  • 为什么要发布重复的答案? (3认同)