How do I return the next array when a button is pressed? - ios

I'm new to programming and Swift and have been following along with a tutorial. I'm learning about the MVC design pattern and I have a function that changes the Title label and Button labels when a button is pressed. Once we get to Story 1 or Story 2 and we select any of the choices, I want it to restart to Story 0 and the original choices. However, when the story changes to Story 1, choice2 triggers Story 2 and when the story changes to Story 2, choice1 triggers Story1. I want it to reset to Story 0 when any choice is chosen. I tried to modify the nextStory function but have been unsuccessful. Any help would be greatly appreciated.!
Model:
struct StoryBrain {
var storyNumber = 0
let story = [Story(t: "You see a fork in the road.", c1: "Take a left.", c2: "Take a right."),
Story(t: "You see a tiger.", c1: "Shout for help.", c2: "Play dead."),
Story(t: "You find a treasure chest", c1: "Open it.", c2: "Check for traps.")
]
func getStoryTitle() -> String {
return story[storyNumber].title
}
func getChoice1() -> String {
return story[storyNumber].choice1
}
func getChoice2() -> String {
return story[storyNumber].choice2
}
mutating func nextStory(_ userChoice: String) {
if userChoice == story[storyNumber].choice1 {
storyNumber = 1
} else if userChoice == story[storyNumber].choice2 {
storyNumber = 2
}
}
}
View Controller
#IBAction func choiceMade(_ sender: UIButton) {
storyBrain.nextStory(sender.currentTitle!)
updateUI()
}
func updateUI() {
storyLabel.text = storyBrain.getStoryTitle()
choice1Button.setTitle(storyBrain.getChoice1(), for: .normal)
choice2Button.setTitle(storyBrain.getChoice2(), for: .normal)
}

If storyNumber is 0 then check choice for update next story.
If storyNumber is not 0 it might be 1 or 2 you should reset story to 0.
mutating func nextStory(_ userChoice: String) {
if storyNumber == 0 {
if userChoice == story[storyNumber].c1 {
storyNumber = 1
} else if userChoice == story[storyNumber].c2 {
storyNumber = 2
}
} else {
storyNumber = 0
}
}
I hope this help.

Hopefully, I am understanding you correctly. You just want to restart once you reach story 2.
mutating func nextStory(_ userChoice: String) {
guard storyNumber != 0 else { storyNumber = 0; return }
if userChoice == story[storyNumber].choice1 {
storyNumber = 1
return
} else if userChoice == story[storyNumber].choice2 {
storyNumber = 2
return
}
}
Hopefully, something like this works for you. Just off the top of my head.

Related

Swift UIKit label text doesn't update / view doesn't update

I have a problem:
I have a list of items this is controller A, and when I click on any item I go to controller B (item info), I then execute the ledLightingButton_Tapped function by pressing the corresponding button that activates the LED indicator for the animal.
#IBAction func ledLightingButton_Tapped(_ sender: Any) {
if !GlobalData.shared.led_animals.contains(GlobalData.shared.selectedAnimalId) {
GlobalData.shared.led_animals.append(GlobalData.shared.selectedAnimalId)
}
activateLED(at: GlobalData.shared.selectedAnimalId)
}
func activateLED(at animalId: String) {
ServerSocket.shared?.perform(
op: "ActivateLED",
with: [
"light_duration": "180",
"led_color": "White",
"client_data": "",
"led_animals": [animalId]
]
) { err, data in
guard err == nil else { return }
print(data)
let ledStatus = data[0]["led_request_status"].stringValue
self.ledStatusLabel.text = ledStatus
GlobalData.shared.isActiveLED = true
self.startTimer()
}
}
Upon successful activation, the animal number is added to the array, and the startTimer is called which every 10 seconds requests checkLEDStatus for all animals in the array.
func startTimer() {
timer = Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(updateCowStatus), userInfo: nil, repeats: true)
}
#objc func updateCowStatus() {
self.checkLEDStatus()
}
func checkLEDStatus() {
ServerSocket.shared?.perform(
op: "CheckStatusLED",
with: [
"light_duration": "180",
"led_color": "White",
"client_data": "",
"led_animals": GlobalData.shared.led_animals
]
) { err, data in
guard err == nil else {
GlobalData.shared.isActiveLED = false
self.stopTimer()
return
}
DispatchQueue.global(qos: .background).async {
for i in 0..<data.count {
if GlobalData.shared.selectedAnimalId == data[i]["animal_id"].stringValue {
let ledStatus = data[i]["led_request_status"].stringValue
if ledStatus.contains("Fail") {
guard let index = GlobalData.shared.led_animals.firstIndex(of: GlobalData.shared.selectedAnimalId) else { return }
GlobalData.shared.led_animals.remove(at: index)
}
DispatchQueue.main.async {
self.ledStatusLabel.text = ledStatus
}
}
}
}
}
}
The current status of the animal is displayed on the label. If you go in the controller A and activate the status + get a result from checkedLEDstatus - it is work for one animal - everything works, but if you go to controller B, activate for animal number 1, go out and open animal number 2 - perform activation, return to animal number 1 - then the label is no longer is updated, it does not display the new value, but I check it from debugging and property self.ledStatuslabel.text contains new value but UI didn't update. self.ledStatuslabel.text show old value.
Please help me, thanks!

Detect when a button is pressed outside of the IBAction in Swift?

I'm a beginner programmer making my first real app, a calculator in Swift.
It's mostly working, but one issue I'm running into is how it displays numbers after pressing one of the operator buttons. Currently, whenever an operator button is pressed, I have it set the label at the top of the calculator to "0". But on actual calculators, this top display won't change until another number button is pressed.
If I don't reset the display to 0, then any number buttons that are pressed will add to the current text at the top, and mess up the equation that the calculator will have to do (i.e. 2+2 displays 22, and the solution it displays is 22+2=24)
I'm wondering if it's possible to detect when one of the number buttons is pressed (listed in my code as the intButtonPressed IBAction) outside of the intButtonPressed function? That way I can keep the top label the same until the user starts inputting more text, then I can set it to 0 to prevent the calculator from breaking.
Any other possible (better) solutions would be welcome as well
Here's my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var topLabel: UILabel!
var num1 = Double()
var solution = Double()
var op = String()
#IBAction func intButtonPressed(_ sender: UIButton) {
if topLabel.text == "0" {
topLabel.text = sender.currentTitle
}
else if topLabel.text == String(solution) {
num1 = Double(topLabel.text!)!
solution = 0.00
topLabel.text = sender.currentTitle
// Basically stops user from adding to solution?
// Also stores solution as num1 and clears solution field
}
else {
topLabel.text = topLabel.text! + sender.currentTitle!
// Keep typing
}
if (topLabel.text?.count)! > 12 {
topLabel.text = "Error"
}
else {
return
}
// if its greater than 12 characters, display "error"
}
#IBAction func clearButtonPressed(_ sender: UIButton) {
num1 = 0.00
solution = 0.00
topLabel.text = "0"
}
#IBAction func opButtonPressed(_ sender: UIButton) {
if sender.currentTitle == "=" {
equals()
}
else {
op = sender.currentTitle!
num1 = Double(topLabel.text!)!
topLabel.text = "0"
}
// Need it to display num1 until I press another button, then I need it to only display that text.
}
func equals() {
switch op {
case "+":
add()
case "-":
subtract()
case "×":
multiply()
case "÷":
divide()
default:
print(Error.self)
}
}
func add() {
solution = num1 + Double(topLabel.text!)!
topLabel.text = String(solution)
}
func subtract() {
solution = num1 - Double(topLabel.text!)!
topLabel.text = String(solution)
}
func multiply() {
print("topLabel = ", topLabel.text!)
solution = num1 * Double(topLabel.text!)!
print("solution = ", solution)
topLabel.text = String(solution)
}
func divide() {
solution = num1 / Double(topLabel.text!)!
//answer()
}
}
Update for anyone who finds this in the future: I've solved the issue, and realized that I wasn't very clear with what I wanted it to do. I solved the problem simply by adding a condition to the if/else statement in the inButtonPressed function that detects if the topLabel is 0. By rewriting that to detect if the topLabel is 0 OR the same as num1, and then changing the else statement in the opButtonPressed function to set topLabel to String(num1), the program does exactly what I want. Here's the rewritten code:
#IBAction func intButtonPressed(_ sender: UIButton) {
if topLabel.text == "0" || topLabel.text == "0.0" || topLabel.text == String(num1){
dotPressed = false
// Resets dotPressed whenever the top is 0 or num1 and an int button is pressed
topLabel.text = sender.currentTitle
}
else if topLabel.text == String(solution) {
num1 = Double(topLabel.text!)!
solution = 0.00
topLabel.text = sender.currentTitle
// Basically stops user from adding to solution?
// Also stores solution as num1 and clears solution field
}
else {
topLabel.text = topLabel.text! + sender.currentTitle!
}
}
#IBAction func opButtonPressed(_ sender: UIButton) {
if sender.currentTitle == "=" {
equals()
}
else {
op = sender.currentTitle!
num1 = Double(topLabel.text!)!
topLabel.text = String(num1)
}
// Successfully displays num1 until I press another button, then only displays that text.
I also added a separate function to handle decimals, and I had to update the code in there as well to keep that working.

Generate consecutive and same number on Array effectively in Swift

I have an example of a case in the application to create a numeric pin pattern that should not have a consecutive number and the same number of all.
Examples of PIN patterns that are rejected are as follows:
123456,
234567,
345678,
654321,
765432,
876543,
000000 and other similar PIN patterns.
var rejectedPinList: [[Int]] = [[Int]]()
var consecutiveNumber = [0,1,2,3,4,5,6,7,8,9,0]
func incrementNumber(currentIndex: Int) -> [Int] {
var rejectedPinPattern: [Int] = [Int]()
for currentIndex in stride(from: currentIndex, to: currentIndex+6, by: 1){
rejectedPinPattern.append(consecutiveNumber[currentIndex])
}
return rejectedPinPattern
}
func decrementNumber(currentIndex: Int) -> [Int] {
var rejectedPinPattern: [Int] = [Int]()
for currentIndex in stride(from: currentIndex, to: currentIndex-6, by: -1){
rejectedPinPattern.append(consecutiveNumber[currentIndex])
}
return rejectedPinPattern
}
func constantNumber(currentIndex: Int) -> [Int] {
var rejectedPinPattern: [Int] = [Int]()
for _ in currentIndex...currentIndex+6 {
rejectedPinPattern.append(consecutiveNumber[currentIndex])
}
return rejectedPinPattern
}
for number in consecutiveNumber {
rejectedPinList.append(constantNumber(currentIndex: number))
if number < 5 {
rejectedPinList.append(incrementNumber(currentIndex: number))
} else if number > 5 {
rejectedPinList.append(decrementNumber(currentIndex: number))
} else {
rejectedPinList.append(incrementNumber(currentIndex: number))
rejectedPinList.append(decrementNumber(currentIndex: number))
}
}
func inputPin(pin: [Int]) {
if rejectedPinList.contains(pin) {
print("Pin Rejected!")
} else {
}
}
inputPin(pin: [8,7,6,5,4,3]) // Should be Rejected!
What I'm looking for is to be more effective than the algorithm code I made above in generating consecutive & same numbers. Because in my opinion, the code I made is too long and not very effective and may be wasteful. Thank you!
Instead of computing a list of all invalid pins in advance, you can verify the given pin by computing the set of all differences of adjacent digits. A pin is invalid if the set consists of -1, 0, or +1 only:
func validatePIN(_ pin: [Int]) -> Bool {
if pin.isEmpty { return false }
let diffs = Set(zip(pin, pin.dropFirst()).map(-))
return diffs.count != 1 || abs(diffs.first!) > 1
}
As the question was to improve efficiency, the approach below this implements the some initial checks before it starts looping through the array to minimise the total number of iterations/time.
func validatePin(_ pin: [Int], minLength: Int = 2 ) -> Bool {
guard pin.count >= max(minLength, 2) else {return false}
guard Set(pin).count != 1 else {return false} //all the same
guard abs(pin.first! - pin.last!) == pin.count - 1 else {return true} //can't be a sequence
let delta = pin.first! < pin.last! ? -1 : 1
for index in (0...pin.count - 2) {
if pin[index] - pin[index + 1] != delta {return true} //items not sequential
}
return false //items are sequential
}
I think this should do what you want. It checks to see if there are any consecutive digits that have an absolute difference that isn't 1. If so then the PIN may be valid (pending a check for repeated digits).
To check for repeated digits, the digits are added to an NSCountedSet. If the count for any digit is the same as the number of digits then the PIN is invalid.
func validatePIN(_ candidate: [Int]) -> Bool {
guard !candidate.isEmpty else {
return false
}
let digitSet = NSCountedSet()
var possiblyValid = false
var lastSign: Int?
for i in 0..<candidate.count {
digitSet.add(candidate[i])
if i > 0 && !possiblyValid {
let difference = candidate[i]-candidate[i-1]
let thisSign = difference.signum()
if abs(difference) != 1 {
possiblyValid = true
} else if let sign = lastSign, sign != thisSign {
possiblyValid = true
}
lastSign = thisSign
}
}
for digit in digitSet {
if digitSet.count(for: digit) == candidate.count {
return false
}
}
return possiblyValid
}
print(validatePIN([]))
print(validatePIN([8,7,6,5,3,3]))
print(validatePIN([8,7,6,5,4,3]))
print(validatePIN([2,2,2,2,2,2]))
print(validatePIN([1,2,3,4,3,2]))
gives:
false
true
false
false
true
You could also add a test for minimum length in the guard statement
Thank you all for helping me. I also improvise my algorithm. Here's my code:
func validatePIN(_ pin: [Int]) -> Bool {
if (pin.isEmpty == true) ||
(pin[0] < 5 && pin == Array(pin[0]...pin[0]+5)) ||
(pin[0] > 5 && pin == Array(stride(from: pin[0], through: pin[0]-5, by: -1)) ||
(pin.allSatisfy({ $0 == pin[0] }))) { return false }; return true
}

How not to show 0´s in label?

I have some buttons that print some text in a label when pushed. I also have a counter in front of the text to show how many times the respective button is pushed. But if some of the buttons isn´t pushed, it shows 0 in the label. Is it possible to convert the initial value on the int to blank space?
I have tried something like this:
var addCount:int = 0
var blankSpace = ""
if addCount == 0 {
addcount = blankSpace
}
else {
....
}
I have also tried:
if addCount == 0 {
addCount = String (blankSpace)
}
else {
}
and I have tried:
if addCount == 0 {
addCount = String ("")
}
I´m probably going about this all wrong, and I would appreciate help with this.
*
*
*
Edit: I thought there was some magic trick in swift to make the 0 disappear, so I took some shortcuts when I asked my question. Here is the whole setup:
I have four buttons in View Controller 1 that prints some text and adds 1 to counter. The data is based through a Swift file allSum.
The label that displays the text and counter is in View Controller 2.
allSum:
import UIKit
class allSum: NSObject {
var btn1Pressed:String = ""
var addToBtn1:Int = 0
var btn2Pressed:String = ""
var addToBtn2:Int = 0
var btn3Pressed:String = ""
var addToBtn3:Int = 0
var btn4Pressed:String = ""
var addToBtn4:Int = 0
func printToLabel() -> String{ //return a string with the current status
return ("\(addToBtn1) \(btn1Pressed + "")") + ("\(addToBtn2) \(btn2Pressed + "")") + ("\(addToBtn3) \(btn3Pressed + "")") + ("\(addToBtn4) \(btn4Pressed + "")")
}
View Controller 1
var total = allSum
var button1 = "Button 1 Pressed\n"
var button2 = "Button 2 Pressed\n"
var button3 = "Button 3 Pressed\n"
var button4 = "Button 4 Pressed\n"
#IBAction func no1(_ sender: UIButton) {
total.btn1Pressed = button1
toal.addToBtn1 += 1
}
#IBAction func no2(_ sender: UIButton) {
total.btn2Pressed = button2
toal.addToBtn2 += 1
}
#IBAction func no3(_ sender: UIButton) {
total.btn3Pressed = button3
toal.addToBtn3 += 1
}
#IBAction func no4(_ sender: UIButton) {
total.btn4Pressed = button4
toal.addToBtn4 += 1
}
View Controller 2
var total = allSum?
label.text = total.printToLabel()
With this setup, the label is showing 0 0 0 0 when no buttons are pressed.
So, my problem is that I can't set the label.text = "" since I also have other variables printed to it.
Sorry for my initial laziness
Set the actual label text as a blank space.
var addCount:Int = 0
var blankSpace = ""
if addCount == 0 {
yourlabel.text = blankSpace
} else {
yourlabel.text = "\(addCount)"
}
UPDATED:
Here's a way to write your current code with less code and fix your issue of changing 0's to blank strings
AllSum
class AllSum {
var sumsArray: [Int] = [0,0,0,0]
func stringForLabel() -> String { //return a string with the current status
var string = ""
for (index, value) in self.sumsArray.enumerated() {
if value > 0 {
string = string + "\(value) Button \(index + 1) was pressed\n"
}
}
return string
}
}
View Controller 1
You can use tags in UIBuilder, to match the indices in your array. Link up all buttons to the same function
var total = AllSum()
#IBAction func numberPressed(_ sender: UIButton) {
total.sumsArray[sender.tag] = sumsArray[sender.tag] + 1
}
View Controller 2
var total = AllSum()
label.text = total.stringForLabel()
You won't be able to assign a string, i.e. "", to a variable you've declared as an Int. In this case you'd be better off dealing with this at the time that you set the text on the label. You can use a simple if/else statement, or a ternary operator if you want a one-liner:
if addCount == 0 {
self.label.text = ""
} else {
self.label.text = "\(addCount)"
}
And for the ternary option:
self.label.text = addCount == 0 ? "" : "\(addCount)"
You're trying to set an Int to a String. Swift is a type safe language and doesn't let you do that. If you need, for some reason, to have the same variable represent both states, you can use an enum with an associated value and still use it as long as it is Describable.
Instead you can just have a computed value that returns a String that returns a blank String if the variable is zero:
func displayCount () -> String {
return addCount == 0 ? String() : String(describing:addCount)
}

UISegmentedControl and .selectedSegmentIndex wrong values?

I have a simple Segment in my code with 3 elements. For testing purposes I also do have a variable that increments based on which of the segments I press (3). The value of that variable is printed in a UITextView. This is the code:
import UIKit
class ViewController: UIViewController
{
#IBOutlet weak var segment: UISegmentedControl!
#IBOutlet weak var prwtoView: UIView!
#IBOutlet weak var prwtoText: UITextField!
var i : Int = 0
override func viewWillAppear(animated: Bool)
{
prwtoText.backgroundColor = UIColor.purpleColor()
prwtoText.textColor = UIColor.whiteColor()
segment.setTitle("Zero", forSegmentAtIndex: 0)
segment.addTarget(self, action: "action", forControlEvents: .ValueChanged)
segment.insertSegmentWithTitle("random", atIndex: 2, animated: false)
}
func action()
{
let argumentForSegment = segment.selectedSegmentIndex
if argumentForSegment == 0
{
i = 0
}
if argumentForSegment == 1
{
i += 2
}
else
{
i += Int(arc4random_uniform(100))
}
print(argumentForSegment)
prwtoText.text = "\(i)"
}
}
While I know that it starts with value -1 i don't want my app to do anything if not pressed. The thing is that even when I press the first segment (0) and it is supposed to make i = 0 it doesn't do that, although if I print argumentForSegment in my terminal it does show the 0 as value. Concluding, every time I press the zero segment (0), my i value won't become 0. Perhaps I am using the wrong method from UISegmentedControl?
edit: Got it fixed by changing the following code:
func action()
{
let argumentForSegment = segment.selectedSegmentIndex
if argumentForSegment == 0
{
i = 0
}
if argumentForSegment == 1
{
i += 2
}
else
{
i += Int(arc4random_uniform(100))
}
print(argumentForSegment)
prwtoText.text = "\(i)"
}
to:
func action()
{
let argumentForSegment = segment.selectedSegmentIndex
if argumentForSegment == 0
{
i = 0
}
if argumentForSegment == 1
{
i += 2
}
else if argumentForSegment == 2 // <==== here
{
i += Int(arc4random_uniform(100))
}
print(argumentForSegment)
prwtoText.text = "\(i)"
}
Could someone explain why it used the priority of else although the value was zero when printing argumentForSegment? In other words why when I had an else alone for the value of argumentForSegment == 0 it chose the else instead of the first statement?
Could someone explain why it used the priority of else although the
value was zero when printing argumentForSegment? In other words why
when I had an else alone for the value of argumentForSegment == 0 it
chose the else instead of the first statement?
When you have a situation where the code is not behaving as you expect, it is helpful to step through it in the debugger, or add some diagnostic print statements.
For example:
func action()
{
let argumentForSegment = segment.selectedSegmentIndex
if argumentForSegment == 0
{
print("In first block")
i = 0
}
if argumentForSegment == 1
{
print("In second block")
i += 2
}
else
{
print("In third block")
i += Int(arc4random_uniform(100))
}
print(argumentForSegment)
prwtoText.text = "\(i)"
}
If you do this, you will notice that when argumentForSegment is 0, the output will be:
In first block
In third block
So, the problem is not that it is choosing the third block over the first. The problem is that it is doing both. You want it to stop after it has detected that argumentForSegment is 0, so add an else to the second conditional statement so that it only does that when the first conditional statement failed:
func action()
{
let argumentForSegment = segment.selectedSegmentIndex
if argumentForSegment == 0
{
i = 0
}
else if argumentForSegment == 1 // added "else" here
{
i += 2
}
else
{
i += Int(arc4random_uniform(100))
}
print(argumentForSegment)
prwtoText.text = "\(i)"
}
To improve on Vacawama's answer, you can format this much easier by using a switch statement:
func action() {
let argumentForSegment = segment.selectedSegmentIndex
switch argumentForSegment {
case 0:
i = 0
case 1:
i += 1
case 2:
i += Int(arc4random_uniform(100))
default:
break
}
print(argumentForSegment)
prwtoText.text = "\(i)"
}
it's much more clean for this type of thing.
(thanks, vacawama)

Resources