Swift - 即使objectId准确,也发现自定义对象为零

时间:2017-03-20 11:46:26

标签: swift parse-platform

        let parent = path[row-1]
        let child = path[row]

        let indexOfChild = matrix[parent.objectId!]!.index(of: child)

        print("indexOfChild = \(indexOfChild)")
        print("keyValuePair = \(matrix[parent.objectId!]!)")
        print("child = \(child)")

        let indexAfter =  matrix[parent.objectId!]!.index(after: indexOfChild!)

结果我将这些信息打印在控制台中。

正如您所见,keyValuePair中存在“child”(这是带有argumentText的参数对象:“example”)(它们具有相同的objectId)。然而,它在keyValuePair中的索引在控制台中显示为零。显然,区别仅在于:“child”旁边有“0x608000333f60”,而keyValuePair中的第二个参数对象有“0x600000321a40”

我不知道这些的目的以及它们是如何计算的,但在我看来索引是零,因为这些代码彼此不同

我一直试图修复它太长时间了。有人可以帮我解决一下吗?

indexOfChild = nil
keyValuePair = [<Argument: 0x608000334e60, objectId: IG9ekqMRw9, localId: (null)> {
    ACL = "<PFACL: 0x60800042f200>";
    argumentText = "the only thing is I can't get over how I feel when it ";
    creatorId = hWRXoRvnYd;
    level = 4;
    parentId = j7GkpwUKsm;
    reach = 0;
    threadId = Dtq632QYJ2;
}, <Argument: 0x600000321a40, objectId: 56AsB1juNP, localId: (null)> {
    ACL = "<PFACL: 0x600000235440>";
    argumentText = "example ";
    creatorId = hWRXoRvnYd;
    level = 4;
    parentId = j7GkpwUKsm;
    reach = 0;
    threadId = Dtq632QYJ2;
}]
child = <Argument: 0x608000333f60, objectId: 56AsB1juNP, localId: (null)> {
    ACL = "<PFACL: 0x608000235540>";
    argumentText = "example ";
    creatorId = hWRXoRvnYd;
    level = 4;
    parentId = j7GkpwUKsm;
    reach = 0;
    threadId = Dtq632QYJ2;
}
fatal error: unexpectedly found nil while unwrapping an Optional value

1 个答案:

答案 0 :(得分:0)

0x608000333f60 0x600000321a40 值是相关变量的内存地址位置。由于地址位置不同,我相信这两个子实例被视为两个独立的值,可能不匹配。这可能是你遇到的问题。

最好通过唯一ID值检查子实例的父实例,以便您可以正确匹配父数组中的子实例。这有意义吗?

您可以通过以下方式找到孩子的索引:

var index = -1
var found = false
for chld in matrix[parent.objectId!]! {
    index = index + 1
    if chld.objectId == child.objectId {
        found = true
        break
    }
}
if found {
    // The index value at this point is the index for the child
}

您也可以简单地过滤子数组,找到父数组中与您的child变量具有相同objectId的子项,然后从子数组中找到索引,如下所示:

let chld = matrix[parent.objectId!]!.filter{ $0.objectId == child.objectId }.first
let indexOfChild = matrix[parent.objectId!]!.index(of: chld)