Ho un oggetto nella radice di plist che memorizza oggetti e chiavi (uno dei quali è immesso da un campo di testo utente). Non ho problemi a scrivere e creare strutture; sta cercando di leggere i valori che causano problemi. Sotto il codice che ho usato per scrivere i dati, qualcuno può dirmi come leggere la stringa [val] e [myKey]?Come leggere la stringa da NSUserDefaults
grazie!
#define defaultValue @"Your Name"
#define myKey @"PersonName"
- (void)textFieldAction:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *val;
// Get the new value from the textfield
val = [myTextField stringValue];
if ([val isEqualToString:defaultValue]) {
[defaults removeObjectForKey:myKey];
} else {
// [defaults setObject:val forKey:myKey];
NSDictionary *my_dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Name", @"PersonName",
@"Email", @"PersonEmail",
@"Dept", @"PersonDept",
@"Job", @"PersonJob",
val, myKey,
@"Status", @"PersonStatus",
nil];
[defaults setObject: my_dict forKey: @"Person"];
[[NSUserDefaults standardUserDefaults] registerDefaults:my_dict];
vorrei per popolare automaticamente il campo di testo con il [val], per [myKey], se l'utente ha l'informazioni già in plist. Ecco il codice che sto cercando di utilizzare per questo:
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
NSUserDefaults *defaults = [[NSUserDefaults standardUserDefaults] objectForKey:myKey];
NSString *val = [defaults stringForKey:myKey];
if (val == nil) val = defaultValue;
[myTextField setStringValue:val];
Questo codice viene effettivamente compilato? – sch
heh. sì, lo fa senza problemi, non deve essere così male, giusto? :-p – Devon
Non è un'app per iOS, è OS X. – Devon