User object contains nil after POST request to serverHow to detect tableView cell touched or clicked in swiftFirebase swift ios login system error Assertion failed/Exec_BAD_INSTRUNCTION (code=EXC_i386_INVOP, subcode=0x0)Expand and Collapse tableview cellsTableView not displaying text with JSON data from API callHow to show images from API in CollectionViewAdding a custom UIViewcontroller to subview programmatically but getting an error message “Cannot convert value of type…”How to optimize UITableViewCell, because my UITableView lagsSwift Error - Use of undeclared type 'cell' - Collection ViewTableView Controller is hiding the dropshadow of swipemenu in swift 4BMI app print result is 0 even with variable being hard coded
Bob's unnecessary trip to the shops
Why did my rum cake turn black?
Cubic programming and beyond?
How does one stock fund's charge of 1% more in operating expenses than another fund lower expected returns by 10%?
Are there any double stars that I can actually see orbit each other?
Would letting a multiclass character rebuild their character to be single-classed be game-breaking?
How to make 1,1-diphenyl-1-butene from benzophenone and 1-bromopropane?
Was adding milk to tea started to reduce employee tea break time?
Is `curl something | sudo bash -` a reasonably safe installation method?
Mbed Cortex-m hardfault when sending data via TCP
Missing Contours in ContourPlot
What does `[$'rn']` mean?
When is pointing out a person's hypocrisy not considered to be a logical fallacy?
How to repair a laptop's screen hinges?
Why limit to revolvers?
Can I intentionally omit previous work experience or pretend it doesn't exist when applying for jobs?
How is the idea of "X comes a distant third" commonly expressed in French?
Why does the autopilot disengage even when it does not receive pilot input?
Historic symbols representing peasants/oppressed persons fighting back?
Can you negate disadvantage on throwing a net by using the Lunging Attack maneuver of the Battle Master fighter?
Is Arc Length always irrational between two rational points?
Is it rude to tell recruiters I would only change jobs for a better salary?
Too many spies!
Why does Hellboy file down his horns?
User object contains nil after POST request to server
How to detect tableView cell touched or clicked in swiftFirebase swift ios login system error Assertion failed/Exec_BAD_INSTRUNCTION (code=EXC_i386_INVOP, subcode=0x0)Expand and Collapse tableview cellsTableView not displaying text with JSON data from API callHow to show images from API in CollectionViewAdding a custom UIViewcontroller to subview programmatically but getting an error message “Cannot convert value of type…”How to optimize UITableViewCell, because my UITableView lagsSwift Error - Use of undeclared type 'cell' - Collection ViewTableView Controller is hiding the dropshadow of swipemenu in swift 4BMI app print result is 0 even with variable being hard coded
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
After sending a POST request using alamofire to register a user in my app, I am attempting to create a User object from the returned user_id from my server. However, when I attempt to print some attributes of this user object directly after I initialize it, it works fine. However, when I try and print some attributes of it after the alamofire post request has finished executing, I am getting nil.
import UIKit
import Alamofire
class ViewController: UIViewController
var user: User!
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var phone_number_field: UITextField!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//MARK: Actions
@IBAction func generateQR(_ sender: UIButton)
let name = nameField.text
let email = emailField.text
let phone_number = phone_number_field.text
let params: Parameters = ["name": name!, "email": email!, "phone_number": phone_number!]
AF.request("http://127.0.0.1:5000/register", method: .post, parameters: params, encoding: JSONEncoding.default).responseString response in
switch response.result
case .success:
let user_id = response.result.value
self.user = User(name: name!, email: email!, phone_number: phone_number!, user_id: user_id!)
print(self.user?.name) // <--This print statement works correctly -->
case .failure:
print("Error")
print(self.user?.name) // <--This prints nil and in the debugger, self.user shows as nil-->
performSegue(withIdentifier: "qrSegue", sender: self)
//END Actions
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if segue.destination is QRCodeController
//Pass user object
let qr_controller = segue.destination as? QRCodeController
qr_controller?.user = self.user
swift xcode alamofire
add a comment |
After sending a POST request using alamofire to register a user in my app, I am attempting to create a User object from the returned user_id from my server. However, when I attempt to print some attributes of this user object directly after I initialize it, it works fine. However, when I try and print some attributes of it after the alamofire post request has finished executing, I am getting nil.
import UIKit
import Alamofire
class ViewController: UIViewController
var user: User!
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var phone_number_field: UITextField!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//MARK: Actions
@IBAction func generateQR(_ sender: UIButton)
let name = nameField.text
let email = emailField.text
let phone_number = phone_number_field.text
let params: Parameters = ["name": name!, "email": email!, "phone_number": phone_number!]
AF.request("http://127.0.0.1:5000/register", method: .post, parameters: params, encoding: JSONEncoding.default).responseString response in
switch response.result
case .success:
let user_id = response.result.value
self.user = User(name: name!, email: email!, phone_number: phone_number!, user_id: user_id!)
print(self.user?.name) // <--This print statement works correctly -->
case .failure:
print("Error")
print(self.user?.name) // <--This prints nil and in the debugger, self.user shows as nil-->
performSegue(withIdentifier: "qrSegue", sender: self)
//END Actions
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if segue.destination is QRCodeController
//Pass user object
let qr_controller = segue.destination as? QRCodeController
qr_controller?.user = self.user
swift xcode alamofire
performSegue call first then after completion block call. you have to add segue code into success case.
– Vishal Patel
Mar 26 at 5:22
Addprint("Alamofire Completion")just before the lineswitch response.result. You'll see that the order ofprint(self.user?.name)output and that one is not the one you think of. You are missing the asynchrone concept.
– Larme
Mar 26 at 11:19
add a comment |
After sending a POST request using alamofire to register a user in my app, I am attempting to create a User object from the returned user_id from my server. However, when I attempt to print some attributes of this user object directly after I initialize it, it works fine. However, when I try and print some attributes of it after the alamofire post request has finished executing, I am getting nil.
import UIKit
import Alamofire
class ViewController: UIViewController
var user: User!
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var phone_number_field: UITextField!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//MARK: Actions
@IBAction func generateQR(_ sender: UIButton)
let name = nameField.text
let email = emailField.text
let phone_number = phone_number_field.text
let params: Parameters = ["name": name!, "email": email!, "phone_number": phone_number!]
AF.request("http://127.0.0.1:5000/register", method: .post, parameters: params, encoding: JSONEncoding.default).responseString response in
switch response.result
case .success:
let user_id = response.result.value
self.user = User(name: name!, email: email!, phone_number: phone_number!, user_id: user_id!)
print(self.user?.name) // <--This print statement works correctly -->
case .failure:
print("Error")
print(self.user?.name) // <--This prints nil and in the debugger, self.user shows as nil-->
performSegue(withIdentifier: "qrSegue", sender: self)
//END Actions
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if segue.destination is QRCodeController
//Pass user object
let qr_controller = segue.destination as? QRCodeController
qr_controller?.user = self.user
swift xcode alamofire
After sending a POST request using alamofire to register a user in my app, I am attempting to create a User object from the returned user_id from my server. However, when I attempt to print some attributes of this user object directly after I initialize it, it works fine. However, when I try and print some attributes of it after the alamofire post request has finished executing, I am getting nil.
import UIKit
import Alamofire
class ViewController: UIViewController
var user: User!
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var phone_number_field: UITextField!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//MARK: Actions
@IBAction func generateQR(_ sender: UIButton)
let name = nameField.text
let email = emailField.text
let phone_number = phone_number_field.text
let params: Parameters = ["name": name!, "email": email!, "phone_number": phone_number!]
AF.request("http://127.0.0.1:5000/register", method: .post, parameters: params, encoding: JSONEncoding.default).responseString response in
switch response.result
case .success:
let user_id = response.result.value
self.user = User(name: name!, email: email!, phone_number: phone_number!, user_id: user_id!)
print(self.user?.name) // <--This print statement works correctly -->
case .failure:
print("Error")
print(self.user?.name) // <--This prints nil and in the debugger, self.user shows as nil-->
performSegue(withIdentifier: "qrSegue", sender: self)
//END Actions
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if segue.destination is QRCodeController
//Pass user object
let qr_controller = segue.destination as? QRCodeController
qr_controller?.user = self.user
swift xcode alamofire
swift xcode alamofire
asked Mar 26 at 5:12
nilay neeranjunnilay neeranjun
7011 bronze badges
7011 bronze badges
performSegue call first then after completion block call. you have to add segue code into success case.
– Vishal Patel
Mar 26 at 5:22
Addprint("Alamofire Completion")just before the lineswitch response.result. You'll see that the order ofprint(self.user?.name)output and that one is not the one you think of. You are missing the asynchrone concept.
– Larme
Mar 26 at 11:19
add a comment |
performSegue call first then after completion block call. you have to add segue code into success case.
– Vishal Patel
Mar 26 at 5:22
Addprint("Alamofire Completion")just before the lineswitch response.result. You'll see that the order ofprint(self.user?.name)output and that one is not the one you think of. You are missing the asynchrone concept.
– Larme
Mar 26 at 11:19
performSegue call first then after completion block call. you have to add segue code into success case.
– Vishal Patel
Mar 26 at 5:22
performSegue call first then after completion block call. you have to add segue code into success case.
– Vishal Patel
Mar 26 at 5:22
Add
print("Alamofire Completion") just before the line switch response.result. You'll see that the order of print(self.user?.name) output and that one is not the one you think of. You are missing the asynchrone concept.– Larme
Mar 26 at 11:19
Add
print("Alamofire Completion") just before the line switch response.result. You'll see that the order of print(self.user?.name) output and that one is not the one you think of. You are missing the asynchrone concept.– Larme
Mar 26 at 11:19
add a comment |
1 Answer
1
active
oldest
votes
Your service call is async. Read about sync vs async methods.
Medium
Ray Wenderlich
AF sent request to the server and codes below continues working. When your response will arrive then success or failure blocks start working.
So the user object is not assigned until the success block is running.
Here is solution:
AF.request("http://127.0.0.1:5000/register", method: .post, parameters: params, encoding: JSONEncoding.default).responseString response in
switch response.result
case .success:
let user_id = response.result.value
self.user = User(name: name!, email: email!, phone_number: phone_number!, user_id: user_id!)
print(self.user?.name) // <--This print statement works correctly -->
performSegue(withIdentifier: "qrSegue", sender: self)
case .failure:
print("Error")
And you can show loading in your page while pending response from request and hide it on success.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55350232%2fuser-object-contains-nil-after-post-request-to-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your service call is async. Read about sync vs async methods.
Medium
Ray Wenderlich
AF sent request to the server and codes below continues working. When your response will arrive then success or failure blocks start working.
So the user object is not assigned until the success block is running.
Here is solution:
AF.request("http://127.0.0.1:5000/register", method: .post, parameters: params, encoding: JSONEncoding.default).responseString response in
switch response.result
case .success:
let user_id = response.result.value
self.user = User(name: name!, email: email!, phone_number: phone_number!, user_id: user_id!)
print(self.user?.name) // <--This print statement works correctly -->
performSegue(withIdentifier: "qrSegue", sender: self)
case .failure:
print("Error")
And you can show loading in your page while pending response from request and hide it on success.
add a comment |
Your service call is async. Read about sync vs async methods.
Medium
Ray Wenderlich
AF sent request to the server and codes below continues working. When your response will arrive then success or failure blocks start working.
So the user object is not assigned until the success block is running.
Here is solution:
AF.request("http://127.0.0.1:5000/register", method: .post, parameters: params, encoding: JSONEncoding.default).responseString response in
switch response.result
case .success:
let user_id = response.result.value
self.user = User(name: name!, email: email!, phone_number: phone_number!, user_id: user_id!)
print(self.user?.name) // <--This print statement works correctly -->
performSegue(withIdentifier: "qrSegue", sender: self)
case .failure:
print("Error")
And you can show loading in your page while pending response from request and hide it on success.
add a comment |
Your service call is async. Read about sync vs async methods.
Medium
Ray Wenderlich
AF sent request to the server and codes below continues working. When your response will arrive then success or failure blocks start working.
So the user object is not assigned until the success block is running.
Here is solution:
AF.request("http://127.0.0.1:5000/register", method: .post, parameters: params, encoding: JSONEncoding.default).responseString response in
switch response.result
case .success:
let user_id = response.result.value
self.user = User(name: name!, email: email!, phone_number: phone_number!, user_id: user_id!)
print(self.user?.name) // <--This print statement works correctly -->
performSegue(withIdentifier: "qrSegue", sender: self)
case .failure:
print("Error")
And you can show loading in your page while pending response from request and hide it on success.
Your service call is async. Read about sync vs async methods.
Medium
Ray Wenderlich
AF sent request to the server and codes below continues working. When your response will arrive then success or failure blocks start working.
So the user object is not assigned until the success block is running.
Here is solution:
AF.request("http://127.0.0.1:5000/register", method: .post, parameters: params, encoding: JSONEncoding.default).responseString response in
switch response.result
case .success:
let user_id = response.result.value
self.user = User(name: name!, email: email!, phone_number: phone_number!, user_id: user_id!)
print(self.user?.name) // <--This print statement works correctly -->
performSegue(withIdentifier: "qrSegue", sender: self)
case .failure:
print("Error")
And you can show loading in your page while pending response from request and hide it on success.
edited Mar 26 at 5:23
answered Mar 26 at 5:17
ibrahimyilmazibrahimyilmaz
1,56315 silver badges25 bronze badges
1,56315 silver badges25 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55350232%2fuser-object-contains-nil-after-post-request-to-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
performSegue call first then after completion block call. you have to add segue code into success case.
– Vishal Patel
Mar 26 at 5:22
Add
print("Alamofire Completion")just before the lineswitch response.result. You'll see that the order ofprint(self.user?.name)output and that one is not the one you think of. You are missing the asynchrone concept.– Larme
Mar 26 at 11:19