Default Entries not updating properly

I am working on a small AS-S app that will allow me to input my time card info. I have a table (w/data source) that has 4 columns (Date, Time In, Time Out, Elapsed Time). The first 3 columns have date/time formatters attached to them. I can add to/remove from the table/data source w/out any problems. However when writing the contents of the data source out to User Defaults, nothing is written.

My first thought what perhaps the data in date-form was causing the problem, so I converted them to strings. No change. The only thing I can figure is the default entries don’t like a list of lists. Is that the case?

to ReadFromDefaults() --> this is called in a "Launched" handler
	set theData to the contents of default entry "TimeTable" of user defaults
	set theData to ConvertToDates(theData)
	return theData
end ReadFromDefaults

to WriteToDefaults() --> this is called in a "will close" handler
	set theData to the contents of every data cell of every data row of TimeTableDS
	set theData to ConvertToStrings(theData)
	set the contents of default entry "TimeTable" of user defaults to theData
end WriteToDefaults

to ConvertToStrings(theData)
	repeat with x in theData
		set item 1 of x to date string of item 1 of x
		set item 2 of x to time string of item 2 of x
		set item 3 of x to time string of item 3 of x
	end repeat
	return theData
end ConvertToStrings

to ConvertToDates(theData)
	repeat with x in theData
		set item 1 of x to date (item 1 of x)
		set item 2 of x to date (item 2 of x)
		set item 3 of x to date (item 3 of x)
	end repeat
	return theData
end ConvertToDates

When running in the debugger, I can see that the data I’m getting from the data source is correct. (TimeTableDS is a property. I’m using Xcode v2.2)

Thanks in advance,
Brad Bumgarner, CTA

I’m not very familiar with tables.

Have you tried using Bindings to automatically save the table’s content? (I would test this out with a duplicate of you project.) In Interface Builder, bring up the NSTableView in the Inspector and choose Bindings. Expand “content” (under “Table Content”), check the “Bind” box, and set the “Model Key Path” to “TimeTable-BindingTest” (I’m not using “TimeTable” because this project will still have the same identifier as your original, and I wouldn’t want to overwrite something your working on.)

Bruce,

Thanks for the reply. I’ve thought about using bindings. That would make life easier. Do I need to bind to User Defaults (also found in IB)? I will look into using bindings again in the morning.

Thanks,
Brad Bumgarner, CTA

Bindings (unless you specifically do it differently) uses the User Defaults system, but you shouldn’t need to do anything else in IB.

If you need/want to check that value in your script (I’m not sure if you need it for your script), you would still use something like this:

set theData to the contents of default entry "TimeTable" of user defaults

If I can add this without going too far off track:

You’d usually want to do that for preferences. Let’s say you an app that does a certain task after you click a button. You might have a preference to display a dialog after the script has finished it’s task. So, assuming you have a checkbox bound to “DisplayConfirmation,” you could use something like this at the end of your script:

get contents of default entry "DisplayConfirmation" of user defaults
if result is true then display dialog "Script finished."