我怎么知道“ forKey”用户默认的密钥名称是什么

时间:2019-06-09 20:15:57

标签: swift xcode key nsuserdefaults

我有一个写作应用程序,用户可以创建一个可以添加到表格视图(和Firebase)中的帖子。 当我要保存新帖子时,应用程序崩溃。它永远不会访问if,它总是转到else,所以我认为if是不正确的。 我怎么知道if中userdefault中“ forKey”的键名是什么?

func readFromNSUserDefault()-> Memory?
{
    let d : UserDefaults = UserDefaults.standard

    if UserDefaults.standard.array(forKey: "") != nil
 {
        print("exists")
    let strTitle = d.object(forKey: "title") as? String
    let strBody = d.object(forKey: "body") as? String
    let strImageRef = d.object(forKey: "imageRef") as? String
    let uid = d.object(forKey: "uid") as? String
    let imageData = d.object(forKey: "imageData") as? Data
    let key = d.object(forKey: "key") as? String
    let date = d.object(forKey: "date") as? NSNumber

    let m = Memory(title: strTitle!, body: strBody!, key: key!, uid: uid!, imageRef: strImageRef!, date: date!) // A variable from type memory
    m.image = UIImage(data: imageData!)
    m.key = key!
    return m
    }
    print ("doesn't exist")
    let m : Memory?
    m = nil
    return m
}

保存功能:

@IBAction func saveMemorey(_ sender: UIButton) {

    let d = UserDefaults.standard

    if( mode == "newMemory")
    {
        self.addNewMemory()
        d.set(true, forKey: "userAdded")
    }
    else if mode == "edit"
    {
        self.editNewMemory()
        d.set(true, forKey: "userEdited")
    }
    d.synchronize()
    self.navigationController?.popViewController(animated: true) // Returns to the memories page after clicking 'save'
}

func addNewMemory()
{
    let refposts = ref.child("MEmories").childByAutoId()
   dateToSet = Double(self.future.millisecondsSince1970)
    imgref = "\(txtTitle.text!).png"
    self.m = Memory(title: txtTitle.text!, body: tBody.text!, key: "", uid: (Auth.auth().currentUser!.uid), imageRef: imgref!, date: self.dateToSet as NSNumber)
    refposts.setValue(m.toAnyObject(), withCompletionBlock: {(error,data) in
        self.m.key = data.key!
        self.m.image = self.ivImage.image
        self.saveImage()
        self.saveToNSUserDefaualts(memory: self.m)
    })
}


func saveToNSUserDefaualts(memory:Memory)
 {
    let d : UserDefaults = UserDefaults.standard
    d.set(memory.title, forKey: "title")
    d.set(memory.body, forKey: "body")
    d.set(memory.key, forKey: "key")
    d.set(memory.imageRef, forKey: "imageRef")
    d.set(memory.uid, forKey: "uid")
    d.set(memory.key, forKey: "key")
    d.set(memory.date, forKey: "date")   
let imageData = NSData(data:ivImage.image!.pngData()!)
  d.set(imageData, forKey: "imageData")
    d.synchronize()
}

加载功能:

func loadMemories()

{
   let UID = Auth.auth().currentUser!.uid
   self.ref.child("MEmories").queryOrdered(byChild: "uid").queryEqual(toValue: UID).observeSingleEvent(of: .value, with: {
        snapShot in
        if let dict = snapShot.value as? NSDictionary
        {
            for d in (dict as? Dictionary<String,AnyObject>)!
            {
                let title = d.value["title"] as?String
                let body = d.value["body"] as? String
                let uid = d.value["uid"] as? String
                let imageRef = d.value["imageRef"] as? String
                let date = d.value["date"] as? NSNumber
                let m = Memory(title: title!, body: body!, uid: uid!,imageRef:imageRef!, date: date!)
                m.key = d.key
                let tempImageRef = self.sref.child(m.imageRef)
                tempImageRef.getData(maxSize: 500*1024*1024, completion: {(data,error) in
                    if error == nil
                    {
                        if let imageData = data
                        {
                            m.image = UIImage(data: imageData)
                            self.memories.append(m)
                            self.tbl.reloadData()
                        }
                    }
                }) 
            }                
        }//end of if
    })
}

内存类别:

import Foundation
import Firebase

class Memory

{
    // Class Attributes

    var title  : String

    var uid  : String  // user id

    var body : String

    var key : String

    var imageRef : String!

    var image : UIImage?

    var date : NSNumber


    init( title:String , body:String , key:String = "", uid:String , imageRef:String , date:NSNumber ) // Build function
    {

        self.title = title

        self.key = key

        self.body = body

        self.uid = uid

        self.imageRef = imageRef

        self.key = ""

        self.date = date


    }

    func toAnyObject()-> [String:AnyObject]
            {
                return ["title" : self.title as AnyObject,"body":self.body as AnyObject,"uid":self.uid as AnyObject,"imageRef":self.imageRef as AnyObject,"date":self.date as AnyObject
 ]

    }

}

0 个答案:

没有答案