Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么当我尝试附加数组时应用程序会崩溃?_Ios_Arrays_Swift_Parse Platform - Fatal编程技术网

Ios 为什么当我尝试附加数组时应用程序会崩溃?

Ios 为什么当我尝试附加数组时应用程序会崩溃?,ios,arrays,swift,parse-platform,Ios,Arrays,Swift,Parse Platform,我正在从parse导入一个数组,我想将该数组添加到一个数组数组中,但应用程序在尝试追加导入的数组时崩溃。为什么会发生这种情况?我如何修复它?崩溃错误是线程1:EXC_BAD_指令(code=EXC_I386_INVOP,subcode=0x0)I注释了追加行,它没有崩溃,因此它必须是该行 var animalarray: [[String]] = [] let query = PFQuery(className: "animals") query.findObjectsI

我正在从parse导入一个数组,我想将该数组添加到一个数组数组中,但应用程序在尝试追加导入的数组时崩溃。为什么会发生这种情况?我如何修复它?崩溃错误是线程1:EXC_BAD_指令(code=EXC_I386_INVOP,subcode=0x0)I注释了追加行,它没有崩溃,因此它必须是该行

 var animalarray: [[String]] = []    

 let query = PFQuery(className: "animals")

    query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
        if error == nil{

                    for object in objects!{

                        if let animalss = object["CoordinateTest"]{
                            print("coord \(animalss)")
                            self.animalarray.append(animalss as! [String])//crashes here

                          }
                  }
             }
        }

您应该创建一个检索数据的方法,并使用查询指定所需内容。此外,您还应该创建一个临时变量来保存检索到的数据,并将该变量附加到数组中

例)


打印(“coord\(Animals)”
行日志中出现了什么?
coord(“dog”,“cat”)
如果我不得不猜测,
Animals
实际上不是一个字符串数组,您的强制转换导致了崩溃。在解析它时,它是一个字符串数组。一般来说,您应该避免强制转换,我建议您添加
?[String]
if
行,并在
else
块中检查它未正确转换的原因。这可能不是您问题的解决方案,但可能有助于您缩小问题的范围。
var animalsArray:   [String] = []

func retrieveData(){
        let query = PFQuery(className: "animals")
        query.whereKey("Key", equalTo: object)
        query.orderByDescending("createdAt")
        query.findObjectsInBackgroundWithBlock {
            (object:[PFObject]?, error:NSError?) -> Void in

    if ( error != nil ){
        print(error?.localizedDescription, error?.userInfo)
    } else {

        for temp: PFObject in object! {
            let animals:    String       = temp["CoordinateTest"]         as! String

            self.animalsArray.append(animals!)
        }
      }  
    }
}