【问题标题】:Terminating app uncaught exception 'NSInvalidArgumentException'终止应用程序未捕获的异常“NSInvalidArgumentException”
【发布时间】:2019-01-24 01:23:03
【问题描述】:

我在控制台中收到以下错误:

终止应用程序未捕获异常'NSInvalidArgumentException',原因:'-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从objects1插入nil对象'

我在选择要添加到 Firebase 的图像时收到此错误。这发生在下面的代码中。

        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)
    }

错误似乎发生在第 76 行,如下所示。

【问题讨论】:

  • 究竟是哪一行导致了错误?
  • 放置痕迹并检查哪一行代码实际上导致了错误
  • 好吧,SIGERT 出现在我的应用委托类中,所以我会尝试找到它
  • 我用我认为发生错误的地方更新了这个问题
  • 您是否在您的 uploadTask 上实现了 .pause()、.resume() 和 .cancel() 函数?

标签: ios iphone swift firebase firebase-storage


【解决方案1】:

您使用了错误的 API。 URL(string 用于以 scheme 开头的 URL 字符串(http://ftp://file://)。

对于本地文件系统中的文件,您必须使用URL(fileURLWithPath:,它采用以/ 开头的字符串路径。

【讨论】:

  • 我试过了: let localFile = URL(fileURLWithPath: "path/to/image") 和前面的 / 一样。但是我继续得到同样的错误
  • 当然 URL(字符串)必须是有效的。硬编码的 URL 字符串不起作用。
  • 我尝试了 userImagesRef.downloadURL 但收到以下错误:无法将类型“(@escaping(URL?,错误?)-> Void)-> Void”的值转换为预期的参数类型“字符串”
  • 我对 firebase 不熟悉。看起来您正在使用闭包。无论如何,URL(fileURLWithPath: 的参数必须是以斜杠开头的有效 string 路径。
猜你喜欢
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多