Wrong init() method called

I keep getting an exception like “2015-06-24 17:44:22.299 RaiseMan[3644:136588] *** -[NSKeyedUnarchiver decodeFloatForKey:]: value for key (raise) is not a float number”. I think that it is calling init(coder:) instead of init() at start-up even though I have added the override init() as per the book. Any suggestions where I have gone wrong?
Thanks

From the error message, it seems to me that the problem is that whatever was stored in raise when it was archived was not a float. Or, it was archived as something other than a float. Use the debugger to examine what has been archived for raise and determine its type. That should point you in the right direction.

I agree with BrianLawson’s diagnosis. Double check your implementation of encodeWithCoder(:slight_smile: and make sure you are calling encodeFloat(:forKey:) for the “raise” key.

Do not think that is the problem, as it occurs when the program first starts - i.e. I am not reading in a file, only when I initially run RaiseMan. It actually does not give an error when I try to Open… a saved file. It reads in saved files perfectly.
I believe it must be using the wrong init() when it first starts up, and I cannot figure out why.

Actually did some further investigation, and found out the first thing the program tries to do in read in data. Not sure why.

When an app opens, it tries to restore the state that existed when the app last quit. If documents were open when the app quit, the app will try to open those documents. If a document is malformed, an exception will happen.

Try setting breakpoints on the first line of each of the following functions:

In Document.swift:

In Employee.swift

Run the app. When the breaks on the first line of readFromData, click the triangle next to self in the Variables View of the Debug Area (Fig 8.12) to disclose the document’s properties. Do the same to disclose the properties of AppKit.NSDocument.

With the properties of AppKit.NSDocument disclosed, you will see the property _fileURL a few lines down. This is the full path to the file being opened. A few lines later is the property _displayName, which is the file name as it will appear in the title bar of the window.

Note that these property names begin with underscore “_”. These properties are private to NSDocument. That means that it is ok to inspect them while debugging; it would be a mistake to write code that depends on these properties. Apple could change how NSDocument is implemented and how these properties are implemented, which might cause your code to break.

If you are new to the debugger, the authors have a good introduction in Chapter 8, Using the Debugger, p 147 - 153.

If you would post your code for the following functions we might be able to spot the problem:

From Document.swift:

dataOfType(typeName: String, error outError: NSErrorPointer) -> NSData? readFromData(data: NSData, ofType typeName: String, error outError: NSErrorPointer) -> Bool

From Employee.swift:

encodeWithCoder(aCoder: NSCoder) init(coder aDecoder: NSCoder) init()

Thanks for that. Have now got the problem fixed. It was trying to read an unsaved file from a previous buggy version where “raise” was saved as an object not a float.

That would do it! :sunglasses: