终止应用程序未捕获的异常' NSInvalidArgumentException' [英] Terminating app uncaught exception 'NSInvalidArgumentException'
问题描述
我在控制台中收到以下错误:
终止应用程序未捕获的异常'NSInvalidArgumentException',原因:'-[__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象中插入零对象
解决方案您使用了错误的API.
URL(string
是用于以scheme
(http://
,ftp://
,file://
).对于本地文件系统中的文件,您必须使用
URL(fileURLWithPath:
,它采用以/
开头的字符串路径.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.
解决方案You are using the wrong API.
URL(string
is for URL strings starting with ascheme
(http://
,ftp://
,file://
).For files in the local file system you have to use
URL(fileURLWithPath:
which takes a string path starting with/
.这篇关于终止应用程序未捕获的异常' NSInvalidArgumentException'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!