objective c - Overwrite and Store Numerical Incremented Value in Variable -


so few months objective-c , have been writing simple program prompt user firstname, lastname , let's favoritecolor. have these user inputs stored via outputting contents .txt file. each time program executed, person object (with full name , favorite color) added list in .txt file via nsoutputstream class.

what want have sequential ticket system in place. each time program executed, ticket number (lets call variable topticket for clarity's sake) assigned new person object , printed out id of sorts along name , color.

so let's after first 2 times run project text file contains following:

favorite color inventory  name:  john doe favorite color:  red ticket:  #031354  name:  jane doe favorite color:  blue ticket:  #031355 

then want ticket variable (topticket) following next time open project files:

topticket = 031356 

i know how can have program overwrite initial value each time executed. therefor next time open project in xcode, after having run once earlier in day, topticket 1 more before (031354 in case), having incremented , overwritten previous value. way have call topticket variable every time when writing .txt file instead of requiring user input.

i hope making sense. love suggestions or guidance how should go this. ...maybe overthinking things haha.

one way accomplish use nsuserdefaults. can store value -setinteger:forkey: , read value -intergerforkey:. sample code:

nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; nsinteger ticketnumber = [defaults integerforkey:@"lastticketnumber"]; ticketnumber++; // use [defaults setinteger:ticketnumber forkey:@"lastticketnumber"]; 

i think @ 1 point in project want save data including user data. in case should save ticket number @ place, store other data, whatever is. keep data consistently.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -