I have created an app where I am uploading data to a database. However, I want to assign a specific key to each entry. For example, if the user is making their first entry into db. The key name would be "Entry 1", if they make a second entry, it would be "Entry 2". This is what I tried, but its not working.
#IBAction func doSomething(_ sender: Any) {
var keyCounter = 0
keyCounter = keyCounter + 1
print("Key Counter = ",keyCounter)
}
Here is the output of me pressing the button 3 times. Expected output: Key Counter = 3
Actual output:
I know that whenever I press the button its re-initializing the value of the counter to 0. I am not sure what the best way to approach this is. Any guidance or help would be appreciated.
Do something like this:
class FooViewController: UIViewController {
var keyCounter = 0
#IBAction func doSomething(_ sender: Any) {
keyCounter = keyCounter + 1
print("Key Counter = ",keyCounter)
}
}
Related
I am new to Swift, and I am trying to make a basic question answer app. I want to set up the question button so that whenever I press it, it displays one question, and if I press it again, it shows another question. I have a separate button that will show the answer but I need to connect to which ever question is being asked. How do I do this?
Here is what I have so far, it just asks the question at random, but I want to be able to ask all the questions, not just whatever it picks, I am not sure how to do that though.
#IBAction func question(_ sender: Any) {
let questions = ["What is your name?", "What is your favourite colour?",
"What is your favourite movie?", "What is your major?"]
let randomQuestion = questions.randomElement()
qLabel.text = randomQuestion
}
You can try
var index = 0 // add a current index of the shown question
let questions = ["What is your name?", "What is your favourite colour?",
"What is your favourite movie?", "What is your major?"]
#IBAction func question(_ sender: Any) {
if index < questions.count {
qLabel.text = questions[index]
index += 1
}
else {
// end of questions
}
}
I have a program that displays a random card when you click the screen, and then removes that card from the card deck. But I also want to check if the random card is either of the picture cards in the deck (so jack, king, queen or ace), and then do some stuff if it is one of those. Anyone know how I can do it? When I try to check if the random card is equal to for example cardDeck[0] (the first card in the deck is a picture card), I get an error:
Cannot assign value of type 'UIImage' to type 'Int'
Thanks for all help!
My code looks like this:
var cardDeck = [card1,card2,card2 etc....]
var randomCard: Int = 0
var cardPosition = 0
//row1card1
#IBAction func row1card1tapped(_ sender: UITapGestureRecognizer) {
randomCard = Int.random(in: 0...51)
if cardDeck.count > randomCard + 1 {
if randomCard == cardDeck[0] {
//do stuff here
} else {
row1card1.image = cardDeck[randomCard+1]
cardDeck.remove(at: randomCard)
print("removed:" + "\(cardDeck[randomCard])")
score += 2
currentPoints.text = "\(score)"
}
Your randomCard variable is an Int and you’re trying to compare that with an element from cardDeck which is an image. That’s why you are getting the above exception.
if randomCard == 0 {
//do stuff here
}
So, I'm not sure if I've been staring at my code so long I've become dumb or if this is a bit too advanced for my current skills. I am trying to make a pickerview pop up when the user presses a button using DPPickerManager and when the user selects a color the title of the button is supposed to change to what was selected by the user. Flowing the directions I was able to link the button and pickerview but I can't get the button title to change. Here's my code:
#IBAction func colorPicker(_ sender: UIButton) {
let condition = ["Blue", "Black", "Green", "Orange", "Purple"]
DPPickerManager.shared.showPicker(title: "Strings Picker", selected: "Value 1", strings: condition) { (value, ind, cancel) in
if !cancel {
//sender.titleLabel.text = value![index].text
print(value as Any)
}
}
}
I keep getting this error: Cannot convert value of type '(Any) -> Int' to expected argument type '(UnboundedRange_) -> ()' and I think its because of the value's index but I am unsure. Can someone please help me?
I have used your code and it's working fine for me.
#IBAction func btnActionTapped(_ sender: UIButton) {
let condition = ["Blue", "Black", "Green", "Orange", "Purple"]
DPPickerManager.shared.showPicker(title: "String Picker", selected: "Value 1", strings: condition) { (value, index, cancel) in
if !cancel {
if let value = value {
sender.setTitle(value, for: .normal)
print(value)
}
}
}
}
I just changed following code to get value instead of optional value.
if let value = value {
print(value)
}
I’m getting string value there and print fine in console.
I'm trying to make a type of polling code that once a person presses an option, a value in the Firebase database updates by one but that person cannot access it again.
Right now, I'm trying to test only one button. For reference, my database is:
"Redacted for privacy"
-> "MainContent"
->-> "Polls"
->->-> "GunControl"
->->->-> "BackGroundChecks"
->->->->-> OptionA: 0
->->->->-> OptionB: 0
->->->->-> OptionC: 0
->->->->-> OptionD: 0
Unfortunately I do not have enough points to show a picture so please bear with it.
My Code is:
var counterRef = Database.database().reference()
var optionA: Int = 0
var retrieveData: Int = 0
static var pollAnswered: Bool = false
#IBAction func optionA(_ sender: Any) {
print("Called")
self.counterRef = Database.database().reference(fromURL: "URL Redacted For Privacy")
print("Called2")
updateOptionADos()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func updateOptionADos() {
print("Called3")
counterRef.observeSingleEvent(of: .value, with: { snapshot in
let valString = snapshot.value as? String
if (valString != nil && PollViewController.pollAnswered == false){
var value = Int(valString!)
value = value! + 1
self.retrieveData = value!
self.optionA = value!
print(self.optionA)
self.counterRef.setValue("\(String(describing: value))")
PollViewController.pollAnswered = true
print("Noice")
}
else {
print("ya done f***** up")
}
})
}
It is supposed to call updateOptionADos() to determine what value is in the location I am trying to access (OptionA) and add 1 to it while storing that updated value in the variable optionA for later use in statistics. It builds perfectly, but I keep getting the "ya done f***** up" which tells me that the database value isn't storing for valString. Someone please help me fix this. What have I done wrong? I'm at a loss.
You get the value and try to cast it as a String:
let valString = snapshot.value as? String
And then you cast that string to an Int:
var value = Int(valString!)
If you are storing the count as an Integer, not a string, snapshot.value as? String will return nil, as it is not a String.
I am having trouble configuring a button that:
1st press will do action 1
2nd press will do action 2
3rd press will do action 3
4th press will do action 1
5th press will do action 2
...and so on
Is there any way to configure that button?
Thanks
If you were interested in keeping track of the number of clicks while also doing the job of the 3 alternating tasks:
var counter : Int = 0
#IBAction func buttonClicked(_ sender: Any) {
counter+=1
//print(counter)
switch counter % 3 {
case 1:
// First Action
case 2:
// Second Action
default:
// Third Action
}
}
Do as below in your button action:
var counter = 0
#IBAction func buttonTapped(_ sender: Any) {
counter += 1
switch counter {
case 1:
// do your action
case 2:
// do your action
case 3:
// do your action
counter = 0
default:
break
}
}