在类

时间:2016-05-09 02:48:54

标签: ios arrays uiviewcontroller swift2

我有一个Users类和Login类(在单独的文件中),其中数据首先在Login类中初始化并获得,然后存储在Users类中以供将来在其他类中使用。要使用Users类中的变量,似乎我必须不断初始化它,这会重置类中的值。如何在不经常初始化的情况下获取变量?

现在的情况:

在Login.swift中收到数据 - >数据传递给Users.swift - >无法在ViewController.swift中显示存储在Users.swift中的数据

我在寻找什么:

在Login.swift中收到数据 - >数据传递给Users.swift - >显示存储在ViewController.swift中的Users.swift中的数据

ViewController中的代码

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var usernameLabel: UILabel!

// var usernameLabelText = String()

let dataObj = Users(Name: "", Email: "", Id: "", ProfilePicture: "", Username: "")

var usernameLabelText: String? {
    return String(dataObj!.username)
}

override func viewDidLoad() {
    usernameLabel.text = "Welcome \(usernameLabelText)"
    print("This is the \(dataObj!.username)")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

Login.swift中的代码

import Foundation
import Alamofire
import SwiftyJSON
class Login {

var userName:String!
var passWord:String!

init(userName : String, passWord : String) {

    let parameters = [
        "username": userName,
        "password": passWord 
    ]

    Alamofire.request(.POST, "http://anyapi.com", parameters: parameters)
        .responseJSON { response in
            print(response.request)
            print(response.response)
            print(response.data)
            print(response.result)
            if let result = response.result.value {
                print("Did receive JSON data: \(result)")
                let value = JSON(result)
                if let api_key = value["api_token"].string {
                    print("The token is " + api_key)
                } else{
                    print("error parsing api token")
                }
             //pass data to Users class
               _ = Users.init(Name: value["name"].string, Email: value["email"].string, Id: value["id"].string, ProfilePicture: value["profile_picture"].string, Username: value["username"].string)
            }
            else {
                print("JSON data is nil.")
            }


    }

}
}

Users.swift中的代码

import Foundation
import Alamofire
import SwiftyJSON

class Users {
private var _name: String!
private var _email: String!
private var _userId: String!
private var _profilePicture: String!
private var _username: String!

var particulars: [String] = []

var name: String {
    _name = particulars[0]
    return _name
}

var email: String {
    _email = particulars[1]
    return _email
}

var userId: String {
    _userId = particulars[2]
    return _userId
}

var profilePicture: String {
    _profilePicture = particulars[3]
    return _profilePicture
}

var username: String {
      _username = particulars[4]
    return _username
}


required init?(Name: String?, Email: String?, Id: String?, ProfilePicture: String?, Username: String?) {
    particulars += ["\(Name)"]
    particulars += ["\(Email)"]
    particulars += ["\(Id)"]
    particulars += ["\(ProfilePicture)"]
    particulars += ["\(Username)"]
}

}

2 个答案:

答案 0 :(得分:0)

  1. 在View Controller中创建用户类型变量。

    var usrData : Users?
    
  2. 实例化视图控制器时,将变量分配给已初始化的用户类。

    let view_controller_to_present = blahblah as! View Controller;<br>
    view_controller_to_present.usrData = DataFromRequest;<br>
    presentViewController(_ viewControllerToPresent: view_controller_to_present,animated flag: true, completion completion: nil);
    
  3. 访问数据。

答案 1 :(得分:0)

有很多话要说......

关于存储: 您的数据尚未存储。您可以使用Core Data来保留它们,或者使用NSUserDefault获得更轻松的解决方案(后者在应用程序查杀后不会保留数据),甚至是文件。

关于将数据从一个视图传递到另一个视图: 您可以覆盖prepareForSegue函数,或使用协议和委托模式。