Updating UILabel text programmatically using a function












0















so I am trying to update the UIlabel text using a func. I have seen few other questions like mine and I tried their method but it doesn't work. fun returns no value when used in the UIlabel. I have shown my code below.It has to be noted that when I use the function in viewdidload I can retrieve the data.



lazy var name: UILabel = {
let lb = UILabel()
lb.text? = getthename()
lb.lineBreakMode = NSLineBreakMode.byWordWrapping
lb.textColor = .mainGreen
lb.textAlignment = .left
lb.font = UIFont.boldSystemFont(ofSize:12.0)
return lb
}()

func getthename() -> String {
var wins : String = ""
let ud = (Auth.auth().currentUser?.uid)
self.ref = Database.database().reference()
self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in
if let getData = snapshot.value as? [String:Any] {
wins = (getData["lastName "] as? String)!
}
})
return wins
}









share|improve this question

























  • if let getData = snapshot.value could you add a print there, and a print when you do return wins? Which one do you think is printed first, which one is really printed first? You are missing the asynchrone logic/concept.

    – Larme
    Jan 20 at 15:27













  • when I print it in the view did load it does print it correctly. when I use it in UILabel It doesnt return anything

    – Sara
    Jan 20 at 15:29











  • That's not what I asked. I asked you to replace if let getData = snapshot.value as? [String:Any] { with print("Something1"); if let getData = snapshot.value as? [String:Any] { and return wins with print("Somethin2"); return wins, and ask you, in your opinion which print (Something1 or Something2) should appear in console first, and in reality which ones appear first.

    – Larme
    Jan 20 at 15:31













  • not sure if this is what you said: func getthename() { var wins : String = "" let ud = (Auth.auth().currentUser?.uid) self.ref = Database.database().reference() self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in print("Something1") }) print("Somethin2") }

    – Sara
    Jan 20 at 15:37






  • 1





    Finally, you pointed out the issue. Just before it was "it's not working", now you'll see that it's returned string is indeed empty when read. Because your method is asynchrone. Look for "Swift + Asynchrone + Closure" to manage that.

    – Larme
    Jan 20 at 15:42
















0















so I am trying to update the UIlabel text using a func. I have seen few other questions like mine and I tried their method but it doesn't work. fun returns no value when used in the UIlabel. I have shown my code below.It has to be noted that when I use the function in viewdidload I can retrieve the data.



lazy var name: UILabel = {
let lb = UILabel()
lb.text? = getthename()
lb.lineBreakMode = NSLineBreakMode.byWordWrapping
lb.textColor = .mainGreen
lb.textAlignment = .left
lb.font = UIFont.boldSystemFont(ofSize:12.0)
return lb
}()

func getthename() -> String {
var wins : String = ""
let ud = (Auth.auth().currentUser?.uid)
self.ref = Database.database().reference()
self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in
if let getData = snapshot.value as? [String:Any] {
wins = (getData["lastName "] as? String)!
}
})
return wins
}









share|improve this question

























  • if let getData = snapshot.value could you add a print there, and a print when you do return wins? Which one do you think is printed first, which one is really printed first? You are missing the asynchrone logic/concept.

    – Larme
    Jan 20 at 15:27













  • when I print it in the view did load it does print it correctly. when I use it in UILabel It doesnt return anything

    – Sara
    Jan 20 at 15:29











  • That's not what I asked. I asked you to replace if let getData = snapshot.value as? [String:Any] { with print("Something1"); if let getData = snapshot.value as? [String:Any] { and return wins with print("Somethin2"); return wins, and ask you, in your opinion which print (Something1 or Something2) should appear in console first, and in reality which ones appear first.

    – Larme
    Jan 20 at 15:31













  • not sure if this is what you said: func getthename() { var wins : String = "" let ud = (Auth.auth().currentUser?.uid) self.ref = Database.database().reference() self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in print("Something1") }) print("Somethin2") }

    – Sara
    Jan 20 at 15:37






  • 1





    Finally, you pointed out the issue. Just before it was "it's not working", now you'll see that it's returned string is indeed empty when read. Because your method is asynchrone. Look for "Swift + Asynchrone + Closure" to manage that.

    – Larme
    Jan 20 at 15:42














0












0








0








so I am trying to update the UIlabel text using a func. I have seen few other questions like mine and I tried their method but it doesn't work. fun returns no value when used in the UIlabel. I have shown my code below.It has to be noted that when I use the function in viewdidload I can retrieve the data.



lazy var name: UILabel = {
let lb = UILabel()
lb.text? = getthename()
lb.lineBreakMode = NSLineBreakMode.byWordWrapping
lb.textColor = .mainGreen
lb.textAlignment = .left
lb.font = UIFont.boldSystemFont(ofSize:12.0)
return lb
}()

func getthename() -> String {
var wins : String = ""
let ud = (Auth.auth().currentUser?.uid)
self.ref = Database.database().reference()
self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in
if let getData = snapshot.value as? [String:Any] {
wins = (getData["lastName "] as? String)!
}
})
return wins
}









share|improve this question
















so I am trying to update the UIlabel text using a func. I have seen few other questions like mine and I tried their method but it doesn't work. fun returns no value when used in the UIlabel. I have shown my code below.It has to be noted that when I use the function in viewdidload I can retrieve the data.



lazy var name: UILabel = {
let lb = UILabel()
lb.text? = getthename()
lb.lineBreakMode = NSLineBreakMode.byWordWrapping
lb.textColor = .mainGreen
lb.textAlignment = .left
lb.font = UIFont.boldSystemFont(ofSize:12.0)
return lb
}()

func getthename() -> String {
var wins : String = ""
let ud = (Auth.auth().currentUser?.uid)
self.ref = Database.database().reference()
self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in
if let getData = snapshot.value as? [String:Any] {
wins = (getData["lastName "] as? String)!
}
})
return wins
}






swift uilabel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 15:37









Yannick Loriot

6,34622850




6,34622850










asked Jan 20 at 15:22









SaraSara

54




54













  • if let getData = snapshot.value could you add a print there, and a print when you do return wins? Which one do you think is printed first, which one is really printed first? You are missing the asynchrone logic/concept.

    – Larme
    Jan 20 at 15:27













  • when I print it in the view did load it does print it correctly. when I use it in UILabel It doesnt return anything

    – Sara
    Jan 20 at 15:29











  • That's not what I asked. I asked you to replace if let getData = snapshot.value as? [String:Any] { with print("Something1"); if let getData = snapshot.value as? [String:Any] { and return wins with print("Somethin2"); return wins, and ask you, in your opinion which print (Something1 or Something2) should appear in console first, and in reality which ones appear first.

    – Larme
    Jan 20 at 15:31













  • not sure if this is what you said: func getthename() { var wins : String = "" let ud = (Auth.auth().currentUser?.uid) self.ref = Database.database().reference() self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in print("Something1") }) print("Somethin2") }

    – Sara
    Jan 20 at 15:37






  • 1





    Finally, you pointed out the issue. Just before it was "it's not working", now you'll see that it's returned string is indeed empty when read. Because your method is asynchrone. Look for "Swift + Asynchrone + Closure" to manage that.

    – Larme
    Jan 20 at 15:42



















  • if let getData = snapshot.value could you add a print there, and a print when you do return wins? Which one do you think is printed first, which one is really printed first? You are missing the asynchrone logic/concept.

    – Larme
    Jan 20 at 15:27













  • when I print it in the view did load it does print it correctly. when I use it in UILabel It doesnt return anything

    – Sara
    Jan 20 at 15:29











  • That's not what I asked. I asked you to replace if let getData = snapshot.value as? [String:Any] { with print("Something1"); if let getData = snapshot.value as? [String:Any] { and return wins with print("Somethin2"); return wins, and ask you, in your opinion which print (Something1 or Something2) should appear in console first, and in reality which ones appear first.

    – Larme
    Jan 20 at 15:31













  • not sure if this is what you said: func getthename() { var wins : String = "" let ud = (Auth.auth().currentUser?.uid) self.ref = Database.database().reference() self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in print("Something1") }) print("Somethin2") }

    – Sara
    Jan 20 at 15:37






  • 1





    Finally, you pointed out the issue. Just before it was "it's not working", now you'll see that it's returned string is indeed empty when read. Because your method is asynchrone. Look for "Swift + Asynchrone + Closure" to manage that.

    – Larme
    Jan 20 at 15:42

















if let getData = snapshot.value could you add a print there, and a print when you do return wins? Which one do you think is printed first, which one is really printed first? You are missing the asynchrone logic/concept.

– Larme
Jan 20 at 15:27







if let getData = snapshot.value could you add a print there, and a print when you do return wins? Which one do you think is printed first, which one is really printed first? You are missing the asynchrone logic/concept.

– Larme
Jan 20 at 15:27















when I print it in the view did load it does print it correctly. when I use it in UILabel It doesnt return anything

– Sara
Jan 20 at 15:29





when I print it in the view did load it does print it correctly. when I use it in UILabel It doesnt return anything

– Sara
Jan 20 at 15:29













That's not what I asked. I asked you to replace if let getData = snapshot.value as? [String:Any] { with print("Something1"); if let getData = snapshot.value as? [String:Any] { and return wins with print("Somethin2"); return wins, and ask you, in your opinion which print (Something1 or Something2) should appear in console first, and in reality which ones appear first.

– Larme
Jan 20 at 15:31







That's not what I asked. I asked you to replace if let getData = snapshot.value as? [String:Any] { with print("Something1"); if let getData = snapshot.value as? [String:Any] { and return wins with print("Somethin2"); return wins, and ask you, in your opinion which print (Something1 or Something2) should appear in console first, and in reality which ones appear first.

– Larme
Jan 20 at 15:31















not sure if this is what you said: func getthename() { var wins : String = "" let ud = (Auth.auth().currentUser?.uid) self.ref = Database.database().reference() self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in print("Something1") }) print("Somethin2") }

– Sara
Jan 20 at 15:37





not sure if this is what you said: func getthename() { var wins : String = "" let ud = (Auth.auth().currentUser?.uid) self.ref = Database.database().reference() self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in print("Something1") }) print("Somethin2") }

– Sara
Jan 20 at 15:37




1




1





Finally, you pointed out the issue. Just before it was "it's not working", now you'll see that it's returned string is indeed empty when read. Because your method is asynchrone. Look for "Swift + Asynchrone + Closure" to manage that.

– Larme
Jan 20 at 15:42





Finally, you pointed out the issue. Just before it was "it's not working", now you'll see that it's returned string is indeed empty when read. Because your method is asynchrone. Look for "Swift + Asynchrone + Closure" to manage that.

– Larme
Jan 20 at 15:42












1 Answer
1






active

oldest

votes


















0














Try replacing this line of code



lb.text? = getthename()



With lb.text = getthename()



You're currently using optional unwrapping by using the "?" symbol and it is causing that line to essentially equate to nil = getthename()



Also, as mentioned by @Larme the following code is actually asynchronous:



self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in
if let getData = snapshot.value as? [String:Any] {
wins = (getData["lastName "] as? String)!
}
})


Rather than trying to set your label's text within the lazy variable initializer I would recommend you set it elsewhere. You can update the getthename() function to use a reference to the label to update the label's text after the call to observeSingleEvent() has returned.



So something like this



lazy var name: UILabel = {
let lb = UILabel()
lb.lineBreakMode = NSLineBreakMode.byWordWrapping
lb.textColor = .mainGreen
lb.textAlignment = .left
lb.font = UIFont.boldSystemFont(ofSize:12.0)
return lb
}()


Then the getthename() function can be changed to the following. You can call the function from anywhere and it will set the label for you.



func getthename() {
var wins : String = ""
let ud = (Auth.auth().currentUser?.uid)
self.ref = Database.database().reference()
self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: { [weak self] (snapshot) in
if let getData = snapshot.value as? [String:Any] {
self?.name.text = (getData["lastName "] as? String)!
}
})
}


Please notice the [weak self] in the observeSingleEvent() closure as with any asynchronous code you need to check to make sure whether the right variables are still available.






share|improve this answer


























  • I have tried that also. Doesnt work

    – Sara
    Jan 20 at 15:52











  • I've updated my answer for you.

    – Robert Barber
    Jan 20 at 16:07






  • 1





    oh wow worked beautifully.

    – Sara
    Jan 20 at 16:15











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277920%2fupdating-uilabel-text-programmatically-using-a-function%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









0














Try replacing this line of code



lb.text? = getthename()



With lb.text = getthename()



You're currently using optional unwrapping by using the "?" symbol and it is causing that line to essentially equate to nil = getthename()



Also, as mentioned by @Larme the following code is actually asynchronous:



self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in
if let getData = snapshot.value as? [String:Any] {
wins = (getData["lastName "] as? String)!
}
})


Rather than trying to set your label's text within the lazy variable initializer I would recommend you set it elsewhere. You can update the getthename() function to use a reference to the label to update the label's text after the call to observeSingleEvent() has returned.



So something like this



lazy var name: UILabel = {
let lb = UILabel()
lb.lineBreakMode = NSLineBreakMode.byWordWrapping
lb.textColor = .mainGreen
lb.textAlignment = .left
lb.font = UIFont.boldSystemFont(ofSize:12.0)
return lb
}()


Then the getthename() function can be changed to the following. You can call the function from anywhere and it will set the label for you.



func getthename() {
var wins : String = ""
let ud = (Auth.auth().currentUser?.uid)
self.ref = Database.database().reference()
self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: { [weak self] (snapshot) in
if let getData = snapshot.value as? [String:Any] {
self?.name.text = (getData["lastName "] as? String)!
}
})
}


Please notice the [weak self] in the observeSingleEvent() closure as with any asynchronous code you need to check to make sure whether the right variables are still available.






share|improve this answer


























  • I have tried that also. Doesnt work

    – Sara
    Jan 20 at 15:52











  • I've updated my answer for you.

    – Robert Barber
    Jan 20 at 16:07






  • 1





    oh wow worked beautifully.

    – Sara
    Jan 20 at 16:15
















0














Try replacing this line of code



lb.text? = getthename()



With lb.text = getthename()



You're currently using optional unwrapping by using the "?" symbol and it is causing that line to essentially equate to nil = getthename()



Also, as mentioned by @Larme the following code is actually asynchronous:



self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in
if let getData = snapshot.value as? [String:Any] {
wins = (getData["lastName "] as? String)!
}
})


Rather than trying to set your label's text within the lazy variable initializer I would recommend you set it elsewhere. You can update the getthename() function to use a reference to the label to update the label's text after the call to observeSingleEvent() has returned.



So something like this



lazy var name: UILabel = {
let lb = UILabel()
lb.lineBreakMode = NSLineBreakMode.byWordWrapping
lb.textColor = .mainGreen
lb.textAlignment = .left
lb.font = UIFont.boldSystemFont(ofSize:12.0)
return lb
}()


Then the getthename() function can be changed to the following. You can call the function from anywhere and it will set the label for you.



func getthename() {
var wins : String = ""
let ud = (Auth.auth().currentUser?.uid)
self.ref = Database.database().reference()
self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: { [weak self] (snapshot) in
if let getData = snapshot.value as? [String:Any] {
self?.name.text = (getData["lastName "] as? String)!
}
})
}


Please notice the [weak self] in the observeSingleEvent() closure as with any asynchronous code you need to check to make sure whether the right variables are still available.






share|improve this answer


























  • I have tried that also. Doesnt work

    – Sara
    Jan 20 at 15:52











  • I've updated my answer for you.

    – Robert Barber
    Jan 20 at 16:07






  • 1





    oh wow worked beautifully.

    – Sara
    Jan 20 at 16:15














0












0








0







Try replacing this line of code



lb.text? = getthename()



With lb.text = getthename()



You're currently using optional unwrapping by using the "?" symbol and it is causing that line to essentially equate to nil = getthename()



Also, as mentioned by @Larme the following code is actually asynchronous:



self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in
if let getData = snapshot.value as? [String:Any] {
wins = (getData["lastName "] as? String)!
}
})


Rather than trying to set your label's text within the lazy variable initializer I would recommend you set it elsewhere. You can update the getthename() function to use a reference to the label to update the label's text after the call to observeSingleEvent() has returned.



So something like this



lazy var name: UILabel = {
let lb = UILabel()
lb.lineBreakMode = NSLineBreakMode.byWordWrapping
lb.textColor = .mainGreen
lb.textAlignment = .left
lb.font = UIFont.boldSystemFont(ofSize:12.0)
return lb
}()


Then the getthename() function can be changed to the following. You can call the function from anywhere and it will set the label for you.



func getthename() {
var wins : String = ""
let ud = (Auth.auth().currentUser?.uid)
self.ref = Database.database().reference()
self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: { [weak self] (snapshot) in
if let getData = snapshot.value as? [String:Any] {
self?.name.text = (getData["lastName "] as? String)!
}
})
}


Please notice the [weak self] in the observeSingleEvent() closure as with any asynchronous code you need to check to make sure whether the right variables are still available.






share|improve this answer















Try replacing this line of code



lb.text? = getthename()



With lb.text = getthename()



You're currently using optional unwrapping by using the "?" symbol and it is causing that line to essentially equate to nil = getthename()



Also, as mentioned by @Larme the following code is actually asynchronous:



self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: {(snapshot) in
if let getData = snapshot.value as? [String:Any] {
wins = (getData["lastName "] as? String)!
}
})


Rather than trying to set your label's text within the lazy variable initializer I would recommend you set it elsewhere. You can update the getthename() function to use a reference to the label to update the label's text after the call to observeSingleEvent() has returned.



So something like this



lazy var name: UILabel = {
let lb = UILabel()
lb.lineBreakMode = NSLineBreakMode.byWordWrapping
lb.textColor = .mainGreen
lb.textAlignment = .left
lb.font = UIFont.boldSystemFont(ofSize:12.0)
return lb
}()


Then the getthename() function can be changed to the following. You can call the function from anywhere and it will set the label for you.



func getthename() {
var wins : String = ""
let ud = (Auth.auth().currentUser?.uid)
self.ref = Database.database().reference()
self.ref.child("Users").child(ud!).observeSingleEvent(of: .value, with: { [weak self] (snapshot) in
if let getData = snapshot.value as? [String:Any] {
self?.name.text = (getData["lastName "] as? String)!
}
})
}


Please notice the [weak self] in the observeSingleEvent() closure as with any asynchronous code you need to check to make sure whether the right variables are still available.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 20 at 16:07

























answered Jan 20 at 15:51









Robert BarberRobert Barber

365




365













  • I have tried that also. Doesnt work

    – Sara
    Jan 20 at 15:52











  • I've updated my answer for you.

    – Robert Barber
    Jan 20 at 16:07






  • 1





    oh wow worked beautifully.

    – Sara
    Jan 20 at 16:15



















  • I have tried that also. Doesnt work

    – Sara
    Jan 20 at 15:52











  • I've updated my answer for you.

    – Robert Barber
    Jan 20 at 16:07






  • 1





    oh wow worked beautifully.

    – Sara
    Jan 20 at 16:15

















I have tried that also. Doesnt work

– Sara
Jan 20 at 15:52





I have tried that also. Doesnt work

– Sara
Jan 20 at 15:52













I've updated my answer for you.

– Robert Barber
Jan 20 at 16:07





I've updated my answer for you.

– Robert Barber
Jan 20 at 16:07




1




1





oh wow worked beautifully.

– Sara
Jan 20 at 16:15





oh wow worked beautifully.

– Sara
Jan 20 at 16:15




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277920%2fupdating-uilabel-text-programmatically-using-a-function%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Homophylophilia

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage