我具有从json初始化类并通过从视图控制器的文本字段发送参数来发送值的功能。我应该在不发送参数的情况下在另一个视图控制器中显示此类的值。这是我的代码
Alamofire.request(url!, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON(completionHandler: { response in
if response.data != nil {
switch response.result
{
case .failure(let error):
print(error)
case .success(let value):
let json = JSON(value)
guard let dataarr = json["my_profile"].arrayObject as? [String] else {return}
if dataarr.count > 0 {
teacherinfo.name = dataarr[0]
teacherinfo.surname = dataarr[1]
teacherinfo.email = dataarr[2]
teacherinfo.phone = dataarr[3]
completion(teacherinfo,true)
}
else
{
completion(teacherinfo,false)
}
}
}
})
这是我的课程
class TeacherInfo {
var name : String = ""
var surname : String = ""
var email : String = ""
var phone : String = ""
}
我在这里初始化它
func search2() {
TeacherSearcher.SearchTeachers(name: about.nametextfield.text!, surname: about.surnametextfiel.text!) { (teacherinfo,success) in
if success! {
self.navigationController?.pushViewController(TeacherViewController(), animated: true)
}
else {
print("not")
}
}
}
我应该在不初始化参数的情况下返回class的值
答案 0 :(得分:0)
创建结构
struct TeacherInfo {
static var name : String = ""
static var surname : String = ""
static var email : String = ""
static var phone : String = ""
}
传递数据
/// Save Data in Struct
/// Now this struct is Accessible in all your controllers
/// Without passing any Object in it
TeacherInfo.email = "Teacher Email"
TeacherInfo.name = "iOS"
TeacherInfo.surname = "Geek"
TeacherInfo.phone = "123456789"
/// Destination VC
print(TeacherInfo.email)
print(TeacherInfo.name)
print(TeacherInfo.surname)
print(TeacherInfo.phone)
注意,这样您就可以在不初始化类的情况下将数据从一个VC传递到另一个VC
答案 1 :(得分:0)
struct TeacherInfo {
static var name : String!
static var surname : String!
static var email : String!
static var phone : String!
init(name:String, surname:String, email:String, phone:String) {
self.name = name
self.surname = surname
self.email = email
self.phone = phone
}
}
-
if dataarr.count > 0 {
let element = TeacherInfo(name:dataarr[0], surname:dataarr[1], email:dataarr[2], phone:dataarr[3])
completion(teacherinfo,true)
}