从 Parse.com 的 CloudCode 接收空数组时 iOS 崩溃 [英] iOS crashing when receiving empty array from Parse.com's CloudCode

查看:38
本文介绍了从 Parse.com 的 CloudCode 接收空数组时 iOS 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从返回字典的Parse.com 的云代码"中获取查询.字典包含两个键,groups"和invites",都是数组.

I'm attempting to fetch a query from "Parse.com's cloudcode" which returns a dictionary. The dictionary contains two keys, "groups" and "invites", both being arrays.

当其中一个键是空数组时,会出现以下错误:

When one of they key's is an empty array, the following error occurs:

2014-05-23 17:14:03.967 XXX[2267:60b] * 由于以下原因终止应用未捕获的异常 'NSInvalidArgumentException',原因:'*-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: 尝试从 objects[1]' 插入 nil 对象* 第一次抛出调用堆栈:(0x2d589f0b 0x37d20ce7 0x2d4c7cff 0x2d4c7ac3 0x1acd5b 0x1acfef 0xddb95 0xdda07 0xedd4d 2x3fdd3d0x2fe83057 0x2fdd3d23 0x2fe38e7f 0x2fe38e09 0x2fdb1b73 0x2d5550390x2d5529c7 0x2d552d13 0x2d4bd769 0x2d4bd54b 0x3242a6d3 0x2fe1c8910xe3805 0x3821eab7) libc++abi.dylib:以未捕获终止NSException 类型的异常 (lldb)

2014-05-23 17:14:03.967 XXX[2267:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]' * First throw call stack: (0x2d589f0b 0x37d20ce7 0x2d4c7cff 0x2d4c7ac3 0x1acd5b 0x1acfef 0xddb95 0xdda07 0xedd4d 0x2fdd3d23 0x2fe83057 0x2fdd3d23 0x2fe38e7f 0x2fe38e09 0x2fdb1b73 0x2d555039 0x2d5529c7 0x2d552d13 0x2d4bd769 0x2d4bd54b 0x3242a6d3 0x2fe1c891 0xe3805 0x3821eab7) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

这里是云代码功能:

Parse.Cloud.define("GroupsAndInvites",function(request,response){
        var user = request.user;
        //query memberships and invites for the current user
        var membershipQuery = new Parse.Query("Membership").include("group").equalTo("user",user);
        var inviteQuery = new Parse.Query("Invite").include("from").include("group").equalTo("to",user);

        //make promises
        var membershipPromise = membershipQuery.find();
        var invitePromise = inviteQuery.find();

        //call when both queries are done
        Parse.Promise.when(membershipPromise,invitePromise).then(function(memberships,invites){
                //make an array of groups instead of memberships
                var groups = [];
                for(var i=0;i<memberships.length;i++){
                        groups.push(memberships[i].get("group"));
                }
                //return groups and invites
                response.success({
                        "groups":groups,
                        "invites":invites
                });
        });
});

这是我在前端实现的那个函数:

And here's my implementation of that function on the front end:

  [PFCloud callFunctionInBackground:@"GroupsAndInvites" withParameters:nil block:^(NSDictionary *dict, NSError *error) {
    if (!error) {
      NSMutableArray *groupsArray = [NSMutableArray array];
      NSMutableArray *invitesArray = [NSMutableArray array];

      for (BVYGroup *group in dict[@"groups"]) {
        [groupsArray addObject:group];
      }

      for (BVYInvite *invite in dict[@"invites"]) {
        [invitesArray addObject:invite];
      }

      if ([strongDelegate respondsToSelector:@selector(groupStream:didFetchGroups:andInvites:)]) {
        [strongDelegate groupStream:self didFetchGroups:groupsArray andInvites:invitesArray];
      }
    } else {
      if ([strongDelegate respondsToSelector:@selector(groupStream:didFailToFetchGroupsWithError:)]) {
        [strongDelegate groupStream:self didFailToFetchGroupsWithError:error];
      }
    }
  }];

错误在检索到响应时发生.它不会到达检查错误的 if 语句.

The error occurs right when a response is retrieved. It does not reach the if statement that checks for an error.

对为什么会发生这种情况有任何见解吗?
提前致谢.

Any insight to why this might be occurring?
Thank you in advance.

推荐答案

您不能将 nil 插入到 Objective-C 集合中.如果需要在 Objective-C 集合中表示 nil 值,请改用 [NSNull null].

You can not insert nil into Objective-C collections. If you need to represent a nil value in an Objective-C collection, use [NSNull null] instead.

这篇关于从 Parse.com 的 CloudCode 接收空数组时 iOS 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆