class AddElementVC: UIViewController {
// textfields and some other functions are defined here
@IBAction func addElement(sender: UIBarButtonItem) {
let newElement = Element(/* properties are defined here */)
var theDict = NSUserDefaults.standardUserDefaults().dictionaryForKey(tableViewData) as [String: [Element]]
if var theArray = theDict[newElement.someProperty] {
theArray += newElement
theDict[newElement.someProperty] = elementArray
} else{
theDict[newElement.someProperty] = [newElement]
}
NSUserDefaults.standardUserDefaults().setObject(theDict, forKey: tableViewData)
}
}
EXPLAINATION OF THE CODE ABOVE:
My tableView-app receives its data from a dictionary of type [String: [Element]] (Element is a custom class), which is loaded from the userDefaults. In a second viewController you can fill out some textFields and finally press a UIBarButtonItem. From the textFields' data a new instance of the class "Element" is created and added to "theDict" at the key of one of the "newElement"'s properties. All that works fine as I checked it with console-outputs, but I am not able to finally save the edited dictionary in the userDefaults. There are no syntax-errors, but when I use the app and try to to add another element to the Dictionary via the second viewController, the following error is displayed:
ERROR-MESSAGE:
NSForwarding: warning: object 0xdcb002c of class '_TtC12myApp7Element' does not implement methodSignatureForSelector: -- trouble ahead
Researches didn't help me to understand the error-message.
EDIT
import Foundation
class Element: NSCoding {
enum State: String {
case state1 = "state1"
case state2 = "state2"
}
let state: State
// some more properties
init(state: String /* something more */ ){
self.state = State.fromRaw(state)!
// something more
}
init(coder aDecoder: NSCoder!) {
self.state = aDecoder.decodeObjectForKey("state") // displays error: 'AnyObject' is not convertible to 'Element.State'
// something more
}
func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeObject(self.state, forKey: "state") // displays error: Extra Argument 'forKey' in call
// something more
}
}