将问题保存到UserDefaults,文件和iOS中的NSCache

时间:2017-08-05 18:32:11

标签: swift dictionary nsuserdefaults

我有将字典数据保存到UserDefaults,NSCache,文件的问题,我试图弄清楚我做错了什么,但到目前为止我没有任何进展。 我会尝试简化我的问题

     struct Notes {


        var ntitle: String!
        var username: String!
        var snapshot: FIRDataSnapshot
        var key: String!
        var uid: String!
        var ref: FIRDatabaseReference?


        init(ntitle: String, username: String, uid: String,key: String, ref: FIRDatabaseReference!, snapshot: FIRDataSnapshot ){
            self.username = username
            self.ntitle = ntitle
            self.uid = uid
            self.key = key
            self.snapshot = snapshot

            self.ref = ref

        }

        func toAnyObject() -> [String: AnyObject] {


            return ["ntitle": ntitle as AnyObject, "username": username as AnyObject, "uid": uid as AnyObject]


        }



    }


   class NotesController: UITableViewController  {

   var notesdict = [Notes]()


    override func viewDidAppear(_ animated: Bool) {

        DispatchQueue.main.async{
            self.tableView.reloadData()
        }



        var newItems = [Notes]()


        if FIRAuth.auth()?.currentUser == nil {

            let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login")
            self.present(vc, animated: true, completion: nil)
        }

        else {

            guard  let uid = FIRAuth.auth()?.currentUser?.uid else {
                return
            }
            let ref = FIRDatabase.database().reference().child("user-Notes").child(uid)

            ref.observe(.childAdded, with: { (snapshot) in

                let postID = snapshot.key
                let postReference = FIRDatabase.database().reference().child("/Notes/\(postID)")

                postReference.observe(.value, with: { (snapshot) in

                    let reff = FIRDatabase.database().reference()

  if snapshot.hasChildren() == true{

                        let newTodos = Notes(ntitle: ((snapshot.childSnapshot(forPath: "ntitle").value as? String as AnyObject??)! ?? ("" as AnyObject?)!) as! String,username: ((snapshot.childSnapshot(forPath: "username").value as? String as AnyObject??)! ?? ("" as AnyObject?)!) as! String,uid: ((snapshot.childSnapshot(forPath: "uid").value as? String as AnyObject??)! ?? ("" as AnyObject?)!) as! String, key: snapshot.key, ref: snapshot.ref, snapshot: snapshot)


                        newItems.insert(newTodos, at: 0)

                        self.notesdict = newItems



                    }
                })



            }, withCancel: nil)

        }

        self.tableView.reloadData()
    }

我只想保存并使用以后的 notesdict [Notes]类型,我几乎尝试了所有内容,Write and read file with list NSDictonary swiftSave dictionary in userdefaults in swift 3 with xcode 8 ....如果有人有一些想法我非常感激。

0 个答案:

没有答案