-5

能否请你帮忙。我有很多这样的储蓄

SaveFBProfilePicture1 = ProfilePicture1.stringValue;

DefaultProfilePicture1 = [NSUserDefaults standardUserDefaults];
[DefaultProfilePicture1 setObject:SaveFBProfilePicture1 forKey:@"SaveProfilePicture1"];
[DefaultProfilePicture1 synchronize];

SaveFBProfilePicture2 = ProfilePicture2.stringValue;
DefaultProfilePicture2 = [NSUserDefaults standardUserDefaults];
[DefaultProfilePicture2 setObject:SaveFBProfilePicture2 forKey:@"SaveProfilePicture2"];
[DefaultProfilePicture2 synchronize];

SaveFBAlbums2 = Albums2.stringValue;
DefaultAlbums2 = [NSUserDefaults standardUserDefaults];
[DefaultAlbums2 setObject:SaveFBAlbums2 forKey:@"SaveAlbums2"];
[DefaultAlbums2 synchronize];

SaveFBAbout1 = About1.stringValue;
DefaultAbout1 = [NSUserDefaults standardUserDefaults];
[DefaultAbout1 setObject:SaveFBAbout1 forKey:@"SaveAbout1"];
[DefaultAbout1 synchronize];

SaveFBAbout2 = About2.stringValue;
DefaultAbout2 = [NSUserDefaults standardUserDefaults];
[DefaultAbout2 setObject:SaveFBAbout2 forKey:@"SaveAbout2"];
[DefaultAbout2 synchronize];

而且我不知道如何为此创建功能。

我需要一个功能

- (void)SaveFB {
SaveFBxy = xy.stringValue;
Defaultxy = [NSUserDefaults standardUserDefaults];
[Defaultxy setObject:SaveFBxy forKey:@"Savexy"];
[Defaultxy synchronize];}

例如,我可以轻松地将“x”更改为“关于”或“专辑”,将“y”更改为“1”或“2”

请帮我。并提前感谢您!

4

2 回答 2

1
- (void)SaveFB:(NSString *)x:(NSString *)y
{
    NSString *value=[NSString stringWithFormat:@"%@%@",x,y];
    NSString *key=[NSString stringWithFormat:@"Save%@%@",x,y];
    NSUserDefaults  *defaultxy = [NSUserDefaults standardUserDefaults];
    [defaultxy setObject:value forKey:key];
    [defaultxy synchronize];
}
于 2013-08-06T07:34:37.693 回答
0

这似乎很基本;

void setUserDefault(NSString* key, NSString* value) {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:value forKey:key];
    [userDefaults synchronize];
}

...或作为类方法;

+ (void) setUserDefault:(NSString*)key toValue:(NSString*)value {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:value forKey:key];
    [userDefaults synchronize];
}

如果一次设置多个值,您可能不想为每个值同步,但最好这样做;

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:ProfilePicture1.stringValue forKey:@"SaveProfilePicture1"];
[userDefaults setObject:ProfilePicture2.stringValue forKey:@"SaveProfilePicture2"];
[userDefaults setObject:Albums2.stringValue forKey:@"SaveAlbums2"];
[userDefaults setObject:About1.stringValue forKey:@"SaveAbout1"];
[userDefaults setObject:About2.stringValue forKey:@"SaveAbout2"];
[userDefaults synchronize];

(同步也会定期自动调用,因此您可能根本不需要它,除非您在应用退出时正确执行此操作)

于 2013-08-06T07:24:14.893 回答