Swift- Adding Two UI Numbers then Output in Label - swift

I can do this in C++/C, Objective C and Playground. I cannot get this to work in Swift when I try to make the App.
Can someone help out please?
//
// ViewController.swift
// Simple Mathemtical Functions_Swift
//
// Created by on 7/01/2015.
// Copyright (c) 2015 . All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet var labelResult: UILabel!
#IBOutlet var Number2: UILabel!
#IBOutlet var Number1: UILabel!
#IBOutlet var field1: [UITextField]!
#IBOutlet var field2: [UITextField]!
#IBAction func Calculate(sender: AnyObject) {
labelResult.text = "\()"}
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.
}
}

If field1 and field2 and just UITextFields then [UITextField]! should be changed to UITextField!. [UITextField] indicates an array of UITextFields.
#IBOutlet var labelResult: UILabel!
#IBOutlet var Number2: UILabel!
#IBOutlet var Number1: UILabel!
#IBOutlet var field1: UITextField!
#IBOutlet var field2: UITextField!
#IBAction func Calculate(sender: AnyObject) {
var a = (field1.text as NSString).floatValue
var b = (field2.text as NSString).floatValue
var sum = a + b
labelResult.text = "\(sum)"
}

Maybe you're having trouble because field1.text is a String? not an NSString.
#IBAction func Calculate(sender: AnyObject) {
labelResult.text = "\((field1.text as NSString).floatValue + (field2.text as NSString).floatValue)"
}

Related

How to Store NSPopUpButton TitleOfSelectedItem to Core Data Model Field?

I have created the following view controller and wired up the controls in my storyboard to it.
import Foundation
import CoreData
import AppKit
protocol SubmissionViewControllerDelegate {
func controller(_controller: SubmissionViewController,
didAddSubmissionWithID profileID: Int64)
}
class SubmissionViewController : ViewController {
#IBOutlet var profileIDComboBox: NSPopUpButton!
var profileSelectedValue: String!
#IBOutlet var transcriptTypeComboBox: NSPopUpButton!
var transcriptTypeSelectValue: String!
#IBOutlet var submissionTypeComboBox: NSPopUpButton!
var submissionTypeSelectValue: String!
#IBOutlet var transcriptionTurnaroundComboBox: NSPopUpButton!
var turnaroundSelectValue: String!
#IBOutlet var persistentNoteComboBox: NSPopUpButton!
var persistentNoteSelectValue: String!
#IBOutlet var notesTextField: NSTextField!
#IBOutlet var batchIdentifierTextField: NSTextField!
#IBOutlet var languageInAudioComboBox: NSPopUpButton!
var languageSelectValue: String!
#IBOutlet var addResultViewButton: NSButton!
#IBOutlet var submissionFiles: NSTableView!
var delegate: SubmissionViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func save(_ sender: Any) {
guard case profileIDComboBox.titleOfSelectedItem =
profileSelectedValue else { return }
guard case transcriptTypeComboBox.titleOfSelectedItem =
transcriptTypeSelectValue else { return }
guard let delegate = delegate else { return }
delegate.controller(self, didAddSubmissionWithID:
profileSelectedValue)
}
}
I now want to store the titleOfSelectedItem from each NSPopUpButton in my Core Data Submission model. However, when I set the variables to be strings, I receive the error as follows:
Cannot convert value of type 'String?' to expected argument type '_OptionalNilComparisonType'
How do I accomplish this task?

Cannot convert value of type 'String?' to type 'NSString' in coercion

Im trying to make this calculator for various math formulas and I'm stuck at this point. I was following this tutorial
Here's my code:
import UIKit
class pythagorasViewController: UIViewController {
#IBOutlet weak var aLabel: UILabel!
#IBOutlet weak var bLabel: UILabel!
#IBOutlet weak var aField: UITextField!
#IBOutlet weak var bField: UITextField!
#IBOutlet weak var answerLabel: UILabel!
#IBAction func calculateButton(_ sender: UIButton) {
var a = (aField.text as NSString).floatValue
var b = (bField.text as NSString).floatValue
var answer = sqrt(a*a + b*b)
answerLabel.text = "\(answer)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The part where I'm getting the error is at:
var a = (aField.text as NSString).floatValue
var b = (bField.text as NSString).floatValue
Prefer let to var when possible. You do not need to use NSString. You can cast String to Float?. You need to unwrap both the text property which is a String? (if you have a question about the type of a variable option click and it will show you) and the Float? conversion:
func calculateButton(_ sender: UIButton) {
guard let aText = aField.text,
let bText = bField.text,
let a = Float(aText),
let b = Float(bText) else {
return
}
let answer = sqrt(a*a + b*b)
answerLabel.text = "\(answer)"
}

Can't make sens of UIStepper (S)

I'm trying to make an app to count different tokens in a board game. I thought it would be easy peasy, but not quite.
I succeeded in adding one UIStepper to count one kind of token, but I don't know how to add the other four. This is what I've got so far:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var theLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func StepperTapped(sender: UIStepper) {
self.theLabel.text = "\(self.stepper.value)"
}
}
I got one action and two outlets.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var theLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBOutlet weak var theLabel2: UILabel!
#IBOutlet weak var stepper2: UIStepper!
#IBOutlet weak var theLabel3: UILabel!
#IBOutlet weak var stepper3: UIStepper!
#IBOutlet weak var theLabel4: UILabel!
#IBOutlet weak var stepper4: UIStepper!
#IBOutlet weak var theLabel5: UILabel!
#IBOutlet weak var stepper5: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func StepperTapped(sender: UIStepper) {
self.theLabel.text = "\(self.stepper.value)"
}
#IBAction func StepperTapped2(sender: UIStepper) {
self.theLabel2.text = "\(self.stepper2.value)"
}
#IBAction func StepperTapped3(sender: UIStepper) {
self.theLabel3.text = "\(self.stepper3.value)"
}
#IBAction func StepperTapped4(sender: UIStepper) {
self.theLabel4.text = "\(self.stepper4.value)"
}
#IBAction func StepperTapped5(sender: UIStepper) {
self.theLabel5.text = "\(self.stepper5.value)"
}
}

Resign First Responder

I was wondering how I could get rid of my search bar whenever I switch to my detailView, so I don't have to keep pressing cancel on the Search Bar whenever I switch to the detailView.
Here is my detailView Code.
import UIKit
class DetailViewLemon: UIViewController {
var sentData1:String!
#IBOutlet weak var DetailCarMake: UILabel!
#IBOutlet weak var Zacks: UILabel!
#IBOutlet weak var TheStreet: UILabel!
#IBOutlet weak var MarketWatch: UILabel!
#IBOutlet weak var StockMaster: UILabel!
#IBOutlet weak var Nasdaq: UILabel!
#IBOutlet weak var InvestorPlace: UILabel!
#IBOutlet weak var Benzinga: UILabel!
#IBOutlet weak var Rating: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
DetailCarMake.text = sentData1
self.title = sentData1
if self.title == "Apple" {
Zacks.text = "Zacks Rank: Buy"
TheStreet.text = "The Street: Buy"
MarketWatch.text = "MarketWatch: Buy"
StockMaster.text = "StockMaster: Buy"
Nasdaq.text = "Nasdaq Rating: Buy"
InvestorPlace.text = "InvestorPlace Rating: Buy"
Benzinga.text = "Benzinga Rating: Buy"
Rating.text = "Buy"
}
if self.title == "Google" {
Zacks.text = "Goog Zacks Rank: Buy"
TheStreet.text = "Goog The Street: Buy"
MarketWatch.text = "MarketWatch: Buy"
StockMaster.text = "StockMaster: Hold"
Nasdaq.text = "Nasdaq Rating: Buy"
InvestorPlace.text = "InvestorPlace Rating: Hold"
Benzinga.text = "Benzinga Rating: Buy"
Rating.text = "Buy"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
In prepareForSegue of your TableViewController you should call self.searchController.searchBar.resignFirstResponder() (replacing names of variables with whatever they actually are, as you didn't show that code.
You could also use "view.endEditing(true)" in your viewWillDisappear
func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
view.endEditing(true)
}
It closes keyboard regardless of whichever input is being FirstResponder

How can I add several UIStepper Values to one Label?

I'm working right now on my first Swift project. I've got 2 stepper and one label - both stepper are sending their values to it. How can I add the value of the second stepper to the label, in which the value of the first stepper is already? Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
stepper.wraps = true
stepper.autorepeat = true
stepper.maximumValue = 10000
stepper2.wraps = true
stepper2.autorepeat = true
stepper2.maximumValue = 10000
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var valueLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBAction func stepperValueChanged(sender: UIStepper) {
valueLabel.text = Int(sender.value).description
}
#IBOutlet weak var stepper2: UIStepper!
#IBAction func stepper2ValueChanged(sender: UIStepper) {
valueLabel.text = Int(sender.value).description
}
}
Thank you!
If you want to combine the two values to ONE String and show this String on your Label, than you have to create a new function that does this for you. I added such a function to your code:`
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
stepper.wraps = true
stepper.autorepeat = true
stepper.maximumValue = 10000
stepper2.wraps = true
stepper2.autorepeat = true
stepper2.maximumValue = 10000
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var valueLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBAction func stepperValueChanged(sender: UIStepper) {
// valueLabel.text = Int(sender.value).description
addValuesToASumAndPutItIntoTheLabel()
}
#IBOutlet weak var stepper2: UIStepper!
#IBAction func stepper2ValueChanged(sender: UIStepper) {
// valueLabel.text = String(sender.value)
addValuesToASumAndPutItIntoTheLabel()
}
func addValuesToASumAndPutItIntoTheLabel() {
let summe : Int = Int(stepper.value + stepper2.value)
valueLabel.text = summe.description
}
}`