1

I'm making a game like logo quiz;

Would using the below code be appropriate for saving and loading data:

This code would only allow the user to play that specific logo once and once only!

of course I'd wire up each and every outlet that has an action!

Is this the right way or should I try something else?

[super viewDidLoad];

{  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
   NSString *loadstring = [defaults objectForKey:@"savedstring"]; 
   [_textbox setText:loadstring];
}

- (IBAction)btncheck:(id)sender {
   NSString *savestring = _textbox.text; 
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
   [defaults setObject:savestring forKey:@"savedstring"]; 
   [defaults synchronize];
}
John Topley
  • 113,588
  • 46
  • 195
  • 237
Ricky
  • 41
  • 5
  • your code seems appropriate : it's Ok for a simple case. Maybe in a more complx case, where you would have much more data to save, you would use something else than `NSUserDefault` which is only for simples cases like this. – Vinzzz Mar 30 '14 at 23:36
  • @Vinzzz A more complexed case such as saving score and coin data, is that correct? – Ricky Mar 30 '14 at 23:37
  • @Ricky more complex when is no longer primitive data, you would then want to use a database instead. – meda Mar 30 '14 at 23:39
  • @Vinzzz well I have 300 logos to go, do you believe that I should use a database? – Ricky Mar 30 '14 at 23:43
  • 1
    I reformatted your code - if it actually was all on one line and that wasn't just an artefact of the post, make sure to get out of that habit. It will become incredibly hard to read when your code gets more complex. – Adam Eberbach Mar 31 '14 at 00:10
  • @Ricky, there is no hard answer to when using a database vs. NSUserDefaults is more appropriate. The thing to remember about NSUserDefaults is that it is just a plist and that gets loaded in whole every time you do [NSUserDefaulst standardUserDefaults]... So at some point it's just going to get too big. When exactly depends on a number if factors. On the other hand, alternative solutions to this convenience method all come with a bunch of additional overhead, so you will have to balance yourself when the tipping point is reached. – user1459524 Mar 31 '14 at 00:22
  • @Vinzzz Could I ask you something important! Ok so I have 300 logos to fill in, what do you recommend I do: 1.) USE multiple UIViews on one ViewController for each logo or 2. make each logo appear on a new Viewcontroller and .h/m file? And if I do use many Viewcontrollers should I import files or make new ones for each logo? Thanks again! – Ricky Mar 31 '14 at 02:20
  • I'm not sure what you call 'logos', but UIViewControllers are only meant to manage relatively autonomous/independant parts of 'screens'. It seems to me you're only trying to represent a 'collection' of things, which are exactly what UITableView or UICollectionView are about. You can implement your own UIViewController using one of thesse collection containers, or you can use UIKit UITableViewController or UICollectionViewController. (these should be fine at first...) – Vinzzz Mar 31 '14 at 13:40

1 Answers1

0

I just wanted to add a bit of clarity, if this is the actual code you are going to use then I don't think it is appropriate. You mention that you only want each logo to be answered once and once only. How does saving a string to NSUserDefaults achieve this?

Given the code and assuming you are letting the user enter something into the text box you will get a different value stored with the key savedstring no previous value will be remembered.

Or assuming you don't allow the user to add anything into the textbox and keep adding to the string and saving it you don't show how you are going to check that its already been used/checked?

Instead of a string you could save a NSDictionary to NSUserDefaults, Saving NSDictionary to NSUserDefaults, each key could relate to the logo. You could then just check if the key already exists in the dictionary. Add any new ones to that dictionary and save it again.

Community
  • 1
  • 1
Flexicoder
  • 8,251
  • 4
  • 42
  • 56