OS X Hello World Swift tutorial - swift

So I'm following this tutorial for Swift and am not getting the desired result at the end. This is my code for the ViewController:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var nameField: NSTextField!
#IBOutlet weak var helloLabel: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
#IBAction func sayButtonClicked(_ sender: Any) {
var name = nameField.stringValue
if name.isEmpty {
name = "World"
}
let greeting = "Hello \(name)!"
helloLabel.stringValue = greeting
}
}
And the following is the result:
When I debug the program however it does show the correct variables:
What am I doing wrong here?

Turns out I didn't stretch out the the Label field enough.

Related

Passing boolean from one View controller to another, both controlled by a Tab Controller

I've been looking at other questions/answers around this and I can't seem to find an answer thats worked for me. I'm making a game where you click a pizza and you generate money. You start off with only being able to generate one dollar through clicking but as you purchase Upgrades this number gains in value. In theory, when you click the level 1 upgrade button it will send a true value to the main game view, making a click earn 5 dollars instead of 1.
import UIKit
class GameViewController: UIViewController {
var wallet: Int = 0
var totalIncome: Int = 0
var level1UpgradeUsed: Bool = false
#IBOutlet weak var pizzaButton: UIButton!
#IBOutlet weak var labelWallet: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
print(level1UpgradeUsed)
// Do any additional setup after loading the view.
}
#IBAction func pizzaClick(_ sender: Any) {
let upgrades = UpgradesViewController()
let level1UpgradeUsed = upgrades.level1used
if level1UpgradeUsed == true {
wallet = wallet + 4
}
wallet = wallet + 1
labelWallet.text = ("$" + String(wallet))
}
In the upgrades controller I'm trying to send the true value once the button is clicked
import UIKit
class UpgradesViewController: UIViewController {
public var level1used: Bool = false
public var level2used: Bool = false
public var level3used: Bool = false
public var level4used: Bool = false
var level5used: Bool = false
#IBOutlet weak var buttonLevel1: UIButton!
#IBOutlet weak var buttonLevel2: UIButton!
#IBOutlet weak var buttonLevel3: UIButton!
#IBOutlet weak var buttonLevel4: UIButton!
#IBOutlet weak var buttonLevel5: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let gameViewController = segue.destination as? GameViewController {
gameViewController.level1UpgradeUsed = self.level1used
}
}
#IBAction func Level1Click(_ sender: Any) {
if level1used == false{
buttonLevel1.backgroundColor = UIColor.gray
level1used = true
}
else {
}
}
Im geussing I'm not utilizing the TabBarController to pass data and not sure at all how to go about doing that. I'm new to swift and teaching myself everything so I'm sorry if this seems like a redundant question. It feels like it should be a lot simpler then it looks.

Swift Combine question with UILabel subscribe

import UIKit
import Combine
class ViewController: UIViewController {
#IBOutlet weak var allowMessageSwitch: UISwitch!
#IBOutlet weak var sendButton: UIButton!
#IBOutlet weak var messageLabel: UILabel!
#Published var canSendMessages: Bool = false
#Published var newMsg: String = ""
private var switchSubscriber: AnyCancellable?
private var btnSubscriber: AnyCancellable?
override func viewDidLoad() {
allowMessageSwitch.isOn = false
super.viewDidLoad()
setupProcesscingChain()
}
func setupProcesscingChain() {
switchSubscriber = $canSendMessages.receive(on: DispatchQueue.main).assign(to: \.isEnabled, on: sendButton)
btnSubscriber = $newMsg.receive(on: DispatchQueue.main).assign(to: \.text, on: messageLabel)
}
#IBAction func didSwitch (_ sender: UISwitch) {
canSendMessages = sender.isOn
}
#IBAction func sendMessage( _ sender: Any) {
}
}
I am getting error in
btnSubscriber = $newMsg.receive(on: DispatchQueue.main).assign(to: \.text, on: messageLabel)
error msg is
Type of expression is ambiguous without more context
I dont understand why label does not work as Switcher (bool)
I assume it is because \.isEnabled is not optional, and \.text is optional..??
how can I make this work with the same format. this is for practice and to understand how Combine works.. please help!
lableSubscriber = $newMsg.receive(on: DispatchQueue.main).assign(to: \.text!, on: messageLabel)
I solved it on my own! it was very simple.
just force unwrap the keyPath.

Error : Consecutive statements on a line must be separated by ';' What's wrong?

import UIKit
class ViewController: UIViewController {
//Place your instance variables here
let questions = Questionbank()
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet var progressBar: UIView!
#IBOutlet weak var progressLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let firstQuestion = questions.list[0]
questionLabel.text = firstQuestion.questionText
}
#IBAction func answerPressed(_ sender: AnyObject) {
}
func updateUI() {
}
func nextQuestion() {
}
func checkAnswer() {
}
func startOver() {
}
}
This is my code above. And following is the error come up.
This is the errors come up.
There seems to be problem with questionLabel.text line but
I can't figure it out. This is a part of ios tutorial I am currently
working on.
Thank you for help.
I just found out why.
Problem was questionLabel.text not being written using xcode auto complete feature.
First time I typed manually and got error.
Second time, I let Xcode autocomplete it and it worked.
Anyone know why this works this way?

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)"
}

Xcode 7 Swift 2 error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, SUBCODE=0X0)

Ello! I am working on a notes app for mac, using swift as the language. I am currently working on a save and load feature, and have run across an error I am not sure how to fix. Here is the problematic code:
import Cocoa
class NoteViewController: ViewController {
#IBOutlet weak var notefield: NSTextField!
#IBOutlet weak var savenamefield: NSTextField!
#IBOutlet weak var loadnamefield: NSTextField!
#IBAction func save(sender: AnyObject) {
// The line below gets the error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0
NSUserDefaults.standardUserDefaults().setObject(notefield.stringValue, forKey: savenamefield.stringValue)
}
#IBAction func load(sender: AnyObject) {
var name = loadnamefield.stringValue;
var lnote = NSUserDefaults.standardUserDefaults().objectForKey(name)
notefield.stringValue = lnote as! String
}
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
What might I be doing wrong?
I figured it out. With some testing, I discovered that everything worked until I added notefield.stringValue into the code.I then realized that I had this is a different window. The code is covering 3 different windows, 2 of which are popups. notefield is the only element I had inside the code from the main window. I moved all of the elements to the same window, and recoded the entire thing. I got it working well using the following code:
import Cocoa
class NoteViewController: ViewController {
#IBOutlet weak var notefield: NSTextField!
#IBOutlet weak var savenamefield: NSTextField!
#IBOutlet weak var loadnamefield: NSTextField!
#IBAction func save(sender: AnyObject) {
NSUserDefaults.standardUserDefaults().setObject(notefield.stringValue, forKey: savenamefield.stringValue)
}
#IBAction func load(sender: AnyObject) {
notefield.stringValue = NSUserDefaults.standardUserDefaults().objectForKey((loadnamefield?.stringValue)!) as! String
}
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
Please note that I am supplying this code as a guideline only. Thanks for the help that was given.