shuffle order of labels swift / Xcode - swift

I'm working on a quiz app and I have 3 labels who will show the answers. How can I shuffle the order of the labels on the screen to avoid having the right answer always at the same place?
I have all my answers stored in an array and I'm displaying them like that
#IBOutlet weak var firstAnswer: UILabel!
#IBOutlet weak var secondAnswer: UILabel!
#IBOutlet weak var thirdAnswer: UILabel!

There is no need to shuffle your labels. If you have your possible answers in let's say an array like that:
var answers = ["answer1", "answer2", "answer3"]
You can shuffle the elements of the array by calling function shuffle() of Array, before you set the text properies of your UILabels:
answers.shuffle()

let randomInt = Int.random(in: 1..<30)
and have a logic like
if the randomint %% 2 == 0 { firstAnswer.text = right_answer}
else if randomint %% 3 == 0 { secondAnswer.text = right_answer}
else { thirdAnswer.text = right_answer}

Related

Swift Timer Loop doesn't trigger but view and controller are connected

I'm trying to print the title "⚡️FlashChat" one letter at a time.
I'm coding along with a video and I've checked my code against the instructor's and it matches.
Right now, the title prints in its entirety when the sim is built.
I've adjusted the withTimeInterval to see fit that was set too quickly.
If I remove the loop and just execute the titleLabel.text = "", the title disappears, so it is connected correctly.
Thanks in advance!
import UIKit
class WelcomeViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = ""
var charIndex = 0.0
let titleText = "⚡FlashChat"
for letter in titleText {
Timer.scheduledTimer(withTimeInterval: 7.0
* charIndex, repeats: false) {(timer)
in
self.titleLabel.text?.append(letter)}
}
charIndex += 1
}
}
You're incrementing charIndex outside your for loop. So interval will always be equal to 0

Swift refactoring assistance

I have a meter laid out on a storyboard that is comprised of ten segments. There are an additional ten segments on top of the original segments that are colored to simulate a lighted segment - these are outlets. (See image below.) Currently I am using a switch statement to hide and unhide each outlet/segment based on a constantly varying input level. But, as you can see in the code below, it's not pretty. I keep reading that polymorphism is often the way to improve a switch statement, but I can't see how that would help here.
switch input {
case 0...9:
seg1.hidden = false
seg2.hidden = true
seg3.hidden = true
seg4.hidden = true
seg5.hidden = true
seg6.hidden = true
seg7.hidden = true
seg8.hidden = true
seg9.hidden = true
seg10.hidden = true
case 10...19:
seg1.hidden = false
seg2.hidden = false
seg3.hidden = true
seg4.hidden = true
seg5.hidden = true
seg6.hidden = true
seg7.hidden = true
seg8.hidden = true
seg9.hidden = true
seg10.hidden = true
...
and on and on for eight more levels.
//You can have these segments in an array like this-
let segments = [seg1, seg2, seg3, seg4, seg5,seg6, seg7, seg8, seg9, seg10]
// Function that will setup the segments based on input
func setUpSegmentsForInputValue(segments:[UIView], value:Int) {
for (index, segment) in segments.enumerate() {
segment.hidden = (value/10) != index
}
}
// Call the function with whatever input values
setUpSegmentsForInputValue(segments: segments, value: someValue)
You could add all of your outlets to an array. Map the input to an "index range" and then decide in a for loop if outlet is in "index range". Then set hidden property accordingly.
#IBOutlet weak var seg1 = UIView()
#IBOutlet weak var seg2 = UIView()
#IBOutlet weak var seg3 = UIView()
#IBOutlet weak var seg4 = UIView()
...
let segs = [seg1, seg2, seg3, seg4]
let input = 19
let range = 0 ... input / 10
for (index, seg) in segs.enumerate() {
if range ~= index {
seg.hidden = false
}else {
seg.hidden = true
}
}
Alternative (instead of the for loop):
#IBOutlet weak var seg1 = UIView()
#IBOutlet weak var seg2 = UIView()
#IBOutlet weak var seg3 = UIView()
#IBOutlet weak var seg4 = UIView()
...
let segs = [seg1, seg2, seg3, seg4]
let input = 19
let range = 0 ... input / 10
segs.enumerate().forEach { index, seg in
seg.hidden = !(range ~= index)
}
Hope this helps.

When using < operator swift goes over specified number before exiting

I am currently learning swift/iOS programing. When I use a < operator and do something like: x < y, x will go past y before exiting. Is there a way around this so it stops as it hits y rather than going over?
Any and all help is greatly appreciated!
Thanks!
So when I press my AddBtnPressed button it adds firstNum and secondNum storing it in sumNum. Say firstNum = 995, secondNum = 15, when I hit my button it will hit 1010 instead of exiting and then if I hit my button again after that it will then exit after its already gone over.
Hope this makes sense!
The user inputs a number hits play which takes you to a second screen that says "press add to add" you press the Add button and it reveals an equation of 0 + (user input number) = x. and every time you press the Add button it adds the user number to the previous sum until it hits 999 then its supposed to take you back to the main screen.
import UIKit
class ViewController: UIViewController {
var firstNum = 0
var secondNum = 0
var sumNum = 0
var maxNum = 900
// First Screen
#IBOutlet weak var logo: UIImageView!
#IBOutlet weak var userNum: UITextField!
#IBOutlet weak var play: UIButton!
// Second Screen
#IBOutlet weak var pressAddToAdd: UILabel!
#IBOutlet weak var AddBtn: UIButton!
// Thrird Screen
#IBOutlet weak var ContSum: UILabel!
#IBOutlet weak var addUserNum: UILabel!
#IBOutlet weak var sum: UILabel!
#IBOutlet weak var mathSymbols: UILabel!
#IBAction func playBtnPressed(sender: UIButton) {
if userNum.text != nil && userNum.text != "" {
userNum.hidden = true
logo.hidden = true
play.hidden = true
pressAddToAdd.hidden = false
AddBtn.hidden = false
}
}
#IBAction func AddBtnPressed(sender: UIButton) {
if sumNum <= maxNum {
// Hide 2nd screen
pressAddToAdd.hidden = true
// Reveal 3rd screen
ContSum.hidden = false
addUserNum.hidden = false
sum.hidden = false
mathSymbols.hidden = false
// Variables
firstNum = sumNum
secondNum = Int (userNum.text!)!
sumNum = firstNum + secondNum
// Imput Variables into textFields
addUserNum.text = "\(secondNum)"
sum.text = "\(sumNum)"
ContSum.text = "\(sumNum - secondNum)"
} else {
// Restart
firstNum = 0
secondNum = 0
sumNum = 0
ContSum.hidden = true
addUserNum.hidden = true
sum.hidden = true
mathSymbols.hidden = true
AddBtn.hidden = true
userNum.hidden = false
logo.hidden = false
play.hidden = false
}
}
Evaluation your total outside of your conditional
sumNum += Int(userNum.text!)!
if sumNum <= 999 {
...
}
operator < is nothing but less than. less than can be true or false. expression x < y is evaluated as value of type Bool
// "do something like: x < y"
2 < 3 // true
"a" < "b" // true
var x = 0
var y = 1
x++ < y // true
print(x, y) // 1 1
--x < y // true
print(x, y) // 0 1

How can I add gravity to UIButton SWIFT

So I am trying to add gravity to 4 UIButtons but they are not falling, I added this code to the view did load, I don't why this isn't working well.
Here is the code:
//initialize the animator
var animator = UIDynamicAnimator(referenceView: self.view)
//add gravity
let gravity = UIGravityBehavior(items: [redButton!, greenButton!, blueButton!, cameraButton!])
let direction = CGVectorMake(0.0, 1.0)
gravity.gravityDirection = direction
animator.addBehavior(gravity)
My buttons are the redButton, greenButton, blueButton, and the cameraButton, also I have applied the direction of the fall but when I run the app they are just static. So is it possible to add gravity to the UIButtons?
You are declaring your animator locally to the function. When the function returns, the animator is released from memory, and all animation stops (actually viewDidLoad() returns so early in the lifecycle that animation never gets started).
Declare it as:
var animator: UIDynamicAnimator!
at class scope
Class scope means put it within the curly braces of the class:
class Foo {
var thisVariableIsAtClassScopeAndPersistsAcrossMethods: Int
func bar() {
var thisVariableIsAtLocalScopeAndDisappearsWhenFunctionReturns: String
}
}
And only initialize it in viewDidLoad():
animator = UIDynamicAnimator(referenceView: self.view)
(You cannot initialize it at class scope without lazy trickiness, because self is not yet available.)
** EDIT FINAL ANSWER **
After chat, we resolved the misunderstood answer and now we have:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var redButton: UIButton!
#IBOutlet weak var greenButton: UIButton!
#IBOutlet weak var blueButton: UIButton!
#IBOutlet weak var cameraButton: UIButton!
// Declare the animator at class scope so it doesn't get released prematurely.
var animator: UIDynamicAnimator!
override func viewDidLoad() {
super.viewDidLoad()
//add gravity
let gravity = UIGravityBehavior(items: [redButton, greenButton, blueButton, cameraButton])
let direction = CGVectorMake(0.0, 1.0)
gravity.gravityDirection = direction
animator.addBehavior(gravity)
// other viewDidLoad setup code to set attributes of buttons
}
}

Tic tac toe finding win?

I am making a tic tac toe game in Swift for 2 players. Now I know I can find a winner by making outlets for the 9 buttons and then writing for all the 3 in row cases, but I was wondering if there is a better way to do this? And is there a better way to write my switch statement?
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
#IBOutlet weak var button3: UIButton!
#IBOutlet weak var button4: UIButton!
#IBOutlet weak var button5: UIButton!
#IBOutlet weak var button6: UIButton!
#IBOutlet weak var button7: UIButton!
#IBOutlet weak var button8: UIButton!
#IBOutlet weak var button9: UIButton!
var player = 1
#IBAction func playersTurn(sender: UIButton){
switch player{
case 1: sender.setTitle("X", forState: UIControlState.Normal); player = 2;break;
case 2: sender.setTitle("O", forState: UIControlState.Normal); player = 1;break;
default:exit(0); break
}
}
I would set the tag property on your buttons using values 0-9 in order from top to bottom, left to right, like so:
[ 0 ][ 1 ][ 2 ]
[ 3 ][ 4 ][ 5 ]
[ 6 ][ 7 ][ 8 ]
Then create a model to represent each square and give it a corresponding index value, as well as a reference to the player who owns it. Then you can handle you game logic with those models and update the UI by getting references to your buttons with viewForTag(_:). Here's my version:
class Square {
var owningPlayer: Player?
let index: Int
init( index: Int ) {
self.index = index
}
}
class Player {
let symbol: String
init( symbol: String ) {
self.symbol = symbol
}
}
class ViewController: UIViewController {
var squares = [Square]()
var players = [Player]()
var currentPlayer: Player?
override func viewDidLoad() {
super.viewDidLoad()
self.players = [ Player(symbol: "X"), Player(symbol: "O") ]
self.currentPlayer = self.players[0]
// Create squares
for i in 0..<9 {
self.squares.append( Square(index: i) )
}
}
#IBAction func buttonPressed( button: UIButton ) {
guard let player = self.currentPlayer else {
fatalError( "Don't have a current player!" )
}
if let square = self.squares.filter({ $0.index == button.tag }).first {
// Update model
square.owningPlayer = player
// Update UI
button.setTitle( player.symbol, forState: .Normal )
}
if self.checkWin() {
print( "\(player.symbol) wins!" )
}
else {
self.currentPlayer = self.players.filter({ $0 !== player }).first
}
}
func checkWin() -> Bool {
guard let player = self.currentPlayer else {
fatalError( "Don't have a current player!" )
}
let winningSequences = [
// Horizontal rows
[ 0, 1, 2 ],
[ 3, 4, 5 ],
[ 6, 7, 8 ],
// Diagonals
[ 0, 4, 8 ],
[ 2, 4, 6 ],
// Vertical rows
[ 0, 3, 6 ],
[ 1, 4, 7 ],
[ 2, 5, 8 ],
]
// Get indexes owned by this player
let selectedIndexes = self.squares.filter({ $0.owningPlayer === player }).map { $0.index }
// Change the sequence arrays into sets for accurate comparison using `contains(_:)`
if winningSequences.map({ Set<Int>($0) }).contains( Set<Int>(selectedIndexes) ) {
return true
}
return false
}
}
Although #Patrick Lynch's answer is excellent I found that it didn't match a win should the game go beyond 3 moves.
This is because he is only checking if any of the winningSequences contains a current move sequence (selectedIndexes). As the winningSequences map only has a max of 3 items it'll never contain a set of 4 moves.
To resolve this I changed contains to subset. i.e is a winningSequence (0, 1, 2) a subset of the selectedIndexes (0, 5, 1, 2)
Replace
if winningSequences.map({ Set<Int>($0) }).contains( Set<Int>(selectedIndexes) ) {
return true
}
with
for seq in winningSequences.map( { Set<Int>($0)}) {
if seq.isSubset(of: selectedIndexes) {
return true
}
}
I'm still new to swift so if I'm wrong or anyone has a better idea then I'd be happy to hear it.