I seem to be tripping myself up here. I am loading/saving out a single line of text with an integer value.
I read it back in to highscore. Before saving out I check the score against highscore and if it is higher replace it.
I get an error when trying to convert the highscore from the text property
var n as integer = integer.FromText( highscore )
if score > n then
highscore = score.ToText
Dim file As FolderItem = SpecialFolder.Documents.Child(“hs.txt”)
If file <> Nil Then
Dim output As TextOutputStream
output = TextOutputStream.Create(file, TextEncoding.UTF8)
output.WriteLine(highscore)
output.Close
I was looking for Val but realised those days are gone just starting to look at trimming. I only save out a numeric value but as text, when I look at the error it says it doesn’t like ’ a single quote when trying to get the value of the text??
Exactly.
highscore must contain only numbers.
If you have saved ‘for excel’ and stuck a single quote in front, that breaks conversion to and from numbers.
Mind you, I see no sign of a single quote in your SAVE code
Are you remembering to set the encoding when you read it back in?
If no, you may(? long shot) be running into a couple of control characters (the BOM) at the start of your text file.
When I had the encoding set UTF16 it complained about a " quote so I changed back to UTF8 and the complaint is ’ quote which is probably eol. When I grab the length-1 it behaves but I still don’t think I’m doing it correctly. Maybe I should have done deadline instead of readall?
Where are the NSUserDefaults values being saved? Locally on the device?
I am asking because right now in my app, when the user makes an in-app purchase, I store this information in a text file on the device. When the user starts the app the next time I will read from that text file if the purchase is already made. That’s probably not a good way to do it, is it?
You should definitely use NSUserDefaults for this. They are saved on the device in a special location hidden from view and designed for that use. I believe your app can save like 4K of data in NSUserDefaults
Using the built in classes designed for this use over a hacked together system using files? Yeah its better. Down the road if he wants to save other values as well he doesnt create compatibility issues with what order, encoding, format, etc he saves the values, it will just work with NSUserDefaults.
They are saved locally on the device, but are also backed-up on iCloud if you have backup enabled.
If the user activated the feature to unload unused apps, the data will be kept when re-installing the app. Which isn’t necessarily the case for your own system using files.
EDIT: one more reason to use NSUserDefaults is that values are cached by the OS when the app is in use. Meaning there is no overhead when reading the value multiple times throughout the workflow of the app.