Patel Ankit Patel Ankit - 3 months ago 13
iOS Question

unexpectedly found nil while unwrapping an Optional value

i save simple message in

NSMutableArray


this is first view controller

import UIKit
import MessageUI
import Contacts
import ContactsUI

class OneView: UIViewController, MFMessageComposeViewControllerDelegate,CNContactPickerDelegate,UIAlertViewDelegate {

@IBAction func save(sender: AnyObject) {

if (NewSmu.text! == "") {
let alert = UIAlertController(title: nil, message: "Please enter message.", preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

self.presentViewController(alert, animated: true, completion: nil)

} else {
let uDefault = NSUserDefaults.standardUserDefaults()
var templates: NSMutableArray?

if templates != nil {

templates = NSMutableArray(array: uDefault.arrayForKey("TEMPLATES")!)

}
if ((templates?.containsObject(NewSmu.text)) != nil) {
templates!.addObject(NewSmu.text)
uDefault.setObject(templates, forKey: "TEMPLATES")
}

let alert = UIAlertController(title: "Success", message: "Template saved.", preferredStyle: UIAlertControllerStyle.Alert)


alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

self.presentViewController(alert, animated: true, completion: nil)
}
}
}


this is second view controller

import UIKit
class TwoView: UIViewController {

@IBOutlet weak var tableView: UITableView!
var templates = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let uDefault = NSUserDefaults.standardUserDefaults()
self.templates = NSMutableArray(array: uDefault.arrayForKey("TEMPLATES")!)
}
}

Answer

You have few issues in save and viewWillAppear functions. You need to check if saved array exists in user defaults. You may try this code:

First view controller:

@IBAction func save(sender: AnyObject) {
    let alert = UIAlertController(title: nil, message: "Please enter message.", preferredStyle: UIAlertControllerStyle.Alert)
    if NewSmu.text?.isEmpty == false {
        var templates = NSMutableArray()
        let uDefault = NSUserDefaults.standardUserDefaults()
        if let savedArray = uDefault.arrayForKey("TEMPLATES") {
            templates = NSMutableArray(array: savedArray)
        }
        if !templates.containsObject(NewSmu.text) {
            templates.addObject(NewSmu.text)
            uDefault.setObject(templates, forKey: "TEMPLATES")
        }
        alert.title = "Success"
        alert.message = "Template saved."
    }
    alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    presentViewController(alert, animated: true, completion: nil)
}

Second view controller:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    if let savedArray = NSUserDefaults.standardUserDefaults().arrayForKey("TEMPLATES") {
        templates = NSMutableArray(array: savedArray)
    }
}
Comments