I have two classes in swift, TestView()
and compileResults()
.
I have functions in compileResults()
that return a value. I need that value returned to TestView()
.
I have managed to SET a value in compileResults()
from TestView()
but I can't seem to return one.
I've set up a delegate in the same way I'm using delegates elsewhere but it still doesn't seem to yield results.
TestView:
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:
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"
}
}
EDIT: This seems to work as I want it to, anything wrong with it?
compileResults.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"
}
}
TestView.swift
var comResClass = complileResults()
class TestView: UIViewController, XMLParserDelegate {
override func viewDidLoad() {
super.viewDidLoad()
comResClass.setConTypeResult(val: "WiFi")
let ct = comResClass.getConTypeResult()
print("CT ", ct)
}
}
There are some ways to do this.
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")
}
}
Or this one which is a bit more generic.
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"))
}
}
your problem is the complileResults().setConTypeResult(val: "WiFi")
is a different instance from weak var resStartDelegate: ResStartDelegate?
that's why you are fetching a nil from resStartDelegate.getConTypeResult
.
Your protocol should have the setConTypeResult(string)
method AND change complileResults().setConTypeResult(val: "WiFi")
to 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)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.