我很快有两个班,TestView()
和compileResults()
。
我在compileResults()
中有返回值的函数。我需要将该值返回给TestView()
。
我设法在compileResults()
的{{1}}中设置了一个值,但似乎无法返回一个值。
我已经以与在其他地方使用代理相同的方式设置了一个代理,但它似乎仍未产生结果。
TestView:
TestView()
compileResults:
import UIKit
import Foundation
class TestView: UIViewController, XMLParserDelegate {
weak var resStartDelegate: ResStartDelegate?
override func viewDidLoad() {
super.viewDidLoad()
resStartDelegate?.setConTypeResult(val: "WiFi")
print("ConType is: ", resStartDelegate?.getConTypeResult()) //outputs value nil
}
编辑:这似乎可以按我的意愿工作,这有什么问题吗?
compileResults.swift
import Foundation
protocol ResStartDelegate: class {
func getConTypeResult() -> String
func setConTypeResult(val: String)
}
var testViewDelegate = TestView()
var theTestResults : [String: String] = [
"conType": ""
]
class complileResults : ResStartDelegate {
init() {
setDelegate()
}
func setDelegate() {
testViewDelegate.resStartDelegate = self
}
func resetResults() {
theTestResults["conType"] = ""
}
func setConTypeResult(val: String) {
theTestResults["conType"] = val
print("Just set contype to: ", theTestResults["conType"]) //prints correctly what has been passed from TestView
}
func getConTypeResult() -> String {
return theTestResults["conType"] ?? "Err"
}
}
TestView.swift
import Foundation
class complileResults {//: ResStartDelegate {
var theTestResults : [String: String] = [
"conType": "",
"date": "",
"time": "",
"jitter": "",
"loss": "",
"dspeed": "",
"uspeed": "",
"delay": ""
]
func setConTypeResult(val: String) {
theTestResults["conType"] = val
}
func getConTypeResult() -> String {
return theTestResults["conType"] ?? "Err"
}
}
答案 0 :(得分:2)
有一些方法可以做到这一点。
class complileResults {
var theTestResults : [String: String] = [
"conType": "",
"date": "",
"time": "",
"jitter": "",
"loss": "",
"dspeed": "",
"uspeed": "",
"delay": ""
]
}
class TestView: UIViewController, XMLParserDelegate {
let result = complileResults()
override func viewDidLoad() {
super.viewDidLoad()
result.theTestResults["conType"] = "WiFi"
print(result.theTestResults["conType"] ?? "Err")
}
}
或者说这个更通用。
class orcomplileResults {
var theTestResults : [String: String] = [
"conType": "",
"date": "",
"time": "",
"jitter": "",
"loss": "",
"dspeed": "",
"uspeed": "",
"delay": ""
]
func setResult(key: String, value: String) {
theTestResults[key] = value
}
func getResult(key: String) -> String {
return (theTestResults[key] ?? "Err")
}
}
class orTestView: UIViewController, XMLParserDelegate {
let result = orcomplileResults()
override func viewDidLoad() {
super.viewDidLoad()
result.setResult(key: "conType", value: "WiFi")
print(result.getResult(key: "conType"))
}
}
答案 1 :(得分:0)
您的问题是complileResults().setConTypeResult(val: "WiFi")
与weak var resStartDelegate: ResStartDelegate?
是不同的实例,这就是为什么要从resStartDelegate.getConTypeResult
获取零的原因。
您的协议应具有setConTypeResult(string)
方法,并将complileResults().setConTypeResult(val: "WiFi")
更改为resStartDelegate?.setConTypeResult(val: "WiFi")
class TestView: UIViewController, XMLParserDelegate {
weak var resStartDelegate: ResStartDelegate?
override func viewDidLoad() {
super.viewDidLoad()
resStartDelegate?.setConTypeResult(val: "WiFi")
print("ConType is: ", resStartDelegate?.getConTypeResult()) //outputs value nil
}
protocol ResStartDelegate: class {
func getConTypeResult() -> String
func setConTypeResult(val: String)
}