Thread1: Exc_Bad_Instruction - swift

I can't figure out why am I keep getting this Thread:
Error message when running this program.
I'm new to programming and trying an exercise and keep getting this error message.
Also I was trying to figure out how to resize the pictures to fit on the screen.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var memberChoice: UISegmentedControl!
#IBOutlet weak var webView: UIWebView!
#IBAction func getMember(sender:AnyObject){
var member: String =
memberChoice.titleForSegmentAtIndex(memberChoice.selectedSegmentIndex)!
var imageURLString: String!
if member == "Daddy" {
imageURLString = "file: ///Users/natashamays/Desktop/Daddy.jpg"
}
else if member == "Tasha" {
imageURLString = "https://lh3.googleusercontent.com/-2c_GDVdcAFk/UBR7auHIHrI/AAAAAAAAEW8/gJ3F-MVUpL4/w140-h139-p/3.jpg"
}
else if member == "Jasmin" {
imageURLString = "file:///Users/natashamays/Desktop/IMG_3879.JPG"
}
var imageURL: NSURL = NSURL(string:imageURLString)!
webView.loadRequest(NSURLRequest(URL: imageURL))
}
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.
}

Related

comparing local images swift

"Optional type Bool cannot be used as a boolean; test for !nil instead"
Is the error I'm getting
I'm trying to make a "slot machine" app, very basic,
You press the UIButton, and the three images should all change, randomly, if the 3 matches, print "You won!"
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var img1: UIImageView!
#IBOutlet weak var img2: UIImageView!
#IBOutlet weak var img3: UIImageView!
#IBOutlet weak var rollBtn: UIButton!
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.
}
#IBAction func onRollPress(sender: AnyObject) {
let randomRoll = ImgArray().getRandomImage()
img1.image = randomRoll
img2.image = randomRoll
img3.image = randomRoll
if (img1.image! == img2 && img3) {
print("You won!")
}
}
}
If your images are in an array, why not get the indexes of the images and compare that. Or you could convert the images to base64 strings and compare those.
Edit: Not knowing much about your ImgArray class, this may work for you:
#IBAction func onRollPress(sender: AnyObject) {
let randomRoll1 = ImgArray().getRandomImage()
let randomRoll2 = ImgArray().getRandomImage()
let randomRoll3 = ImgArray().getRandomImage()
img1.image = randomRoll1
img2.image = randomRoll2
img3.image = randomRoll3
let imgIndex1 = ImgArray().indexOf(randomRoll1)
let imgIndex2 = ImgArray().indexOf(randomRoll2)
let imgIndex3 = ImgArray().indexOf(randomRoll3)
if (imgIndex1 == imgIndex2 && imgIndex3) {
print("You won!")
}
}

Getting boolean type errors on variables

Im trying to make a string variable if statement and I'm getting '()' is not convertible to 'BooleanType'
this is the code
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var button: UIBarButtonItem!
#IBOutlet weak var textView: UITextField!
var git = StringLiteralType()
var num = Int()
func go() {
if git = "4" {
button.enabled = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
git = self.textView.text!
button.enabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Confusion assignment operator = vs. equal to operator ==.
You need the equal to operator
if git == "4"

How to send my UITextfield to a variable via an UIButton?

I want to know how can I type something in my UITextfield and then send it to a variable via an UIButton?
When I try it with this code the app automatically crashes and show me this error:
Thread:1 signal SIGABRT
Here's the code:
import UIKit
import Foundation
class ViewController: UIViewController {
#IBOutlet weak var LabelHeure: UILabel!
#IBOutlet weak var Heure: UITextField!
#IBAction func Send(sender: AnyObject) {
var one = String(Heure.text)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
LabelHeure.text = NSDateFormatter.localizedStringFromDate(NSDate(),dateStyle:NSDateFormatterStyle.MediumStyle,timeStyle: NSDateFormatterStyle.NoStyle)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You don't need to cast Heure.text as a string because it is already a string.
Here is the code I used and it worked. After pressing the button, the variable is updated with the text from the textfield. I used a println to verify.
import UIKit
import Foundation
class ViewController: UIViewController {
#IBOutlet weak var LabelHeure: UILabel!
#IBOutlet weak var Heure: UITextField!
#IBAction func Send(sender: AnyObject) {
var myVariable = Heure.text
println(myVariable)
}
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.
}
}

Delegate Method is not called in Swift?

I want to pass a Bool value from on view controller to another without the help of segues. So i referred & got Delegates.
I have applied delegates in my App. But the Delegate method is not being called. I don't know where i am making the mistake.
So Please help me.
MainViewController
class MainViewController: UIViewController, WriteValueBackDelegate {
#IBOutlet weak var LoginButton: UIButton!
var LoggedInL :Bool?
override func viewDidLoad() {
super.viewDidLoad()
}
func writeValueBack(value: Bool) {
println("Delegate Method")
if (value == true){
LoginButton.setTitle("My Profile", forState:UIControlState.Normal)
}
}
Second View Controller
class LoginController: UIViewController {
#IBOutlet weak var LoginLabel: UILabel!
#IBOutlet weak var email: UITextField!
#IBOutlet weak var pwd: UITextField!
var LoggedInL :Bool?
var mydelegate: WriteValueBackDelegate?
override func viewDidLoad() {
super.viewDidLoad() }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func onSubmit(sender: AnyObject) {
Alamofire.request(.GET, "http://www.jive.com/index.php/capp/user_verification/\(email.text)/\(pwd.text)")
.responseJSON { (_, _, data, _) in
println(data)
let json = JSON(data!)
let name = json["first_name"].stringValue
let status = json["valid_status"].intValue
println(status)
var e = self.email.text
println(e)
self.LoginLabel.text = "Hey \(name)!"
if status == 1{
println("Correect")
self.LoggedInL = true
self.mydelegate?.writeValueBack(true)
}else {
self.LoggedInL = false
println("Error")
}
}
navigationController!.popViewControllerAnimated(true)
}
}
protocol WriteValueBackDelegate {
func writeValueBack(value: Bool)
}
you didn't initialize the delegate, and no need to, delegates are usually for async callbacks. do that instead:
class MainViewController: UIViewController {
static var sharedInstace : MainViewController?;
#IBOutlet weak var LoginButton: UIButton!
var LoggedInL :Bool?
override func viewDidLoad() {
super.viewDidLoad()
MainViewController.sharedInstace = self; //this is better from init function
}
func writeValueBack(value: Bool) {
println("Delegate Method")
if (value == true){
LoginButton.setTitle("My Profile", forState:UIControlState.Normal)
}
}
}
in login view controller
MainViewController.sharedInstance?.writeValueBack(true)
In MainViewControlleryou need a reference of the LoginController instance maybe with an IBOutlet and then set the delegate in viewDidLoad
#IBOutlet weak var loginController : LoginController!
override func viewDidLoad() {
super.viewDidLoad()
loginController.mydelegate = self
}

Changing uiLabel value on field change Swift (newbie)

Disclaimer: First iOS/ Swift app and following a tutorial.
I am trying to update the value of a uilabel based on what I enter in the uiField. In the debugger I see it entering the onEditingChanged() but the values of the label never change... No errors thrown. Also, the initial assignment to tipLabel.text = "$0.00" and totalLabel.text = "$0.00" works fine...
Any suggestions what may be wrong?
// ViewController.swift
// tips
//
// Created by Jonida Cali on 12/13/14.
// Copyright (c) 2014 Jonida Cali. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var amountField: UITextField!
#IBOutlet weak var tipLabel: UILabel!
#IBOutlet weak var totalLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tipLabel.text = "$0.00"
totalLabel.text = "$0.00"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func onEditingChanged(sender: AnyObject) {
println("changed")
var billAmount = (amountField.text as NSString).doubleValue
var tip = billAmount*0.2
var total = billAmount + tip
tipLabel.text = String(format: "$%.2f", tip)
totalLabel.text = String(format: "$%.2f",total)
}
}