从swift文件

时间:2017-11-15 02:35:47

标签: arrays json swift class

所以我有一个类swift文件,它有一个像这样的UserAccount类

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

 }

}

我有登录视图控制器,我获取类变量并执行我的json并分配它们的值如下:

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

这是我得到我的Json并将其值添加到相应的用户类varible的地方,我在登录视图控制器上执行此操作:

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

这是我将信息附加到我的类数组的地方:

self.userAcount.append(self.user)

现在我的问题是我不知道如何在其他视图控制器上显示UserAccount类中的信息,例如用户配置文件视图控制器,是我第一次使用用户和配置文件,任何帮助将不胜感激,谢谢你

2 个答案:

答案 0 :(得分:0)

向另一个https://graph.facebook.com/{page-id]/feed?access_token={facebook_app_id}| {facebook_app_secret)&limit=5

提供数据
  1. 使用UIViewControllerStoryboard

    segue
  2. 编程

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

答案 1 :(得分:0)

在另一个ViewController中创建 UserAccount 的实例。 当您唤醒新的视图控制器时,传递 UserAccount 的值,要传递值,您有两个选项 的 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。按代码

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

现在要在NewViewController中访问,只需通过

进行访问即可
let userName = self.user.userName