问题
I get the following error in the console:
Terminating app uncaught exception 'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects1'
I get this error when I am selecting an image to be added to firebase. This happens in the code bellow.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
// let imageToUse = PhotoArray()
// let data = UIImagePNGRepresentation(userPickedImage) //here convert to data
PhotoArray.sharedInstance.photosArray.append(userPickedImage) //append converted data in array
imageView.image = userPickedImage
//-----------------------------//
// //begin code from firebase docs
// Create a root reference
let storageRef = Storage.storage().reference()
// Create a reference to "mountains.jpg"
let ImgRef = storageRef.child("ImgRef.jpg")
// Create a reference to 'images/mountains.jpg'
let userImagesRef = storageRef.child("images/userImagesRef.jpg")
// While the file names are the same, the references point to different files
ImgRef.name == userImagesRef.name; // true
ImgRef.fullPath == userImagesRef.fullPath; // false
// Local file you want to upload
let localFile = URL(string: "path/to/image")!
// Create the file metadata
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
// 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
}
}
}
imagePicker.dismiss(animated: true, completion: nil)
}
The error appears to happen on line 76 as shown bellow.
回答1:
You are using the wrong API. URL(string
is for URL strings starting with a scheme
(http://
, ftp://
, file://
).
For files in the local file system you have to use URL(fileURLWithPath:
which takes a string path starting with /
.
来源:https://stackoverflow.com/questions/51887956/terminating-app-uncaught-exception-nsinvalidargumentexception