[英]Trying to build multiple image upload to Firebase: 2 Errors in SWIFT 4, FIREBASE 5.0.0
[英]Getting an error when trying to upload image to firebase (Swift)
我有以下代碼:
func upload() {
let localFile = usersImg!
// Create the file metadata
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
let storageRef = Storage.storage().reference()
// Upload file and metadata to the object 'images/mountains.jpg'
let uploadTask = storageRef.putFile(from: localFile, metadata: metadata)
// Listen for state changes, errors, and completion of the upload.
uploadTask.observe(.resume) { snapshot in
// Upload resumed, also fires when the upload starts
}
uploadTask.observe(.pause) { snapshot in
// Upload paused
}
uploadTask.observe(.progress) { snapshot in
// Upload reported progress
let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
/ Double(snapshot.progress!.totalUnitCount)
}
uploadTask.observe(.success) { snapshot in
// Upload completed successfully
}
uploadTask.observe(.failure) { snapshot in
if let error = snapshot.error as NSError? {
switch (StorageErrorCode(rawValue: error.code)!) {
case .objectNotFound:
// File doesn't exist
break
case .unauthorized:
// User doesn't have permission to access file
break
case .cancelled:
// User canceled the upload
break
/* ... */
case .unknown:
// Unknown error occurred, inspect the server response
break
default:
// A separate error occurred. This is a good place to retry the upload.
break
}
}
}
}
userImg
變量只是用戶選擇的圖像,如果我要打印它,它看起來像這樣:
Optional(file:///var/mobile/Media/DCIM/100APPLE/IMG_0030.PNG)
但是,當我運行我的代碼並調用該方法時,出現以下錯誤:
Thread 12: "*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]"
這是什么意思,我做錯了什么。 (順便說一句,我嘗試了很多不同的方法來將 url 上傳到 firebase,但是對於每個人我都遇到了奇怪的錯誤,所以這段代碼直接來自 firebase 網站。
我不能保證任何事情,因為我沒有使用相同的代碼,盡管我不得不更改我的 firebase 存儲的規則,這很有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.