unexpected build error in Xcode - ios

I'm attempting to build an iOS app in Xcode v6.2 using swift as a little personal project. So far I've cobbled together bits from my own research, however the following code throws an error when I try to build. Building full apps in swift is well out outside my knowledge scope so I'm hoping it's something obvious that someone can kindly guide me in the right direction.
The purpose is simply to populate a picker view, so I'm not in any way bound to the code if there is some simpler logic.
Here is the code:
import UIKit
import iAd
import QuartzCore
class ViewController: UIViewController, ADBannerViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate{
#IBOutlet weak var yearPicker: UIPickerView!
#IBOutlet weak var yearLabel: UILabel!
let listView = ["2013", "2014", "2015"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println("Entering super.viewDidload() function")
yearPicker.delegate = self
yearPicker.dataSource = self
}
//Data Sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return listView.count
}
//Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return listView[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
yearLabel.text = listView[row]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The error I'm receiving is:
fatal error: unexpectedly found nil while unwrapping an Optional value
and it the following line is highlighted in the IDE:
yearPicker.delegate = self
If there's anything else that might help please let me know.

So I ended up starting again, creating the connection by ctrl + dragging into the editor to create the #IBOutlet connection.
I then removed the .delegate and .dataSource methods because I received an error stating that yearPicker did not have those members. So now I'm left with this code which works perfectly :)
Thanks to Abizern for putting me on the right track.
import UIKit
import iAd
import QuartzCore
class ViewController: UIViewController, ADBannerViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate{
#IBOutlet var yearPicker: [UIPickerView]!
#IBOutlet weak var yearLabel: UILabel!
let listView = ["2013", "2014", "2015"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//Data Sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return listView.count
}
//Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return listView[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
yearLabel.text = listView[row]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Related

Deleting string from UIPicker upon selection

I am creating a booking app. I am using UIPickers to display open times. I have the pickers fully functional but am not sure how to delete booked times after a user has selected one from the choices provided. I am relatively new to Xcode, so any information is greatly appreciated!!
import Foundation
import UIKit
import Firebase
import FirebaseDatabase
class sTimeSelectionViewController:UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
#IBOutlet weak var myPicker: UIPickerView!
#IBOutlet weak var myLabel: UILabel!
let pickerData = ["9:30","10:30","11:30","12:30","1:30","2:30"]
override func viewDidLoad() {
super.viewDidLoad()
myPicker.dataSource = self
myPicker.delegate = self
myPicker.tag = 1
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
myLabel.text = pickerData[row]
}
#IBAction func postData(sender: AnyObject) {
let ref = FIRDatabase.database().referenceFromURL("deleted URL")
ref.child("Time").setValue(myLabel.text)
}
}
Thanks!
First change your pickerData declaration from constant to variable
var pickerData = ["9:30","10:30","11:30","12:30","1:30","2:30"].
Next you would need to remove the array element in your method didSelectRow after your myLabel.text code
pickerData.removeAtIndex(row)
and reload your picker
myPicker.reloadAllComponents()

UITextView throws hierarchy inconsistency

I am trying to show a picker view on touching a UITextView. It works, but then it suddenly crashes, throwing the following error (Frete Empresa is the name of the project):
2016-04-13 00:36:31.837 Frete Empresa[8927:361509] *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7af38e80> should have parent view controller:<Frete_Empresa.ViewController: 0x7ae723d0> but requested parent is:<UIInputWindowController: 0x7c227200>'
The code for viewController.swift:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var txtTipoCaminhao: UITextField!
#IBOutlet weak var pickerTipoCaminhao: UIPickerView!
var arrayTipoCaminhao = ["Semileve", "Leve", "Médio", "Semipesado", "Pesado", "Extrapesado"]
#IBAction func displayPicker(sender: AnyObject) {
pickerTipoCaminhao.hidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerTipoCaminhao.delegate = self
txtTipoCaminhao.inputView = pickerTipoCaminhao
pickerTipoCaminhao.hidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerTipoCaminhao: UIPickerView) -> Int {
return 1
}
func pickerView(pickerTipoCaminhao: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return arrayTipoCaminhao.count
}
func pickerView(pickerTipoCaminhao: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return arrayTipoCaminhao[row]
}
func pickerView(pickerTipoCaminhao: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
txtTipoCaminhao.text = arrayTipoCaminhao[row]
}
}
It appears to link the view to the name of the project, while in the code it appears to be referencing to the window control. What am I doing wrong?

Swift: fatal error: unexpectedly found nil while unwrapping an Optional value when initializing UIlabel value

I'm pretty new to swift (I usually code in Java and C++), and I'm building my first ios app.
In this app, I'm trying to create a picker view, and the problem is when I'm trying to set the initial value of the label point to the first item in the picker view (array) it pops up the error
"fatal error: unexpectedly found nil while unwrapping an Optional
value"
Here is my code I'm working
class ViewController: UIViewController, UIPickerViewDelegate{
#IBOutlet var usertypeLabel: UILabel!
#IBOutlet var Picker: UIPickerView!
var usertype_array = ["patient","nurse"]
//var usertype: String!
override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
usertypeLabel.text = usertype_array[0] //THIS IS WHERE THE ERROR OCCURS
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return usertype_array.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
return usertype_array[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
var rowselected = usertype_array[row]
usertypeLabel.text = rowselected
}
}
Any idea why the error happens? Note that it stills runs if I delete that line of code. Thanks.
You are missing UIPickerViewDataSource and add it here:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
And in your viewDidLoad method add this:
Picker.dataSource = self
Picker.delegate = self
And here is your full working code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
#IBOutlet var usertypeLabel: UILabel!
#IBOutlet var Picker: UIPickerView!
var usertype_array = ["patient","nurse"]
//var usertype: String!
override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
Picker.dataSource = self
Picker.delegate = self
usertypeLabel.text = usertype_array[0] //THIS IS WHERE THE ERROR OCCURS
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return usertype_array.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
return usertype_array[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
var rowselected = usertype_array[row]
usertypeLabel.text = rowselected
}
}
If your outlet is correct it will work fine.
Check THIS sample for more Info.

Picker view does not return always the right value

I've defined a picker which does not return always the right value. Sometimes it just works but most of the time when I pick the item pickerView returns the wrong value.
I did associate the outlet delegate to the view controller and am using the following code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
var categories = ["animals", "profession", "sea_animals", "food", "tools", "misc", "all"]
var cat: String = ""
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 numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
println("cat size: \(categories.count)")
return categories.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
println("cat: \(categories[row])")
return categories[row]
}
}
it works sometime but it is not reliable.
Any idea what I do wrongly?
Thanks!
I found the problem.
There is a function for selecting the row :)
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)

pickerView in Swift does not display data

This seems too simple to qualify as a post, but I've been stuck on this issue for longer than I'd like. The pickerView does not display the array [durations]. When run, the simulator is just the empty field. The code was copied directly from the UI class, yet it seems to be missing something.
class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var durationPicker: UIPickerView!
var durations = ["72 hours", "24 hours", "6 hours", "2 hours", "20 minutes"]
var durationsValue = [72.0, 24.0, 6.0, 2.0, 0.329]
var itemSelected: String = ""
var selectedDuration: Double = 0.0
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 numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 5
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return durations[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
itemSelected = durations[row]
selectedDuration = durationsValue[row]
}
}
You need to set the delegate and the datasource inside viewDidLoad()
durationPicker.delegate = self
durationPicker.dataSource = self
Or just connect your picker delegate and datasource using Xcode interface
I suggest watching this video. This lady does an excellent job explaining various iOS-related topics.
https://www.youtube.com/watch?v=MdXmIViD17U
The issue that jumps out at me is you've got arrays specified at the top. Rather than hard-code values in, you should use return yourArray.count to return Ints.

Resources