Swift error unwrapping nil when data is present - ios

I am passing a User object into another segue. For whatever reason I can print out the object and view all the information in the console. I can even unwrap the data but when I go to insert the data into an outlet it crashes.
var user : User? {
didSet {
if let name = user?.name {
print(name)
nameLabel.text = name
}
}
}
As you can see the property exists, however as soon as I try to apply it to my IBOutlet it crashes....

If you are setting the value from some other controller and ChatVC is not loaded then your nameLabel is nil, you can prevent crash by checking nil for it.
var user : User? {
didSet {
if let name = user?.name {
print(name)
if nameLabel != nil {
nameLabel.text = name
}
}
}
}
Also if your setting the nameLabel where you have created instance of ChatVC then instead of doing this you need to set Label text in viewDidLoad of ChatVC like this.
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = user?.name
}

Related

How to store user data as string, and load that string in viewDidLoad

I am trying to load a value that has been inputted by the user in the viewDidLoad via a String. I am using UserDefaults to save the users value that they input into a UITextField (userValue), I then save this to the String 'search'. I am able to print out the value of search in the GoButton function, and it works fine, but when I load my ViewController as new, the value of 'search' is equal to nil. The aim here is to have the users previous search saved, and loaded into the UITextField (that is used as a search box) upon loading the ViewController.
Code Below:
class ViewController: UIViewController {
#IBOutlet weak var userValue: UITextField!
var search: String!
}
viewDidLoad:
override func viewDidLoad() {
if (search != nil)
{
userValue.text! = String (search)
}
}
Button Function:
#IBAction func GoButton(_ sender: Any) {
let userSearch: String = userValue.text!
let perference = UserDefaults.standard
perference.set(userSearch, forKey: "hello")
perference.value(forKey: "hello")
let value = perference.value(forKey: "hello") as! String
search = value
print (search) // <<this works, it prints out the users search value
}
#VishalSharma has the right idea, but the code should probably look more like…
override func viewDidLoad() {
super.viewDidLoad()
if let search = UserDefaults.standard.string(forKey: "hello") {
userValue.text = search
}
}
or even more simply…
userValue.text = UserDefaults.standard.string(forKey: "hello")
When you load, search is effectively nil.
So either you read userDefaults in viewDidload or you come through a segue: then you can load search in the prepare.
I've always found it convenient and useful to store all UserDefault properties as an extension within the same file along with their getters and setters. It is far easier to maintain, use and read. by using the #function keyword for the key you are referencing the variable's name and not a string that can be accidentally changed somewhere else in code.
UserDefaults.swift
import Foundation
// An Extension to consolidate and manage user defaults.
extension UserDefaults {
/// A value Indicating if the user has finished account setup.
/// - Returns: Bool
var finishedAcountSetup: Bool {
get { return bool(forKey: #function) }
set { set(newValue, forKey: #function) }
}
/// The hello text at the start of the application.
/// - Returns: String?
var helloText: String? {
get { return string(forKey: #function) }
set {set(newValue, forKey: #function) }
}
//etc...
}
When you use these values reference the standard settings:
//Setting
UserDefaults.standard.helloText = "Updated Hello Text"
// Getting
// for non-optional value you can just get:
let didCompleteSetup = UserDefaults.standard.finishedAcountSetup
// Otherwise, safely unwrap the value with `if-let-else` so you can set a default value.
if let text = UserDefaults.standard.helloText {
// Ensure there is text to set, otherwise use the default
label.text = text
} else {
// helloText is nil, set the default
label.text = "Some Default Value"
}
obviously, it provides nil because when view controller load the search is nil try this.
let perference = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
if (perference.value(forKey: "hello") != nil) {
search = perference.value(forKey: "hello") as! String
userValue.text! = String (search)
}
}

UILabel.text with Singleton String is nil - Warning: Attempt to present ** whose view is not in the window hierarchy

I have a dummy question...
in one of my ViewController, I set a UILabel text with a String var coming from a singleton:
var nom: String!
var prenom: String!
override func viewDidLoad() {
.....
self.nom = User.sharedInstance.nom
self.prenom = User.sharedInstance.prenom
....
print("User: \(self.nom) - \(self.prenom)")
self.labelWelcome.text = ("Bienvenue \(self.prenom) \(self.nom)")
the print is displaying the right user value, but my View is displaying Bienvenue nil nil....
any idea ?
try this:
if let nom1 = self.nom {
print("User: \(nom1)")
}

Swift: hot to detect editing by user has stopped in a UITextView

I have a table view where two cells have UITextView into which the user can enter long data. Following some answers here in Stackoverflow, I implemented a protocol/delegate to detect when the user has finished entering the data which then will be saved in a global dictionary:
class DetailsNewTaskViewController: UITableViewController, TextViewCellDelegate {
var cellData:[String:String] = [:]
func controllerView(controller: UITableViewCell, textViewDidEndEditing: String, atIndex:Int) {
switch(atIndex) {
case 0:
self.cellData["titolo"] = (controller as! LittleTextCell).textView.text
break
case 1:
self.cellData["oggetto"] = (controller as! BigTextCell).textView.text
break
default:
break
}
}
and this is the relative custom cell class:
class LittleTextCell: UITableViewCell, UITextViewDelegate {
#IBOutlet weak var label : UILabel!
#IBOutlet weak var textView : UITextView!
var delegate:TextViewCellDelegate?
var rowIndex:Int?
func textViewDidEndEditing(textView: UITextView) {
delegate?.controllerView(self, textViewDidEndEditing: textView.text, atIndex: rowIndex!)
}
}
where the delegate for textView is the class itself.
And this is a screenshot of the application:
The "problem" is that only AFTER the user taps another cell/field then the text is stored in the global dictionary. What about if the user taps "Fine" button (to save data) without having touched another field after he's finished entering the text? That a fatal nil error is raised. So I would like to know if there is a way to detect that the user has stopped typing in even if he's still inside that cell so that the content is always stored.
Is it possible? Is there a particular method to implement?
UPDATE: the function associated to "Fine" button:
func saveTask(sender:UIButton!) {
self.dateFormatter.dateFormat = "yyyy-MM-dd"
var taskToSave = Task(id: -1,
titolo: self.cellData["titolo"]!,
oggetto: self.cellData["oggetto"]!,
check_mail: self.cellData["check_mail"]!.toBool()!,
id_progetto: self.projects[self.cellData["progetto_nome"]!]!.id,
progetto_nome: nil,
assegnato_a: nil,
id_assegnato_a: self.users[self.cellData["assegnato_a"]!]!.id,
richiesto_da: nil,
id_richiesto_da: self.users[self.cellData["richiesto_da"]!]!.id,
priorita: self.cellData["priorita"]!,
termine_consegna: self.dateFormatter.dateFromString(self.cellData["termine_consegna"]!)!,
stato: self.cellData["stato"]!)
self.taskService.addTaskService(taskToSave) {
(response: String) in
if ((response.rangeOfString("Could not connect to the server.")) != nil) {
dispatch_async(dispatch_get_main_queue()) {
self.alertView.title = "Operazione fallita!"
self.alertView.message = "Impossibile connettersi al server. \n Riprovare."
self.alertView.delegate = self
self.alertView.addButtonWithTitle("OK")
self.alertView.show()
}
println(response)
}
else {
if ((response.rangeOfString("status code: 200")) != nil) {
dispatch_async(dispatch_get_main_queue()) {
self.alertView.title = "Operazione eseguita!"
self.alertView.message = "Task creato correttamente"
self.alertView.delegate = self
self.alertView.addButtonWithTitle("OK")
self.alertView.show()
self.navigationController?.popViewControllerAnimated(true)
}
}
else {
println(response)
}
}
}
}
define global selectedIndexPath
var selectedIndexPath:Int = 0
set it to selected indexPath
func controllerView(controller: UITableViewCell, textViewDidEndEditing: String, atIndex:Int) {
selectedIndexPath = indexPath
switch(atIndex) {
case 0:
self.cellData["titolo"] = (controller as! LittleTextCell).textView.text
break
case 1:
self.cellData["oggetto"] = (controller as! BigTextCell).textView.text
break
default:
break
}
}
In saveTask function get cell with cellForRowAtIndexPath
func saveTask(sender:UIButton!) {
let cell = tableView.cellForRowAtIndexPath(selectedIndexPath)
self.cellData["titolo"] = cell.LittleTextCell.textView.text
self.cellData["oggetto"] = cell.BigTextCell.textView.text
}
just use this line on the top in the method fine in your controller
self._tableView.superview?.endEditing(true);
I was facing the same problem , my textfields in cell and i want to check all the fields in controller.When i get my data so last data is not up to date because after last data, i click on the button not on the text field. So i found the solution. I wrote this line in my method (in your case, this line should be in fine method) before getting my dictionary and after that i had updated data.
Thanks

NSUserDefaults Loosing Value in Swift

Update: I've tried changing setValue to setObject, and the same error occurred.Upon further investigation with breakpoints and the LLDB, they are nil before the controller is even presented. I'm not saving them right.
I'm trying to simply save a couple of strings of text, and display them on another view using Swift. I'm not sure why I'm having such a hard time. Here is how I'm trying to accomplish this:
VC1
#IBAction func registerTapped(sender : AnyObject)
// Save the login information
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue(username.text, forKey: "username")
defaults.setValue(password.text, forKey: "password")
if firstName.text.isEmpty == false {
defaults.setValue(firstName.text, forKey: "firstname")
}
if lastName.text.isEmpty == false {
defaults.setValue(lastName.text, forKey: "lastname")
}
let profileView = ProfileViewController()
self.presentViewController(profileView, animated: true, completion: nil)
}
}
Cool. That looks like the correct way to save strings in UITextFields based upon my research. So, I open up VC2 and try to load the saved text into the new UITextField's, like so:
override func viewDidLoad() {
super.viewDidLoad()
let defaults = NSUserDefaults.standardUserDefaults()
username.text = defaults.stringForKey("username")
password.text = defaults.stringForKey("password")
if let first = defaults.stringForKey("firstname")
{
firstName.text = first
}
if let last = defaults.stringForKey("lastname") {
lastName.text = last
}
}
I get the crash fatal error: unexpectedly found nil while unwrapping an Optional value. I've been digging through tutorials for hours and can't for the life of me figure out what I am doing wrong.
Is it because it an an optional? This is my LLDB output:
Your issue has nothing to do NSUserDefaults, whats nil are your labels username, password, etc. in your second controller.
You should add a segue to your button (the one with registerTapped) to show the second controller and remove the last two lines in registerTapped.
Break your code into steps and debug each one. Your code would crash if your outlet is nil or if the key/value pair doesn't exist. Check that both username and password (The text fields) are not nil, as well as that the defaults results aren't nil:
var text: String?
text = defaults.stringForKey("username")
if let text = text
{
if let username = username
{
username.text = text
}
else
{
println("username field is nil!")
}
}
else
{
println("defaults stringForKey("username") = nil!")
}
text = defaults.stringForKey("password")
if let text = text
{
if let password = password
{
password.text = text
}
else
{
println("password field is nil!")
}
}
else
{
println("defaults stringForKey("password") = nil!")
}

Swift: Set UITextField.text from class (retrieved value from SQLite)

I am using Swift with SQLite.swift. I have the following UIViewController:
class LoginViewController: UIViewController {
#IBOutlet weak var emailField: UITextField!
func setEmailAddress(email:String){
emailField.text = email
}
override func viewDidLoad() {
MySQLite().updateLatestEmailAddressFromUserTable() // breaks here (email is in console, though...)
}
}
Then I am trying to update it's value (through the setEmailAddress function) from another class:
class MySQLite {
func updateLatestEmailAddressFromUserTable(){
let dbPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String
let db = Database("\(dbPath)/db.sqlite3")
let users = db["users"]
let id = Expression<Int>("id")
let email = Expression<String>("email")
let time = Expression<Int>("time")
for user in users.limit(1).order(time.desc) {
println(user[email]) // this works, correctly outputs in console: email#domain.com
LoginViewController().setEmailAddress(user[email]) // breaks here
}
}
}
above code gives me the following error
fatal error: unexpectedly found nil while unwrapping an Optional value
To explain a little further: I am retrieving the most recent entry in SQLite table to get the user's email address and update the text field in the login view controller. This allows for easier log in for returning users.
I have been struggling with this for over 2 hours now and trying various things. The main problem I believe is that when I try to simply return the email address as string from my second function and set the field directly from LoginViewController, it doesn't work (SQLite related code was not "executed" yet I believe).
possibly related thread (Obj-C):
set UITextField.text from another class
Here whats happening LoginViewController().setEmailAddress(user[email]) creates new instance of LoginViewController which is not same as your current LoginViewController.
Why don't you make protocol and define as delegate in MySQLite
And LoginViewController will have implementation of update method. Pass the delegate to MySqlite
In MySQLite when you get the value form database call the delegate update method.
Example
MySQLite
protocol loginDelegate
{
func update(NSString)
}
class MySQLite {
var delegate:loginDelegate?
func updateLatestEmailAddressFromUserTable(){
let dbPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String
let db = Database("\(dbPath)/db.sqlite3")
let users = db["users"]
let id = Expression<Int>("id")
let email = Expression<String>("email")
let time = Expression<Int>("time")
for user in users.limit(1).order(time.desc) {
println(user[email]) // this works, correctly outputs in console: email#domain.com
if((delegate) != nil)
{
delegate?.update("example#example.com")
}
}
}
}
class LoginViewController: UIViewController,loginDelegate {
#IBOutlet weak var emailField: UITextField!
func setEmailAddress(email:String){
emailField.text = email
}
override func viewDidLoad() {
var mySQLite: MySQLite=LoginClass();
mySQLite.delegate=self;
[mySQLite .updateLatestEmailAddressFromUserTable()];
}
func update(email: NSString) {
println(email);
emailField.text = email
}
}
Make sure that the view which has the emailField has been instantiated on the screen.
#IBOutlet weak var emailField: UITextField!
This is an optional, which will be nil until the storyboard or nib for it is loaded. I assume OnBoardingRegistrationFormController is an instance of your LoginViewController class?
I see you've accepted an answer, but in this case creating a protocol is likely overkill. If sqlite is your model, why not just have the function return a value, and then you can assign the value to the text field in the controller. ex.
class LoginViewController: UIViewController {
#IBOutlet weak var emailField: UITextField!
override func viewDidLoad() {
emailField.text = MySQLite().updateLatestEmailAddressFromUserTable()
}
}
class MySQLite {
func updateLatestEmailAddressFromUserTable() -> String{
let dbPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String
let db = Database("\(dbPath)/db.sqlite3")
let users = db["users"]
let id = Expression<Int>("id")
let email = Expression<String>("email")
let time = Expression<Int>("time")
for user in users.limit(1).order(time.desc) {
println(user[email]) // this works, correctly outputs in console: email#domain.com
return user[email]
}
}
}
The issue is that LoginViewController's view isn't loaded when you try to assign a text to the textField. i.e: emailField is nil and unwrapping nil values leads to a runtime crash (since the outlet has not been connected to it's storyboard/xib counterpart).

Resources