1

My main problem is I'm not getting the text I type in to go into the label or the table I made (a little deviation, but wanted to customize it a bit). I'm not getting any errors, but could someone take a look at this code and see if there are any major errors in it that I may be overlooking? Edit: I keep going back and changing small things like the type, but nothing is working. Here's my whole code, in case it makes more sense for everyone

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate {

var weatherList = [String] ()

@IBOutlet weak var textField: UITextField!

@IBOutlet weak var textLabel: UILabel!

@IBAction func searchButton(sender: AnyObject) {

    weatherList.append(textField.text!)

    textField.text = ""

    NSUserDefaults.standardUserDefaults().setObject(weatherList, forKey: "weatherList")

}

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    if NSUserDefaults.standardUserDefaults().objectForKey("weatherList") != nil {

        weatherList = NSUserDefaults.standardUserDefaults().objectForKey("weatherList") as! [String]

    }

    self.textField.delegate = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return weatherList.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    cell.textLabel?.text = weatherList[indexPath.row] as String

    return cell

}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == UITableViewCellEditingStyle.Delete {

        weatherList.removeAtIndex(indexPath.row)

        NSUserDefaults.standardUserDefaults().setObject(weatherList, forKey: "weatherList")

        tableView.reloadData()

    }

}

override func viewDidAppear(animated: Bool) {
    tableView.reloadData()

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    self.view.endEditing(true)

}

func textFieldShouldReturn(textField: UITextField) -> Bool {

    textField.resignFirstResponder()

    performAction()

    return true

}

func performAction() {

    weatherList.append(textField.text!)

    textField.text = ""

    NSUserDefaults.standardUserDefaults().setObject(weatherList, forKey: "weatherList")

}

}
Fumbles
  • 122
  • 7

1 Answers1

1

From the NSUserDefaults class reference:

The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.

That is not swift arrays. Use an NSMutableArray instead:

var weatherList:NSMutableArray = NSMutableArray()

then

self.weatherList.addObject(textField.text)
beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • I fixed that, but now I'm getting an error in my AppDelegate on the first line | class AppDelegate: UIResponder, UIApplicationDelegate { | that says Thread 1: signal SIGABRT – Fumbles Nov 17 '15 at 04:05
  • In your code you're still using a swift style array. NSUserDefaults can only accept the object types listed above. You can't cast like this either: weatherList = NSUserDefaults.standardUserDefaults().objectForKey("weatherList") as! [String] If you want to use NSUserDefaults, you need to use NSArray. Here's another question with the same problem and the same answer I've given: http://stackoverflow.com/questions/32798887/app-delegate-weird-error-when-trying-to-add-element-to-nsuserdefaults – beyowulf Nov 17 '15 at 12:55
  • Alright so if I change it to an array, what do I change _NSUserDefaults.standardUserDefaults().setObject(weatherList, forKey: "weatherList")_ and _weatherList = NSUserDefaults.standardUserDefaults().objectForKey("weatherList")_ to? – Fumbles Nov 17 '15 at 17:35
  • Or if I change it to an NSArray, what do I code in to append and remove? – Fumbles Nov 17 '15 at 17:49
  • NSUserDefaults.standardUserDefaults().setObject(weatherList, forKey: "weatherList") and weatherList = NSUserDefaults.standardUserDefaults().arrayForKey("weatherList").mutableCopy(). I gave you and example of how to "append" using NSMutableArray: self.weatherList.addObject(textField.text) if you'd like to remove an object self.weatherList.removeObjectAtIndex(index) – beyowulf Nov 17 '15 at 18:19