1

so i have a class swift file that has a UserAccount class like this

class UserAccount{

 var userName: String!
 var email: String!
 var instaGram: String!
 var timeCreated: String!
 var like: String!
 var role: String!
 var active: String!
 var id: String!


init(userName: String, email: String, instaGram: String, timeCreated: String, like: String, role: String, active: String, id: String){

    self.userName = userName
    self.email = email
    self.instaGram  = instaGram
    self.timeCreated = timeCreated
    self.like = like
    self.role = role
    self.active = active
    self.id = id

 }

}

i have login view controller where i get the class variables and do my json and assign their values like this :

var userAcount = [UserAccount]() // Array Of Object
var user = UserAccount(userName: "", email: "", instaGram: "", timeCreated: "", like: "", role: "", active: "", id: "") //Init of Object

This is where i get my Json and add its value to its corresponding user class varible, i do this on my login view controller:

self.user.userName = userData.value(forKey: "userName") as! String
self.user.email = userData.value(forKey: "email") as! String
self.user.instaGram = userData.value(forKey: "instaGram") as! String
self.user.timeCreated = userData.value(forKey: "timeCreated") as! String
self.user.role = userData.value(forKey: "role") as! String
self.user.like = userData.value(forKey: "likes") as! String
self.user.id = userData.value(forKey: "id") as! String
self.user.active = userData.value(forKey: "active") as! String

this is where i append the information to my class array:

self.userAcount.append(self.user)

now my problem is that i dont know how to display the information in the UserAccount class on other view controllers, example the user profile view controller, is my first time working with users and profile, any help would be appreciated it, thank you

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Why are all of your properties declared as implicitly unwrapped optionals? You should remove all of those `!` since you provide a non-optional value for everyone of them in the `init` method. – rmaddy Nov 15 '17 at 02:44
  • missed that, thank you @rmaddy –  Nov 15 '17 at 02:47
  • Provide more informations about what you want to do: "just display data", "Send data to other VC", "Show data in TableView" etc – Yerken Nov 15 '17 at 03:25
  • @YerkebulanAbildin send data another VC –  Nov 15 '17 at 03:30

2 Answers2

0

To provide data to another UIViewController

  1. Using Storyboard or segue

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if segue.identifier == "your_identifier", 
          let destinationVC = segue.destination as? YourViewController {
            destinationVC.userAcount = self.userAcount
        }
    }
    
  2. Programmatically

    func gotoNextVC() {
        let destinationVC = YourViewController(nibName: "YourViewController", bundle: nil)
        destinationVC.userAcount = self.userAcount
        self.navigationController?.pushViewController(destinationVC, animated: true)
    
        // OR
        self.present(destinationVC, animated: true, completion: nil)
    }
    
Jalakoo
  • 3,523
  • 2
  • 21
  • 20
Yerken
  • 299
  • 1
  • 12
0

In another ViewController make a Instance of your UserAccount. When you wake that new view controller, pass value of UserAccount, to pass value you have two option 1. Storyborad/Segue

func prepare(for segue: UIStoryboardSegue, sender: Any?){
      if segue.identifier == "identifier"{ 
        let destinationVC = segue.destination as? YourViewController
        destinationVC.userAcount = self.userAcount
      }
   }

2. By code

let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
   let controller = storyBoard.instantiateViewController(withIdentifier:      identifier)
   controller.user = self.user

and now to access in your NewViewController you can simply access by

let userName = self.user.userName
singh.jitendra
  • 490
  • 1
  • 9
  • 19
  • this partially works, but if the user leaves the view and comes back to it, the variables loses its value, in other words, when the user first login and goes to his profile, it shows his email, but if he goes to another view and comes back to his profile view, then the email is nil @singh.jitendra –  Nov 15 '17 at 15:18
  • if this information don't have security concern than you can try **NSUserDefault** [link]https://stackoverflow.com/questions/31203241/how-to-use-nsuserdefaults-in-swift) It's easy to user but data is much private than you have to sore it local place like coreData or in Sqlite @scs – singh.jitendra Nov 16 '17 at 04:15