Objective:
Type a number in a Text field in the user interface. Press "GO!" button (user interface) and: 1.The number from the textfield is passed onto a PHP script as a post variable (via NSURLSession..dataTaskWithRequest).
2.PHP script uses that variable as part of a query to MySQL.
3.Query result is returned to the iOS App
4.Query result is displayed in a Text View (User Interface)
Setup:
Inside the ViewController class I have written an extra function "mysqlget(MyNum: Int)" that takes the number turns it into the HTTPBody of my NSMutableURLRequest. Function proceeds to setup the NSURLSession, setup the dataTaskwithRequestTask and "resume" i.e. start the task.
My "Go!" button with the @IBActon setup for touchupinside (Also located in ViewController class) has code to:
1.Check if the string entered in the text field is a string
2.Pop up warning if textfield is not a number
3.Call the mysqlget function passing the textfield number in.
4.Change the Textview.text (not the text field) to variable containing the data inside the dataTaskWithRequest task.
Problem:
The scope of the data variable inside dataTaskWithRequest is limited to inside the dataTaskwithRequest body. Even inside the mysqlget function, just after the myDataTask.resume() the variable no longer exists. So obviously, I cannot pass the variable back to my @IBAction Go! button.
Failed Solutions:
Putting a " -> NSString" on my datataskwithrequest instead of void (block is supposed to terminate with the end of the Call Handler)
setting mysqlget to return a NSString and having mysqlget last line include a "return data_frominside_datataskwithrequest" - data does not survive outside request
Trying to update the Textview.text from inside the dataTaskWithRequest block - Even though the @IBOutlet for the textview is declared right at the top, when I try to do "textview.text = data", xcode prompts - must use "self." suggesting it can't see the @IBOutlet? using self.textview.text allows the program to run, but it crashes once it reaches the line.
(I know this is generally bad) Declare a global variable outside of ViewController and make globalvariable = data. Somehow the variable remains blank.
I have tested the code and all parts work up to the point of getting the data I need inside myDataTask. Only thing I can't do is transfer that data outside of myDataTask
Schematic:
var attemptedGlobalVar: String = ""
class ViewController {
@IBOutlet myNum
@IBOutlet TextField
@IBAction GoClicked {
.
.
[code to validate myNum]
.
.
mysqlget(Int(myNum.text))
Textfield.text = data_I_need //nothing returns here
[This is where I want to process my "data_I_need"]
}
override func viewDidLoad {
[default stuff]
}
override func didReceiveMemoryWarning {
[default stuff]
}
func mysqlget(aNum: Int) {
[Setup NSURL]
[Setup POST string]
[Setup NSMutableURLRequest (HTTPMethod, HTTPBody)]
let myDataTask = NSURLSessions.sharedSession().dataTaskWithRequest (myRequest, completion handler: {(requestData, requestResponse, requestError) -> Void in
data_I_need = (NSString(data: requestData, encoding: NSUTF8StringEncoding))!
[data_I_need exists here]
[setting TextField.text = data_I_need xcode warns I need to use self.TextField.text]
}
myGetTask.resume()
[data_I_need is blank here]
}
}
Concise question:
- How can I get data_I_need out of the myGetTask block up the @IBAction GoClicked?
Sorry for the length of question. I just want to provide full context. If there are other suggestions you would like to make (hopefully as an alternative to my desired action) please let me know. I have limited programming experience and welcome any comments or suggestions. Thank you.