Swift Segmented Control - swift

I made a segmented control in swift that changes a boolean to either true or false; However, every time I select "selectedSegmentedIndex == 1" in the application, I get error "Thread 1: signal SIGABERT"
My code goes as flows:
#IBOutlet weak var translationType: UISegmentedControl!
var state = true
#IBAction func translation(_ sender: Any)
{
if translationType.selectedSegmentIndex == 0
{
state = ture
}
else if translationType.selectedSegmentIndex == 1
{
state = false
}
}
Any information would be greatly appreciated. Thanks.

At least using the sender parameter and the static type avoids the crash if translationType is not connected – which is most likely the case.
#IBAction func translation(_ sender: UISegmentedControl)
{
if sender.selectedSegmentIndex == 0
{
state = true
}
else if sender.selectedSegmentIndex == 1
{
state = false
}
}
or a bit shorter
#IBAction func translation(_ sender: UISegmentedControl)
{
state = sender.selectedSegmentIndex == 0
}

var state = true
#IBOutlet weak var translationType: UISegmentedControl!
#IBAction func translation(_ sender: UISegmentedControl)
{
if translationType.selectedSegmentIndex == 0
{
state = true
}
else if translationType.selectedSegmentIndex == 1
{
state = false
}
print(state)
print(translationType.selectedSegmentIndex)
}

make sure your outlet is connected! see connection inspector

Related

How do you create a simple login screen in Xcode?

I am making an app that uses a login screen I made. The input is this Text Field that you type in your username. How do I make it respond to different usernames? For example the username is UK. How do I make it respond to if it is or is not 'UK'? I am a complete beginner to Xcode so could you please walk me through the steps please?
I tried using this if(Username!.text, isEqual: "Hello") that I saw from a video, but an error pops up saying:
'(String?, isEqual: String)' is not convertible to 'Bool'.
I have no idea what it means and how to get around it.
import UIKit
class ViewController: UIViewController {
#IBOutlet var ftest: UILabel!
#IBOutlet var Username: UITextField?
#IBOutlet var Password: UITextField!
#IBAction func SignIn(_ sender: Any) {
if(Username!.text, isEqual: "Hello")
}
//this code above is the code I tried to do based on a video and it did not work.
override func viewDidLoad() {
super.viewDidLoad()
}
}
I want it to print "Hi" or something. I just want it to react to:
If the username is 'UK' and
If the username is not 'UK'.
You can just simply use == as follow
#IBAction func SignIn(_ sender: Any) {
if(Username!.text == "Hello" || Username!.text == "UK") {
print("Hi")
}
}
|| means OR
&& means AND
Hope this help!
You can do it in this way
#IBAction func SignIn(_ sender: Any) {
if Username!.text == "UK"
{
print("Hi")
}
else
{
// do your stuff
}
}
Moreover you can also use a variable to check whether a string contained in your textfield or not like this:
var Str = "UK"
if Str.contains(self.Username!.text!) {
print("Hi")
}
Extension way:
extension String {
func contains(find: String) -> Bool{
return self.range(of: find) != nil
}
}
var value = self.Username!.text // You can put value as of your textfield.text and then check
print(value.contains("UK")) // true
print(value.contains("THIS")) // false
if value.contains("UK") == true
{
print("Hi")
}
else
{
// do your stuff
}

Unresolved Identifier 'count'

Here is the error that I am seeing.
The "cardButton" is responsible for showing the next question. This is a small card app game that I am trying to make and as you can see from the image this is where I am having the issue with the code.
Here is the code:
import UIKit
class MultipleViewController: UIViewController {
#IBOutlet weak var questionLabel2: UILabel!
#IBOutlet var answerButtons: [UIButton]!
#IBOutlet weak var cardButton: UIButton!
#IBAction func cardButtonHandle(_ sender: Any) {
cardButton.isEnabled = true
if questionIdx < count(mcArray) - 1 { // There are still more questions
questionIdx += 1 //
} else {
questionIdx = 0
}
nextQuestion()
}
#IBAction func answerButtonHandle(_ sender: UIButton) {
if sender.titleLabel?.text == correctAnswer{
sender.backgroundColor = UIColor.green
print("Correct!")
} else {
sender.backgroundColor = UIColor.red
print("Wrong Answer")
}
for button in answerButtons{
button.isEnabled = false
if button.titleLabel?.text == correctAnswer {
button.backgroundColor = UIColor.green
}
}
cardButton.isEnabled = true // next question
}
var correctAnswer: String? // correct answers
var answers = [String]() // answers
var question : String? // Questions
var questionIdx = 0 // not sure what this is ?
override func viewDidLoad() {
super.viewDidLoad()
// titleForButtons() // call buttons as soon its loaded..
cardButton.isEnabled = false
nextQuestion()
}
func nextQuestion (){
let currentQuestion = mcArray![questionIdx]
answers = currentQuestion["Answers"] as! [String]
correctAnswer = currentQuestion["CorrectAnswer"] as? String
question = currentQuestion["Question"] as? String
titleForButtons ()
}
func titleForButtons (){
for (idx,button) in answerButtons .enumerated() {
button.titleLabel?.lineBreakMode = .byWordWrapping
button.setTitle(answers[idx],for:.normal)
button.isEnabled = true
}
questionLabel2.text = question
}
}
The following should work, you did not have the correct syntax for the length of the array. Note that if you have not initialized your questions array, this would cause a crash. Therefore you might want to add a guard into your code. Perhaps use the following
#IBAction func cardButtonHandle(_ sender: Any) {
cardButton.isEnabled = true
if questionIdx < (mcArray!.count) - 1 { // There are still more questions
questionIdx += 1 //
} else {
questionIdx = 0
}
nextQuestion()
}

Swift error when I press button more times than there are pizzas in my array list

So I just started programming and I am now getting this error.
It occurs every time I press the button more times than there are Pizzas in the list.
Full error code: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0):
Here`s my code so far:
import UIKit
var pizzaNumber = 0
var pizzaNames = ["Skinke Pizza", "Salat Pizza", "Pepperoni Pizza"]
let priser = [65,70,65]
var totalProdukt = pizzaNames.count
class ViewController: UIViewController {
#IBOutlet weak var produktNavn: UILabel!
#IBAction func rightButton(_ sender: UIButton) {
pizzaNumber+=1
showPizza()
}
#IBAction func leftButton(_ sender: UIButton) {
pizzaNumber-=1
if pizzaNumber < 0 {
pizzaNumber = 0
}
showPizza()
}
func showPizza() {
if pizzaNumber > totalProdukt {
pizzaNumber = pizzaNames.count
} else {
self.produktNavn.text = pizzaNames[pizzaNumber]
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You should make sure that pizzaNumber-1 can never be bigger than the number of elements in your array to ensure that you are never trying to access an index that doesn't exist. This can be easily done by changing totalProdukt to be a computed variable. This way, the value of the variable will always be updated when you are trying to access it.
var totalProdukt: Int {
return pizzaNames.count
}
Also bear in mind that array indexing starts at 0, so you need
if pizzaNumber >= totalProdukt {
pizzaNumber = pizzaNames.count-1
} else {
self.produktNavn.text = pizzaNames[pizzaNumber]
}
Bear in mind that with your current code, there's no need for storing the count of the array in a separate variable, since you are only using it at one place in code.
Moreover, the cleanest solution is to check the value before actually increasing it rather than when using it, this way in showPizzas you don't need to do any checks, just update the label:
class ViewController: UIViewController {
#IBOutlet weak var produktNavn: UILabel!
#IBAction func rightButton(_ sender: UIButton) {
if pizzaNumber < pizzas.count-1 {
pizzaNumber+=1
}
showPizza()
}
#IBAction func leftButton(_ sender: UIButton) {
if pizzaNumber > 0 {
pizzaNumber-=1
}
showPizza()
}
func showPizza() {
self.produktNavn.text = pizzaNames[pizzaNumber]
}
}
Here's how to fix your error
func showPizza() {
if pizzaNumber >= totalProdukt { // << Added '=' since pizzaNames[pizzaNames.count] is out of bounds
pizzaNumber = pizzaNames.count - 1
} else {
self.produktNavn.text = pizzaNames[pizzaNumber]
}
}

How can I remove repetitive code in swift?

I have 2 sections and 6 lines of code that do the opposite things for an app that I have made for a project. The code was typed as it was taught to us, but in the project review I was told to get rid of the repetitive/like code. Can anyone help point me in the right direction with this? Thanks
#IBAction func stopRecording(_ sender: AnyObject) {
recordButton.isEnabled = true
stopRecordingButton.isEnabled = false
recordingLabel.text = "Tap to Record"
}
#IBAction func recordAudio(_ sender: AnyObject)
{
recordingLabel.text = "Recording in Progress"
stopRecordingButton.isEnabled = true
recordButton.isEnabled = false
}
You can also do this by didSet Observer.
var isRecording: Bool = false {
didSet {
recordButton.isEnabled = !isRecording
stopRecordingButton.isEnabled = isRecording
recordingLabel.text = isRecording ? "Recording in progress" : "Tap to Record"
}
}
And the actions become pretty simple like this.
#IBAction func stopRecording(_ sender: AnyObject) {
isRecording = false
}
#IBAction func recordAudio(_ sender: AnyObject) {
isRecording = true
}

If (Bools = true) not executing -Swift

Attempting to execute a modal segue when multiple different Bool variables are all true (activated true through IBAction button push), however, nothing is happening- here is how they are all setup-
UIViewController {
// INITIAL TO CHECK WHICH BUTTONS HAVE BEEN PUSHED //
var 1Check = Bool()
// Checks //
#IBAction func 1(_ sender: AnyObject) {
1Check = true
}
and here is the execution-
viewDidLoad() {
super.viewDidLoad()
MoveOn()
}
func MoveOn(){
if (1Check == true && 2Check == true ...) {
self.performSegue(withIdentifier: "NewScreen", sender: nil)
}
}
what am I missing? Thanks!
The call to MoveOn() needs to be in a place where it will be called every time one of those checked values changes:
UIViewController {
// INITIAL TO CHECK WHICH BUTTONS HAVE BEEN PUSHED //
var 1Check = Bool()
// Checks //
#IBAction func 1(_ sender: AnyObject) {
1Check = true
MoveOn()
}
viewDidLoad() {
super.viewDidLoad()
}
func MoveOn(){
if (1Check == true && 2Check == true ...) {
self.performSegue(withIdentifier: "NewScreen", sender: nil)
}
}
}