I have an iOS game, I am trying to save settings (which are generally stored within an array) to file.
currently,I have the file opened and read in the openStack handler; I have the file written in the shutdown handler...
but, in the openStack handler, how do I test to see if the file has actually been created... and if it doesn't exist I want to create one and write in some default settings
What's the best way to do this?
Usually, I just open and read the file. Next, I put the contents into variables. If the contents happens to be empty, then I use a default value. This makes it unnecessary to check that the file exists and, more importantly, is more compatible with future versions of your software.
on readPrefs
put specialFolderpath("documents") & "/prefs.dat" into myPath
put url ("binfile:" & myPath) into myPrefs
// here, the result may contain "can't open file"
put line 1 of myPrefs into gHighscore
if gHighScore is empty then put 0 into gHighscore
put line 2 of myPrefs into gLicenseKey
if gLicenseKey is empty then put "unregistered" into gLicenseKey
end readPrefs
You could also check for the file and use a slightly different script:
on readPrefs
put specialFolderpath("documents") & "/prefs.dat" into myPath
if there is a file myPath then
put url ("binfile:" & myPath) into myPrefs
put line 1 of myPrefs into gHighscore
put line 2 of myPrefs into gLicenseKey
end if
if gHighScore is empty then put 0 into gHighscore
if gLicenseKey is empty then put "unregistered" into gLicenseKey
end readPrefs
More variations are possible, e.g. you could check the result and set default values if the file can't be opened.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments