简体   繁体   English

终止应用程序未捕获的异常“NSInvalidArgumentException”

[英]Terminating app uncaught exception 'NSInvalidArgumentException'

I get the following error in the console:我在控制台中收到以下错误:

Terminating app uncaught exception 'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects 1 '终止应用程序未捕获的异常“NSInvalidArgumentException”,原因:“-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: 尝试从对象1插入 nil 对象”

I get this error when I am selecting an image to be added to firebase.当我选择要添加到 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.错误似乎发生在第 76 行,如下所示。

在此处输入图片说明

You are using the wrong API.您使用了错误的 API。 URL(string is for URL strings starting with a scheme ( http:// , ftp:// , file:// ). URL(string用于以schemehttp://ftp://file:// )开头的 URL 字符串。

For files in the local file system you have to use URL(fileURLWithPath: which takes a string path starting with / .对于本地文件系统中的文件,您必须使用URL(fileURLWithPath:它采用以/开头的字符串路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “因未捕获的异常而终止应用程序'NSInvalidArgumentException'” - “Terminating app due to uncaught exception 'NSInvalidArgumentException'” 由于未捕获的异常'NSInvalidArgumentException 4而终止应用程序 - Terminating app due to uncaught exception 'NSInvalidArgumentException 4 ***由于未捕获的异常'NSInvalidArgumentException'而终止应用, - *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 由于未捕获而终止应用程序 > 异常“NSInvalidArgumentException” - Terminating app due to uncaught > exception 'NSInvalidArgumentException' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,NSUserDefaults? - Terminating app due to uncaught exception 'NSInvalidArgumentException', NSUserDefaults? 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序, - Terminating app due to uncaught exception 'NSInvalidArgumentException', 由于未捕获的异常NSInvalidArgumentException而终止应用程序 - Terminating app due to uncaught exception NSInvalidArgumentException 由于SDWEbImage中未捕获的异常'NSInvalidArgumentException'而终止应用 - Terminating app due to uncaught exception 'NSInvalidArgumentException' in SDWEbImage 是否由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序? - Terminating app due to uncaught exception 'NSInvalidArgumentException'? 由于未捕获的异常'NSInvalidArgumentException'错误而终止应用程序 - Terminating app due to uncaught exception 'NSInvalidArgumentException' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM