在类

时间:2016-05-06 03:18:06

标签: ios json swift2 xcode7 alamofire

我试图将数据从我的Data类传递到我的Login类(两个文件是分开的。),但它会引发2个错误来自"登录"因为它看起来无法接收数据

以下是Login.swift中显示的错误:

错误1

  

实例成员' dataObj'不能在类型'登录'

上使用

错误2

  

预期声明

Login.swift代码

import Foundation
import Alamofire
import SwiftyJSON
class Login {let dataObj = Data(userName: "username", passWord: "password")

let endPoint = dataObj.todoEndPoint
let parameters = [
    "username": dataObj.userName,
    "password": dataObj.passWord
]

Alamofire.request(.POST, endPoint, parameters:parameters)
.responseJSON { response in
print(response.request)
print(response.response)
print(response.result)
if let JSON = response.result.value {
print("Did receive JSON data: \(JSON)")
}
else {
print("JSON data is nil.")
}
}   
}

Data.swift代码

import Foundation
import Alamofire
import SwiftyJSON

class Data {

var userName:String!
var passWord:String!
let todoEndpoint: String = "http://jsonplaceholder.typicode.com/todos/1"

init(userName : String, passWord : String) {
    self.userName = userName
    self.passWord = passWord
}
}

错误屏幕截图

Login.swift errors

2 个答案:

答案 0 :(得分:0)

根据您的Data类实现,您应该使用它:

class Login {
let dataObj = Data(userName : "username", passWord : "password")
let endPoint = dataObj.todoEndPoint
let parameters = [
               "username": dataObj.userName,
               "password": dataObj.passWord
    ]

    Alamofire.request(.POST, endPoint, parameters:parameters)
        .responseJSON { response in
            print(response.request)
            print(response.response)
            print(response.result)
            if let JSON = response.result.value {
                print("Did receive JSON data: \(JSON)")
            }
            else {
                print("JSON data is nil.")
            }

    }

    }

如果您想从Data获取静态数据,请使用singleton/shared instance

答案 1 :(得分:0)

您只需使用静态

将Data实例属性更改为class属性
class Data {

static var userName:String!
static var passWord:String! 
static let todoEndpoint: String = "http://jsonplaceholder.typicode.com/todos/1"

}

并在userNamepassWord中设置直接值。

in from login set value

Data.userName = "yourusername" 
Data.passWord = "password"

<强> 2 或者您也可以创建数据对象

class Data {

 var userName:String!
 var passWord:String!
 let todoEndpoint: String = "http://jsonplaceholder.typicode.com/todos/1"

init(userName : String, passWord : String) {

 self.userName = userName 
 self.passWord = passWord
}

和登录类

class Login {
static let dataObj = Data(userName: "usename", passWord: "password")

lazy var endPoint = Login.dataObj.todoEndpoint

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


func getRequest() {

Alamofire.request(.POST, endPoint, parameters:parameters)
    .responseJSON { response in
        print(response.request)
        print(response.response)
        print(response.result)
        if let JSON = response.result.value {
            print("Did receive JSON data: \(JSON)")
        }
        else {
            print("JSON data is nil.")
        }
}
}

}

<强>已更新

我猜你应该创建一个单例对象来获取所有用户的值。

class Data {

 static let sharedInstance = Data()
 static let todoEndpoint: String = "http://jsonplaceholder.typicode.com/todos/1"
 var userName: String?
 var passWord: String?

 // for prevent from creating this class object
 private init() { }

 }

在登录类中。

class Login {

 init(userName: String, passWord: String) {
 Data.sharedInstance.userName = userName
 Data.sharedInstance.passWord = passWord
  }

 /// call this method to login
 func getRequest() {
 print(Data.sharedInstance.userName)
 print(Data.sharedInstance.passWord) 

 Alamofire.request(.POST, Data.endPoint, parameters: ["userName": Data.sharedInstance.userName, "passWord": Data.sharedInstance.passWord])
.responseJSON { response in
print(response.request)
print(response.response)
print(response.result)
if let JSON = response.result.value {
print("Did receive JSON data: \(JSON)")
}
else {
print("JSON data is nil.")
}

}

}
}

从其他一些类创建一个Login实例并从那里传递userNamePassword值,并使用Login对象调用getRequest方法。