Save/Load Integer

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) 


End If
End if

What is the exact content of the variable highscore ? (does it end with a carriage return possibly…)

And what is the error?

Off: Integer.fromtext generates exceptions, where Val() would just return 0 for stuff it didn’t like.
Personally, I much prefer the old way.

I was looking for Val but realised those days are gone :slight_smile: 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??

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

output = TextOutputStream.Create(file, TextEncoding.UTF8)

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?

Yes. Readline gets text from the row, stopping at the first EOL
ReadAll is for when you have paragraphs of the stuff and want it all at once.

1 Like

If you just want to read/save values the best practice is to use NSUserDefaults from iOSKit:


Dim defaults As Foundation.NSUserDefaults = Foundation.NSUserDefaults.StandardUserDefaults
Dim value as Integer = defaults.IntegerForKey("highscore")


Dim defaults As Foundation.NSUserDefaults = Foundation.NSUserDefaults.StandardUserDefaults
defaults.SetIntegerForKey(10, "highscore")

NSUserDefaults can save Text, Integers, Doubles, Booleans and many more.


Much better :slight_smile:

Extra tip: when using NSUserDefaults make sure to never set an empty text value or empty key. That would make your app crash

1 Like

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

Much better :slight_smile:

Doesn’t sound like it is…

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.

Great, thanks Jason & Jeremie. I changed it in my app. Works just fine.