为什么我的数据没有写入Firebase - 由于setObjectForKey导致应用程序崩溃:key不能为nil

时间:2016-06-29 06:34:59

标签: ios swift firebase firebase-realtime-database firebaseui

在过去的两周里,我一直在努力将数据写入firebase,虽然教程看起来很简单:/ ....我的应用程序有一个视图控制器和一个tableview控制器。用户可以添加事件标题,事件日期和描述(字符串)并添加图片。每当我尝试运行模拟器时,它就会崩溃!见下面的代码。请帮助我,这真的让人感到沮丧。即使我在代码中放置断点,它也会崩溃..如果有人可以在聊天中帮助我很棒或者发送文件导致它太长时间了我需要一个答案,所以我理解这个问题。

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()
    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!)
    rootref.child(name!).setValue(user)


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

0 个答案:

没有答案