gpt4 book ai didi

swift - 将数据写入 Firebase 不起作用 - 无法将类型 'user' 的值转换为预期参数类型 'AnyObject?'

转载 作者:行者123 更新时间:2023-11-30 10:01:50 24 4
gpt4 key购买 nike

我尝试将注册数据保存到 Firebase,但收到“无法将‘user’类型的值转换为预期参数类型‘AnyObject?’”错误。我尝试了很多东西,但我似乎无法弄清楚......有人可以帮助我吗?怎么保存呢?

import UIKit
import Firebase
import FirebaseDatabaseUI

class EventViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

//outlets for text & image
@IBOutlet weak var photoImageView: UIImageView!

@IBOutlet weak var eventName: UITextField!
@IBOutlet weak var eventDate: UITextField!
@IBOutlet weak var eventDes: UITextView!



//Database connection
let rootref = FIRDatabase().reference()
var imagePicker: UIImagePickerController = UIImagePickerController()



override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

@IBAction func submitEvent(sender: AnyObject) {

let name = eventName.text
let date = eventDate.text
let text = eventDes.text
var data: NSData = NSData()

if let image = photoImageView.image {
data = UIImageJPEGRepresentation(image,0.1)!
}

let base64String = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

if let unwrappedName = name , unwrappedDate = date, unwrappedText = text{
let user: NSDictionary = ["name":unwrappedName, "date":unwrappedDate, "text":unwrappedText, "photoBase64":base64String]
}

//Add firebase child node
let event = FIRDatabase().reference().child(name!)

// Write data to Firebase

event.setValue(user.init())

navigationController?.popViewControllerAnimated(true)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
view.endEditing(true)
super.touchesBegan(touches, withEvent: event)
}

//UIImagePickerControllerDelegate methods

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imagePicker.dismissViewControllerAnimated(true, completion: nil)
photoImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func addPicture(sender: AnyObject) {
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera

presentViewController(imagePicker, animated: true, completion: nil)
} else {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
imagePicker.delegate = self
presentViewController(imagePicker, animated: true, completion:nil)
}
}
}

最佳答案

您已经为基础数据库 rootRef 创建了一个实例,并且您又创建了一个不需要的引用。而且您还创建了您的用户: NSDictionary 在 if let 中,因此您无法在范围之外访问该变量。在上面声明您的用户,然后使用它。试试这个代码:

    let name = eventName.text
let date = eventDate.text
let text = eventDes.text
var data: NSData = NSData()
var user = NSDictionary()//declare here
if let image = photoImageView.image {
data = UIImageJPEGRepresentation(image,0.1)!
}

let base64String = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

if let unwrappedName = name , unwrappedDate = date, unwrappedText = text{
//use your declared dictionary
user = ["name":unwrappedName, "date":unwrappedDate, "text":unwrappedText, "photoBase64":base64String]
}

//Add firebase child node
//let event = FIRDatabase().reference().child(name!)
//Do not create one more reference to database

rootref.child(name!).setValue(user)
//rootref.setValue(user)

// Write data to Firebase
//event.setValue(user.init())

navigationController?.popViewControllerAnimated(true)

关于swift - 将数据写入 Firebase 不起作用 - 无法将类型 'user' 的值转换为预期参数类型 'AnyObject?',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38083681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com