Firebase 存储错误

Firebase storage error

FirebaseStorage Pod 版本 (1.0.1)

var storageRef: FIRStorageReference! = FIRStorage.storage().reference()


let uploadTask = storageRef.putData(UIImagePNGRepresentation(image)!, metadata: nil) { metadata, error in
        if (error != nil) {

        } else {
            let downloadURL = metadata!.downloadURL
            print(downloadURL)
        }
}

我只是从 ImagePicker 中选择了一张图片,并尝试将图片数据保存到 firebase 存储中。如 firebase 文档中所示。(Docs Reference) 从内存中的数据上传 部分。

它抛出以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'
*** First throw call stack:
(
    0   CoreFoundation                      0x02025494 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x03eb2e02 objc_exception_throw + 50
    2   CoreFoundation                      0x01f0c6d2 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 386
    3   CoreFoundation                      0x01f2095b +[NSDictionary dictionaryWithObjects:forKeys:count:] + 75
    4   FirePlay                            0x001e42bc -[FIRStorageUploadTask enqueue] + 815
    5   FirePlay                            0x001e010c -[FIRStorageReference putData:metadata:completion:] + 880

以前使用的相同代码。我不确定现在是什么问题。

原来我们不能将文件放在根存储节点中。需要创建子节点,然后只有你可以保存文件。显然 mentioned in the Firebase docs 不知何故我忽略了它。

例如

let mountainsRef = storageRef.child("mountains.jpg")

let uploadTask = mountainsRef.putData(UIImagePNGRepresentation(image)!, metadata: nil) { metadata, error in
        if (error != nil) {

        } else {
            let downloadURL = metadata!.downloadURL
            print(downloadURL)
        }
}