1

i have problem with saving dictionary data into UserDefaults, NSCache, to file , i try to figure what i am doing wrong but i didn't have any progress so far. I'll try to simplify my problem

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

i just want to save and use later notesdict [Notes]type, I was try almost everything,Write and read file with list NSDictonary swift, Save dictionary in userdefaults in swift 3 with xcode 8 .... if anybody have some idea i would appreciate it very much.

Undo
  • 25,519
  • 37
  • 106
  • 129
FpS
  • 11
  • 2
  • Please clean up the code, this syntax : `value as? String as AnyObject??)!` is horrible. And declare all `String` properties in `Notes` as non-optional since the initializer passes non-optional values. If you want to save all properties in `Notes` to user defaults, change `struct` to `class` inheriting from `NSObject` and implement `NSCoding` – vadian Aug 05 '17 at 18:37
  • yep, this newTodos is very awful, i bet your problem is somewhere there. Besides, NSUserDefaults is not for that, like it's name said it's for saving user defaults like f.e: settings, eventually some simple keys. You should better learn core data or realm if you want to make it like that. – klapinski Aug 05 '17 at 19:08
  • @vadian thanks, now it works , you save my day – FpS Aug 05 '17 at 20:03
  • @kam.voick - core data for one dict with Strings, i don't think that is good solution – FpS Aug 05 '17 at 20:09

0 Answers0