How do I update this variable with data [closed] - swift

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
New programmer here! I'm working with a network request and below is a snippet of my code. After I complete the network request I want to store a value from the request into a variable. However, when the variable is called outside of the if let statement it is NOT updated. Essentially, the print statement inside the if let statement prints the value I'm looking for, but the print statement outside of the if let statement prints the default value I set for that variable. The variable is global. I need to use that updated variable elsewhere in my code, any ideas?
if let data = data {
self.nameLabel.text = data.name
person = data.name
print(person)
}
print(person)

First of all you didn't showed complete code so I am guessing you need main queue to update your UILabel first as shown in below code:
DispatchQueue.main.async {
self.nameLabel.text = data.name
}
next thing is if you want to access the value somewhere else once your request completes and UILabel updated then you can get the value from it this way:
let value = self.nameLabel.text!
And if you want to access the value once request complete then you can use closure and HERE you can check example of it.

Related

how to stop auto reload firebase [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
how do I stop or bypass the auto reload in Firebase Realtime Database, I want to manually reload the data. for instance, I want to use UIRefresh controller instead of using firebase cache reload
If you want to only load data once, and not automatically get notified of changes, you can use the observeSingleEvent methods.
Also see the documentation on reading data once, which contains the following example:
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let username = value?["username"] as? String ?? ""
let user = User(username: username)
// ...
}) { (error) in
print(error.localizedDescription)
}
The above example reads the data of users/$userID once, either from the server or from the local cache. A note on that last aspect: you shouldn't combine observeSingleEvent with enabling disk persistence as they don't work well together as explained in Firebase Offline Capabilities and addListenerForSingleValueEvent

How to fix button clicked not updating but others do [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm making a math game. Every time I click a button a new question should appear with different results to choose from.
However every time I click the button which I think is the result it doesn't update the text but the others do.
See github: https://github.com/Combii/BrainTrainerSwift
Use UIButton.setTitle(, for:) method. I suggest you to change code in ViewController.swift:
func setNumbers(numberDic: Dictionary<String, Int>) {
let btns = [bt1, bt2, bt3, bt4]
UIView.performWithoutAnimation {
for (position, number) in numberDic {
btns[(Int(position) ?? 0) - 1]?.setTitle(String(number), for: .normal)
}
}
}

Expression type '[Card]' is ambiguous without more context with an array passed into function

So I have this function remoteDrawCards, which takes an array of cards that is supposed to come from a remote computer, draw the same number of cards, and if they're the same allocate those cards to the remote player. I've taken out code that doesn't pertain to my issue and left a comment in it's place:
func remoteDrawCards(player: GKPlayer, remoteCards : [Card]) {
//guard to check if the card count is the same remote and locally
remoteCards.enumerated().map({(i, card) in
// guard to check if each card is the same remote and locally
let playerStruct = players![player.playerID!]!
// give the cards to the player
})
undoHistory.append(play: Play(action: .Draw(cards: remoteCards, player: player)))
}
Anyhow, I get an error on the remoteCards variable in the undoHistory.append line of code that says:
Expression type '[Card]' is ambiguous without more context
I don't understand why swift thinks this is ambiguous, and I'm not seeing any of the other times this has been asked that it occurs when a parameter is used.
What it is is that my .Draw was incorrectly formatted. My original issue was with a
for card in remoteCards{
}
giving the error, which I factored out before I asked this question with a .map(), but I thought the error came back when I used the variable in undoHistory. My proper use of .Draw is without the player, which is a variable in Play.

In Unity know which button is clicked [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have two buttons create and join. Both button's on click event I move to next scene, but in next scene I have two scripts networkmanager and networkclient.
Now if I press create button I want to call only networkmanger not networkclient. So how can I disable the networkclient script and join I want to disabled networkmanger?
How can I know which button is been pressed?
public void OnPress(){
string name = this.gameObject.name;
switch(name){
case "Create":break;
case "Join":break;
}
}
You can read the name of the object the script is attached to.
If this is not suitable for any given reason (both scripts are on same object), you can pass a parameter to the method.
public void OnPress(string name){
switch(name){
case "Create":break;
case "Join":break;
}
}
then you give the name in the inspector.
The final way is to use two different methods for each button.

Unable to set member variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Very strange issue.
I have the following code:
NSDictionary* notificationUserInfo = [pNotification userInfo];
NSManagedObject* newShoppingFilter = [notificationUserInfo valueForKey:#"shoppingListFilter"];
self.shoppingListFilter = newShoppingFilter;
NSLog(#"%# tapped", [newShoppingFilter valueForKey:#"name"]);
For some reason the self.shoppingListFilter = newShoppingFilter is not setting the variable.
I assume that this is some issue with not initializing the self.shoppingListFilter variable in some way but I cannot figure this out. The NSLog shows the right output, newShoppingFilter is not null but self.shoppingListFilter is.
Any help is appreciated.
I bet newShoppingFilter is nil. Most likely, there is no key "shoppingListFilter" in the notification user info dictionary.
Set a breakpoint at the line that assigns a value to self.shoppingListFilter and check the value of newShoppingFilter. Also display the entire contents of notificationUserInfo.
Post the code that creates the user info dictionary and passes it into the notification that you are posting. That would help track down the problem.