I'm having a difficulty understanding what output.text means. I think my understanding is that output is an instance of the class UIViewController and text is a variable defined in this class. Is this correct? Also, in the function, consoleOut(), why should one do output.text = output.text + text instead of output.text = text?
//
// ViewController.swift
// Guessing Game
//
// Created by Jae Hyun Kim on 8/6/15.
// Copyright (c) 2015 Jae Hyun Kim. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var inputField: UITextField!
#IBOutlet weak var output: UITextView!
var guesses : UInt = 0;
var number : UInt = 0;
var gameover = false;
let MAX_GUESSES = 8;
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.
}
func consoleOut(text : String) {
output.text = output.text + text;
}
#IBAction func guess(sender: UIButton) {
}
}
output is a UITextView that is defined in the view (the file that shows what screen will appear to the user). So you are correct in saying that text is a variable of the UITextView class.
Additionally, output.text = output.text + text will append text to the existing text in output.text. This is unlike output.text = text which will overwrite whatever was already in output.text with the new text.
Related
I'm trying to create my first game in apples xcode program using swift code but I've come across a problem with Arrays. Here's the code:
//
// ViewController.swift
// War
//
// Created by grayson seymour on 5/13/16.
// Copyright © 2016 SanGames. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var firstCardImageView: UIImageView!
#IBOutlet weak var secondCardImageView: UIImageView!
#IBOutlet weak var playRoundButton: UIButton!
#IBOutlet weak var backgroundImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.playRoundButton.setTitle("Play", forState: UIControlState.Normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func playRoundTapped(sender: UIButton) {
var cardNamesArray:[String] = ["card0", "card1", "card2", "card3", "card4", "card5", "card6", "card7", "card8", "card9"]
// Creates randomly generated number for first and second card
let randomCardFirst = Int(arc4random_uniform(10))
let randomCardSecond = Int(arc4random_uniform(10))
// checks to see what imageview to set for the cards
let firstCardString:String = self.cardNamesArray[randomCardFirst]
let secondCardString:String = self.cardNamesArray[randomCardSecond]
// sets randomly generated number to imageview for second and first card
self.firstCardImageView.image = UIImage(named: firstCardString)
self.secondCardImageView.image = UIImage(named: secondCardString)
}
}
I'm getting an error on line 39 & 40 or more specifically
let firstCardString:String = self.cardNamesArray[randomCardFirst]
let secondCardString:String = self.cardNamesArray[randomCardSecond]
The error I'm getting is
value of type View Controller has no member cardNamesArray
As I am very new to this language I have dabbled a little bit in the code to see if I could fix the problem but everything I do just creates another error. Any help would be very appreciated. Thanks!
Why do you call self before your variable (Because your class has no variable called cardNamesArray), this variable is inside your function (mean that it is not global), so shouldn't call self.
Replace
let firstCardString:String = self.cardNamesArray[randomCardFirst]
let secondCardString:String = self.cardNamesArray[randomCardSecond]
By
let firstCardString:String = cardNamesArray[randomCardFirst]
let secondCardString:String = cardNamesArray[randomCardSecond]
If you just want to use this cardNamesArray in your function, you can try this solution above.
If you want to use it multiple time inside your class, you can declare cardNamesArray as a global variable.
So recently I have been making a practice app that has a textfield and a label. When you enter something into the textfield and hit save it should change the label to that text and save it. The reason why it saves is so when you open the app the text of the label is equal to the last thing you saved. The text will save again if you hit save on a new string in the text box.
The problem I am having is that the label won't appear with text until you save something new and the string isn't saving so their is nothing there even if you saved something.
Here is my code from the ViewController.swift:
//
// ViewController.swift
// Help
//
// Created by Lucas on 9/30/15.
// Copyright (c) 2015 Lucas. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet var Savedlbl: UILabel!
#IBOutlet var Textfield: UITextField!
#IBOutlet var Label: UILabel!
var current = ""
var Saved = ""
override func viewDidLoad() {
super.viewDidLoad()
let currentDefault = NSUserDefaults.standardUserDefaults()
Savedlbl.text = Saved
if(currentDefault.valueForKey("Saved") != nil)
{
self.Saved = currentDefault.valueForKey("Saved") as! NSString! as String
}
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func Set(sender: AnyObject) {
setall()
}
func setall()
{
current = Textfield.text!
Label.text = Textfield.text!
let currentDefault = NSUserDefaults.standardUserDefaults()
Saved = (currentDefault.valueForKey("saved") as? String)!
currentDefault.setValue(Saved, forKey: "saved")
Savedlbl.text = Textfield.text
currentDefault.synchronize()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Here is where I think the problem is in the view did load that would make sense. When the app loads its not setting it to the last thing saved.
import UIKit
class ViewController: UIViewController {
#IBOutlet var Savedlbl: UILabel!
#IBOutlet var Textfield: UITextField!
#IBOutlet var Label: UILabel!
var current = ""
var Saved = ""
override func viewDidLoad() {
super.viewDidLoad()
let currentDefault = NSUserDefaults.standardUserDefaults()
Savedlbl.text = Saved
if(currentDefault.valueForKey("Saved") != nil)
{
self.Saved = currentDefault.valueForKey("Saved") as! NSString! as String
}
// Do any additional setup after loading the view, typically from a nib.
}
Thanks and let me know if you need any more info, I will provide it as fast as I can!
Don't use valueForKey/setValue:forKey:. Those are KVC methods. You want setObject:forKey: and objectForKey:.
Your problems are that you are doing things in the wrong order. In viewDidLoad you are setting the textfield before you have retrieved the value from user defaults and in setall you are re-saving the previously saved value, not the new value.
Try:
import UIKit
class ViewController: UIViewController {
var saved=""
var current=""
#IBOutlet var Savedlbl: UILabel!
#IBOutlet var Textfield: UITextField!
#IBOutlet var Label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let currentDefault = NSUserDefaults.standardUserDefaults()
self.saved=currentDefault.stringForKey("Saved") ?? ""
self.Savedlbl.text=self.saved
}
#IBAction func Set(sender: AnyObject) {
setall()
}
func setall() {
self.current = self.Textfield.text!
self.Label.text = self.current
let currentDefault = NSUserDefaults.standardUserDefaults()
currentDefault.setObject(self.current,forKey:"Saved")
self.Savedlbl.text = self.Textfield.text
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Also, by convention variables should start with a lower case letter and classes with an upper case. I changed Saved to saved and Current to current but I didn't change the IBOutlets because you will need to remake the connection in IB for those, but you should.
I'm trying to create a dictionary-like app using a dictionary with multiple key-value pairs.
The idea is when the user types and existing key in the textField and clicks the button the value of that key is displayed on the label. I could only get to display all the entries (including keys and values) when button is clicked (irrespective of the content of the textField), but that's not what I'm trying to achieve.
Here's my code:
//
// ViewController.swift
// Dictionary
//
// Created by Ralph on 7/20/15.
// Copyright (c) 2015 Ralph. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
let words = [
“doe” : “a deer, a female deer”,
“ray" : “a drop of golden sun”,
“me” : “a name I call myself”
]
#IBOutlet weak var textField: UITextField!
#IBAction func searchButton(sender: AnyObject) {
displayMeaning.text = words.description
}
#IBOutlet weak var displayMeaning: UILabel!
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 need to access the text in the textfield like so
displayMeaning.text = words[textField.text]
In your #IBAction you should try this:
#IBAction func searchButton(sender: UIButton) {
displayMeaning.text = words.objectForKey(textField.text)
}
What is wrong with this it is meant to change text in label to "hi" after you click the button.
ViewController.swift:
//
// ViewController.swift
// HelloSwift
//
// Created by Abdel Rahman Osman on 10/13/14.
// Copyright (c) 2014 Abdel Rahman Osman. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var TextGoesHere: UILabel!
#IBAction func PrintText(sender: AnyObject) {
var Text = TextGoesHere.text
Text = "Hi"
}
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.
}
}
Please help me fix this code as I am new to app development.
What you are doing is creating a copy of the text in the label, discard it and reassign a new value, and then discard it again when the variable goes out of scope.
The correct way is by assigning the text directly to the text property:
#IBAction func PrintText(sender: AnyObject) {
TextGoesHere.text = "Hi"
}
I can't seem to write the if statement in the end to make the outs go up when the strikes hit 3?
can someone lease help and make it so in swift the outs will go up when someone hits 3 strikes
//
// ViewController.swift
// helloWordDemo
//
// Created by Developer on 6/8/14.
// Copyright (c) 2014 AECApps. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
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.
}
#IBOutlet var labelDispaly : UILabel = nil
// dispaly Strikes
var counter = 1
#IBAction func buttonPressed(sender : AnyObject) {
labelDispaly.text = "Strikes \(counter++)"
}
//button to add strikes
#IBOutlet var OutsDispaly : UILabel = nil
var outsCounter = 1
//outs dispaly
#IBAction func outsButtonPressed(sender : AnyObject) {
OutsDispaly.text = "Outs \(outsCounter++)"
}
//button to add outs
if counter = 3 {
outsCounter ++
}
}
Use a property observer:
var counter = 1 {
didSet {
if counter == 3 {
self.outsCounter++
}
}
}
Whenever counter gets changed, didSet will be called.
(Also note that the equality operator is ==. = is for assignment.)