【问题标题】:Firebase uploading file getting optional string in filenameFirebase上传文件在文件名中获取可选字符串
【发布时间】:2017-01-10 13:02:18
【问题描述】:

我在上传图片时正在实施 Firebase 友好的聊天示例。使用可选字符串获取响应。错过了什么?

Optional(FIRStorageMetadata 0x7fc84aced790: {
    bucket = "abc.appspot.com";
    contentEncoding = identity;
    contentType = "image/jpeg";
    downloadTokens = "abctoken";
    generation = 1231231;
    metageneration = 1;
    name = "Optional(\"S5CKnKUykANdxSvZ3wujpMXHTvg1\")/494509700277/asset.JPG";
    timeCreated = "2016-09-02T11:49:00.114Z";
    updated = "2016-09-02T11:49:00.114Z";
})

我的代码:

 let filePath = "(FIRAuth.auth()!.currentUser!.uid)/(Int(NSDate.time‌​IntervalSinceReferen‌​ceDate() * 1000))/(referenceUrl.lastPathComponent!)"

在示例中使用可选值 (?) 编写,因此我将其转换 (!)

【问题讨论】:

  • 当你在 firebase 中设置值时.. 那时你发送可选值检查两次
  • 该名称是在您将文件上传到 Firebase 存储时创建的。您将需要在此处强制解包可选值。如果您需要这方面的帮助,请发布显示如何将文件上传到 Firebase 存储的最少代码。
  • @FrankvanPuffelen 谢谢。 let filePath = "(FIRAuth.auth()!.currentUser!.uid)/(Int(NSDate.timeIntervalSinceReferenceDate() * 1000))/(referenceUrl.lastPathComponent!)" 在示例中用可选值 (?) 编写,所以我转换它(!)
  • 我将代码添加到问题本身,它的可读性更高。如果它不是您的实际代码,请检查并修复(通过单击edit 链接)。

标签: ios firebase firebase-storage


【解决方案1】:

在 swift 中,您必须强制 unwrap 值以避免这种情况(但请确保它不是 nil)。

"\(optionalValue!)"

或以标准方式打开包装

if let value = optionalValue {
    someFIRMethod("\(value)")
}

【讨论】:

    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2015-06-05
    • 2015-11-25
    • 1970-01-01
    相关资源
    最近更新 更多