我试图将数据从我的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
}
}
错误屏幕截图
答案 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"
}
并在userName
和passWord
中设置直接值。
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实例并从那里传递userName
和Password
值,并使用Login对象调用getRequest方法。