Reputation: 87
In class 1
i save a int
converted to a string
, in class 2
I try to load this string for some reason it does not work, when I write the string which I get out of loadString
to a text box in class 2
the text box stays empty.
When i do the same thing in class 1
the text box becomes "1".
class 1.m
- (void)viewDidLoad
{
[super viewDidLoad];
countForString = 1;
saveString = [[NSString alloc]initWithFormat:@"%i", countForString];
[self SaveTextBox:saveString :@"Number"];
}
-(void)SaveTextBox:(NSString*)string :(NSString*)stringsave
{
NSString *savestring = string;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:stringsave];
[defaults synchronize];
}
class 2.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadString:@"Number":teamString];
tbTeamPlayer.text = teamString;
}
-(void)loadString:(NSString *)location:(NSString *)saveInString
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:location];
saveInString = loadstring;
}
Upvotes: 0
Views: 103
Reputation: 38259
In your loadString method new reference of saveInString is created. So it cannot reflect to any new modification to original reference.
Refer nsstring-returns-empty-from-a-return-method link.
Upvotes: 0
Reputation: 25642
You really need to work on naming variables, and use naming of parameters. It's one of the best features in ObjC.
From what I understand, your problem is that you think that saveInString = loadstring;
returns something. It does not. The pointer of the variable is copied when the method gets called.
For returning values, use the return value.
I strongly suggest to start with a good book on Objective C, as this code has really big problems with regard to the language.
Upvotes: 1
Reputation: 11174
loadString:location:
doesn't change the value of saveInString
, all it does is overwrite the local variable. You need to return the string and set that value, e.g.
tbTeamPlayer.text = [self loadString:someLocation];
and change loadString to do:
- (NSString *)loadString:(NSString *)location
{
return [[NSUserDefaults standardUserDefaults] stringForKey:location];
}
Upvotes: 0