【问题标题】:Cannot subscript a value of type '[String : Any]' with an argument of type 'UIImagePickerController.InfoKey'无法使用“UIImagePickerController.InfoKey”类型的参数为“[String : Any]”类型的值下标
【发布时间】:2019-10-23 22:35:55
【问题描述】:

我正在尝试如何更新我的代码以支持 UIImagePickerController() 在 Swift 5 中的新工作方式

我能够在此处的代码的另一部分中修复相同的错误:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
        guard let image = info[.originalImage] as? UIImage else { return }
        let sellLandingVC = navigationController.topViewController as? SellLandingViewController
        sellLandingVC?.viewModel.insertSelectedImage(image)
        navigationController.dismiss(animated: true, completion: nil)
    }

这是我的代码,调用 imagePicker.rx.didFinishPickingMediaWithInfo.map

时出现错误
private func setupRx() {
        let imagePicker = UIImagePickerController()

        Observable.combineLatest(checkIfPermissionIsGranted(), captureButton.rx.tap) { (isGranted, _ ) in return isGranted }
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [weak self] isGranted in
                self?.captureButton.pulsate()
                if !isGranted {
                    self?.displayAlert(with: self?.viewModel.accessDeniedTitle, message: self?.viewModel.accessDeniedMessage, actions: [UIAlertAction.cancel(), UIAlertAction.settings()])
                } else {
                    imagePicker.sourceType = .photoLibrary
                    imagePicker.allowsEditing = false
                    self?.present(imagePicker, animated: true)
                }
            })
        .disposed(by: disposeBag)

        imagePicker.rx.didFinishPickingMediaWithInfo
            .map {
               $0[UIImagePickerController.InfoKey.originalImage] as! UIImage
//This is where I'm getting the error
            }
            .map {
                ProfilePictureCommand.setImage($0)
            }
            .bind(to: viewModel.concurrentCommandSubject)
            .disposed(by: disposeBag)

        viewModel.stateObservable
            .map {
                $0.userImage ?? #imageLiteral(resourceName: "email-signup-avatar")
        }
            .bind(to: profileImageView.rx.image)
            .disposed(by: disposeBag)

        imagePicker.rx.didFinishPickingMediaWithInfo
            .subscribe(onNext: { _ in
                imagePicker.dismiss(animated: true)
            })
            .disposed(by: disposeBag)

        viewModel.stateObservable
            .map {
                return $0.isContinueEnabled
            }
            .bind(to: continueButton.rx.isEnabled)
            .disposed(by: disposeBag)

        viewModel.stateObservable
            .map {
                return $0.isContinueEnabled
            }
            .map {
                $0 ? UIColor.colorHex303030(alpha: 1.0) : UIColor.colorHexC5C5C5(alpha: 1.0)
            }
            .bind(to: continueButton.rx.scBackgroundColor)
            .disposed(by: disposeBag)

        continueButton.rx.tap
            .map { ProfilePictureCommand.next }
            .bind(to: viewModel.concurrentThrottleCommandSubject)
            .disposed(by: disposeBag)
    }

【问题讨论】:

    标签: ios xcode11 swift5


    【解决方案1】:

    您的 rx 版本为您提供 [String:Any] 而不是 [UIImagePickerController.InfoKey: Any]UIImagePickerController.InfoKey 现在是一个带有.originalImage 案例的枚举。如果你想用字符串下标老式方式,只需询问原始值:UIImagePickerController.InfoKey.originalImage.rawValue

     $0[UIImagePickerController.InfoKey.originalImage.rawValue] as! UIImage
    

    【讨论】:

      猜你喜欢
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 2020-02-04
      • 2018-12-29
      相关资源
      最近更新 更多