代码之家  ›  专栏  ›  技术社区  ›  Lukas Bimba

Swift MessageKit-无法将类型为“”的值转换为预期的参数类型“URL”

  •  -1
  • Lukas Bimba  · 技术社区  · 7 年前

    无法将类型为“”的值转换为预期的参数类型“URL”

            completion(meta?.downloadURL())
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   iluvatar_GR    7 年前

    假设您的问题可能如下

    storage.child(channelID).child(imageName).putData(data, metadata: metadata) { meta, error in
      completion(meta?.downloadURL())
    }
    

    storage.child(channelID).child(imageName).putData(data, metadata: metadata) { metaN, error in
        // then we check if the metadata and path exist
        // if the error was nil, we expect the metadata and path to exist
        // therefore if not, we return an error
        guard let metadata = metaN, let path = metadata.path else {
           completion(nil)
           return
        }
        // now we get the download url using the path
        // and the basic reference object (without child paths)
        self.getDownloadURL(from: path, completion: completion)
    }
    
    private func getDownloadURL(from path: String, completion:@escaping (URL?) -> Void) {
        let firebaseStorageUrl = "gs://yourApp-Firebase-Storage.appspot.com"
        let storageReference = Storage.storage().reference(forURL: firebaseStorageUrl)
        storageReference.child(path).downloadURL { (url, error) in
            completion(url)
        }
    }
    

    确保已在Firebase中启用存储,如果失败,请检查控制台错误