Swift: label text --> "fatal error: unexpectedly found nil while unwrapping an Optional value" - swift

like it says in the title, i am trying to change label text upon click of a button. Error appears at line self.playerChoice.text = "You: Rock"
import UIKit
class ViewController: UIViewController {
var player : Int = Int()
#IBOutlet weak var readyLabel: UILabel!
#IBAction func noButton(sender: AnyObject) {
exit(0)
}
// ---------------------------------------------------------
#IBOutlet var computerChoice: UILabel!
#IBOutlet var playerChoice: UILabel!
#IBOutlet var score: UILabel!
// Variables -------------------------------------------------
let Rock : String = "Rock"
let Paper : String = "Paper"
let Scissors : String = "Scissors"
//------------------------------------------------------------
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
// ----------------------------------------------------------------
#IBAction func rockButton(rockbut: UIButton) {
player = 0
var ai = arc4random_uniform(3)
self.playerChoice.text = "You: Rock"
}
#IBAction func paperButton(paperbut: UIButton) {
player = 1
var ai = arc4random_uniform(3)
self.playerChoice.text = "You: Paper"
}
#IBAction func scissorsButton(scissorsbut: UIButton) {
player = 2
var ai = arc4random_uniform(3)
self.playerChoice.text = "You: Scissors"
}
}

I ran into this problem and it turned out that the labels that I was trying to edit didn't exist at the time the code ran.
Turns out I was referencing the same view controller from a parent view and a child container view. The labels I was trying to change were only in the container view but as both views loaded it ran the view controller for both so it tried to find the labels that didn't exist in the parent view and threw the above error.
So the lesson I learned... If a reference to a view object is throwing a NIL..
The View isn't properly mapped to the View Controller.
The object within the view isn't mapped to the referencing IBOutlet.
Or as in my case, there are multiple views connected to the same View Controller and references to view objects in one view are not being found in the other.

Looks like player choice is not initialized.
#IBOutlet var playerChoice: UILabel!
Maybe the connection between the outlet and InterfaceBuilder/Storyboard is lost. Try to connect it again.
I've created a small demo and everything works fine:
Check if the circles at the left side of your IBOutlet are filled. Otherwise the connection is lost.

What fixed this for me (and it gets me everytime, especially when you are new to using storyboards) is to make sure that you are initializing your view controller like so :
slideShowVC = (UIStoryboard(name: "Main",bundle: nil).instantiateViewControllerWithIdentifier("WWPhotoSlideShowVC") as! WWPhotoSlideShowVC)
instead of the stand alone xib way :
slideShowVC = WWSlideShowVC()
Or else all your outlets will be nil na many headaches will soon follow.

I ran into the same issue in Xcode 6.2.
I use individual XIBs instead of storyboards. The problem for me was that with Swift, Xcode does not automatically associate the XIB with the view controller if the names are the same. Hence the IBOutlets for the labels are pointing to nil giving the fatal.
You can change the viewcontroller.xib to be called modulename.viewcontroller.xib so that xcode can associate it with the view controller and the issue goes away.
More options are mentioned on this thread:
Can't Load UIViewController XIB file in Storyboard in Swift

I have tried this code and it working fine for me:
class ViewController: UIViewController {
var player : Int = Int() //Declare this globally
#IBOutlet weak var readyLabel: UILabel!
#IBAction func noButton(sender: AnyObject) {
exit(0)
}
// ---------------------------------------------------------
#IBOutlet var computerChoice: UILabel!
#IBOutlet var playerChoice: UILabel!
#IBOutlet var score: UILabel!
// Variables -------------------------------------------------
let Rock : String = "Rock"
let Paper : String = "Paper"
let Scissors : String = "Scissors"
//------------------------------------------------------------
#IBAction func rockButton(rockbut: UIButton) {
player = 0
var ai = arc4random_uniform(3)
self.playerChoice.text = "You: Rock"
}
#IBAction func paperButton(sender: UIButton) {
player = 1
var ai = arc4random_uniform(3)
self.playerChoice.text = "You: Paper"
}
#IBAction func scissorsButton(sender: UIButton) {
player = 2
var ai = arc4random_uniform(3)
self.playerChoice.text = "You: Scissors"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var p : String = "Undecided"
if (player == 0) {
var p: String = "Rock"
} else if (player == 1) {
var p: String = "Paper"
} else if (player == 2) {
var p: String = "Scissors"
}
}
}

Related

How do I assign Y to X?

I couldn't figure out how to copy value of variable into another variable in Swift, an example code for this in python would be
def assignVariable():
x=1
y=x
return y
RESULT 1
When I did this it doesn't seem to work in Swift. Is there any solution to this or am I doing something wrong?
Edit: problem is at
var originalCount=countDown
it gave me Use of unresolved identifier 'countDown' but when I assign it literally it works. Here's my swift code
import Cocoa
class MainWindow: NSWindowController {
var hitCount = 0
var started:Bool = false
var timer = 10
var colorList: [NSColor] = [ NSColor.black,NSColor.blue,NSColor.brown,NSColor.cyan,NSColor.darkGray,NSColor.gray,NSColor.green,NSColor.lightGray,NSColor.magenta,NSColor.orange,NSColor.purple,NSColor.red,NSColor.white,NSColor.yellow]
#IBOutlet weak var button1: NSButton!
#IBOutlet weak var scrubber1: NSScrubber!
#IBOutlet weak var display: NSTextField!
override func windowDidLoad() {
super.windowDidLoad()
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
var countdown=10
var originalCount=countDown
//(countdown,originalCount) = (10,10) //it works if i use this instead
func startGame(){
if(countDown>0 || started==true){
display.stringValue=String(countDown)
countDown-=1
let seconds = 1.0
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
self.startGame()
}
}else{
display.stringValue="Done "+String(hitCount)+" Taps in " + String(originalCount) + "Tap to RESET"
started=false
countDown=10;
}
}
#IBAction func labelPress(_ sender: Any) {
display.stringValue="__RESET__"
hitCount=0
countDown=10
started=false
}
#IBAction func buttonPressed(_ sender: Any) {
if started==false{
startGame()
}
button1.bezelColor = colorList[Int.random(in: 0..<colorList.count)]
started=true
button1.title=String(hitCount)
hitCount+=1
}
}
You can't initialise one variable with another at the top level in your class. Looking at your code I don't think that originalCount needs to be a property, move it inside startGame() instead and make it a local variable and also use let since it isn't changing
var countdown=10
func startGame(){
let originalCount = countDown
if(countDown>0 || started==true){
...
}

How to access a text field value and convert to double

I am working on trying to build a tip calculator using swift but am running into a multitude of problems. I have a text box where the user enters in the bill amount and then they can adjust a slider to indicate the percentage that they want to tip. I have managed to get the slider and label associated with it to work where changing the value on the slider changes the label. However, I can't figure out how to get the text field to work. I attempted to create an action for the bill amount text box being changed but no matter what I put in it, it doesn't seem like the code is ever being executed (Whenever I run the code and click in the text box, the keyboard won't go away so maybe this is part of the problem?). All that I need is to be able to access the value in the text box field inside of my action when I click the calculate button but I can't seem to even get the value to show up much less convert it to a double so I can perform calculations on it. I am super new to swift and really want to learn what I am doing wrong. I have tried multiple tutorials and similar questions on here but none of them work. I appreciate all help y'all can give.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var txtBillAmount: UITextField!
#IBOutlet weak var lblTipPercentage: UILabel!
#IBOutlet weak var sldTipPercentage: UISlider!
#IBOutlet weak var lblTipAmount: UILabel!
#IBOutlet weak var lblTotalAmount: UILabel!
var tipPercentage = 10
var billAmount = ""
#IBAction func valueChanged(sender: AnyObject) {
let currentValue = Int(sldTipPercentage.value)
lblTipPercentage.text = "\(currentValue)%"
tipPercentage = currentValue
}
#IBAction func btnCalculate(sender: AnyObject) {
lblTipAmount.text = "\(billAmount)"
}
#IBAction func txtBillAmountValueChanged(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
//txtBillAmount.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func btnReset(sender: UIButton) {
sldTipPercentage.value = 10
lblTipPercentage.text = "10%"
txtBillAmount.text = ""
lblTipAmount.text = "--"
lblTotalAmount.text = "--"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This is a screenshot of how the UI looks
Error that I am getting
[Xcode >= 7 and Swift > 2]
Try like this
#IBAction func btnCalculate(sender: AnyObject) {
if let textValue = txtBillAmount.text {
let billAmnt = Double(textValue)
let tipAmount = (billAmnt*tipPercentage)/100
lblTipAmount.text = "\(tipAmount)"
}
}
If you are using Xcode-6 and Swift < 2 then try the following way. because String initializer for Double is only available in Swift 2 (Xcode 7). [Recommend update your Xcode]
#IBAction func btnCalculate(sender: AnyObject) {
let billAmnt = (txtBillAmount.text! as NSString).doubleValue
let tipAmount = (billAmnt*tipPercentage)/100
lblTipAmount.text = "\(tipAmount)"
}

How to random position from UIButtons has created by storyboard?[swift Xcode]

I created 12 UIButtons by storyboard, in my program, these buttons represent cards,I want to random the buttons' position for shuffling the cards.please do me a favor, give a way or idea to achieve it.
my buttons problem
import UIKit
class ViewController: UIViewController {
lazy var game = Concentration(numberOfPairsOfCards: numberOfPairOfCards) //引入model Concentration
var numberOfPairOfCards : Int {
return (cardButtons.count + 1) / 2 //read only computed properties可以省略get{}
}
var flipCount = 0 {
didSet {
flipsCountLabel.text = "Flips: \(flipCount)"
}
}
#IBOutlet var cardButtons: [UIButton]!
#IBOutlet weak var flipsCountLabel: UILabel!
#IBAction func touchCard(_ sender: UIButton) {
flipCount += 1
if let cardNumber = cardButtons.index(of: sender) {
game.chooseCard(at: cardNumber)
updateViewFromModel()
} else {
print("somthing wrong")
}
}
I have done the job.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var buttons = [UIButton]()
for index in cardButtons.indices {
buttons.append(cardButtons[index])
}
for index in cardButtons.indices {
cardButtons[index] = buttons.remove(at: buttons.count.arc4random)
}
}
You could add the buttons to an array, shuffle the array and then loop through it and place the buttons. A better solution would be to create a UICollectionView instead though.

Swift OSX - Delegate protocol function returns nil, crashes when unwrapping textfield value

I'm working on an OSX app with Swift which makes use of an NSSplitView which holds two view controllers: "TableViewController" and "EntryViewController". I'm using delegates in order to transmit a custom NSObject ("Entry") on click from TableViewController up to the SplitViewController, then back down to the EntryViewController.
My problem is this: When the Entry object is received in the EntryViewController, any attempt to assign its properties to a text field value result in an unexpectedly found nil type error, never mind that the IBOutlets are properly linked, and that it can both print the Entry.property and the textfield string value (provided it is in a different, unrelated function).
I have tried many arrangements to solve this problem, which is why the current configuration might be a bit over-complicated. A delegate relation straight from Table VC to Entry VC caused the same issues.
Is there some way that the IBOutlets are not connecting, even though the view has loaded before the delegate is called? I've read many many articles on delegation—mostly for iOS—and yet can't seem to find the root of my problems. I'll be the first to admit that my grasp of Swift is a little bit piecemeal, so I am open to the possibility that what I am trying to do is simply bad/hacky coding and that I should try something completely different.
Thanks for your help!
TableViewController:
protocol SplitViewSelectionDelegate: class {
func sendSelection(_ entrySelection: NSObject)
}
class TableViewController: NSViewController {
#IBOutlet weak var searchField: NSSearchField!
#IBOutlet var tableArrayController: NSArrayController!
#IBOutlet weak var tableView: NSTableView!
var sendDelegate: SplitViewSelectionDelegate?
dynamic var dataArray = [Entry]()
// load array from .plist array of dictionaries
func getItems(){
let home = FileManager.default.homeDirectoryForCurrentUser
let path = "Documents/resources.plist"
let urlUse = home.appendingPathComponent(path)
let referenceArray = NSArray(contentsOf: urlUse)
dataArray = [Entry]()
for item in referenceArray! {
let headwordValue = (item as AnyObject).value(forKey: "headword") as! String
let defValue = (item as AnyObject).value(forKey: "definition") as! String
let notesValue = (item as AnyObject).value(forKey: "notes") as! String
dataArray.append(Entry(headword: headwordValue, definition: defValue, notes: notesValue))
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.sendDelegate = SplitViewController()
getItems()
print("TVC loaded")
// Do any additional setup after loading the view.
}
// send selection forward to entryviewcontroller
#IBAction func tableViewSelection(_ sender: Any) {
let index = tableArrayController.selectionIndex
let array = tableArrayController.arrangedObjects as! Array<Any>
let obj: Entry
let arraySize = array.count
if index <= arraySize {
obj = array[index] as! Entry
print(index)
print(obj)
sendDelegate?.sendSelection(obj)
}
else {
print("index unassigned")
}
}
}
SplitViewController:
protocol EntryViewSelectionDelegate: class {
func sendSecondSelection(_ entrySelection: NSObject)
}
class SplitViewController: NSSplitViewController, SplitViewSelectionDelegate {
var delegate: EntryViewSelectionDelegate?
#IBOutlet weak var mySplitView: NSSplitView!
var leftPane: NSViewController?
var contentView: NSViewController?
var entrySelectionObject: NSObject!
override func viewDidLoad() {
super.viewDidLoad()
// assign tableview and entryview as child view controllers
let story = self.storyboard
leftPane = story?.instantiateController(withIdentifier: "TableViewController") as! TableViewController?
contentView = story?.instantiateController(withIdentifier: "EntryViewController") as! EntryViewController?
self.addChildViewController(leftPane!)
self.addChildViewController(contentView!)
print("SVC loaded")
}
func sendSelection(_ entrySelection: NSObject) {
self.delegate = EntryViewController() //if this goes in viewDidLoad, then delegate is never called/assigned
entrySelectionObject = entrySelection
print("SVC:", entrySelectionObject!)
let obj = entrySelectionObject!
delegate?.sendSecondSelection(obj)
}
}
And Finally, EntryViewController:
class EntryViewController: NSViewController, EntryViewSelectionDelegate {
#IBOutlet weak var definitionField: NSTextField!
#IBOutlet weak var notesField: NSTextField!
#IBOutlet weak var entryField: NSTextField!
var entryObject: Entry!
override func viewDidLoad() {
super.viewDidLoad()
print("EVC loaded")
}
func sendSecondSelection(_ entrySelection: NSObject) {
self.entryObject = entrySelection as! Entry
print("EVC:", entryObject)
print(entryObject.headword)
// The Error gets thrown here:
entryField.stringValue = entryObject.headword
}
}
You don't need a delegate / protocol since there is a reference to EntryViewController (contentView) – by the way the instance created with EntryViewController() is not the instantiated instance in viewDidLoad.
Just use the contentView reference:
func sendSelection(_ entrySelection: NSObject) {
contentView?.sendSecondSelection(entrySelection)
}

Adding Ints in a UILabel in Swift

I am making an app where there is a football field and when you click on the Score Zone 7 points are added to the UILabel that marks score. But I have only been able to display a 7 once and if I click again the seven appears again; I want it to add 7 more to make a 14.
#IBAction func touchdownMD(sender: AnyObject) {
var pointsMD = Int()
scoreMD.text = "\(pointsMD + 7)"
}
#IBOutlet var scoreSH: UILabel!
When you initialize pointsMD inside of the touchdownMD: function, it starts from 0 whenever the function is called.
In order to fix this, you need to declare it at the class level. For example:
var pointsMD = Int() //initialize pointsMD at the class level
#IBAction func touchdownMD(sender: AnyObject){
pointsMD+=7 //add 7 to pointsMD
scoreMD.text = "\(pointsMD)"
}
That way, pointsMD will not reset whenever touchdownMD: is called, it will instead just increment as needed.
So, here's what your code could look like:
var pointsMD = Int()
#IBAction func touchdownMD(sender: AnyObject) {
pointsMD+=7
scoreMD.text = "\(pointsMD)"
}
#IBOutlet var scoreSH: UILabel!