以下是附加到从NSUserDefaults中提取的布尔值的一堆社交媒体名称。在上一个视图中,用户可以使用UISwitch来确定布尔变量的值。在这里,我只希望仅显示用户之前已打开的社交媒体(BOOL YES)。但是,当我记录可变数组时,它甚至不输出。就目前而言,阵列" medias"相应地输出1和0。字典会更好吗?
ViewController.m
self.oneTableView.dataSource = self;
self.oneTableView.delegate = self;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSNumber *facebook = [NSNumber numberWithBool:[prefs boolForKey:@"facebook"]];
NSNumber *twitter = [NSNumber numberWithBool:[prefs boolForKey:@"twitter"]];
NSNumber *instagram = [NSNumber numberWithBool:[prefs boolForKey:@"instagram"]];
NSNumber *tumblr = [NSNumber numberWithBool:[prefs boolForKey:@"tumblr"]];
NSNumber *gmail = [NSNumber numberWithBool:[prefs boolForKey:@"gmail"]];
NSNumber *linkedin = [NSNumber numberWithBool:[prefs boolForKey:@"linkedin"]];
NSNumber *github = [NSNumber numberWithBool:[prefs boolForKey:@"github"]];
NSNumber *youtube = [NSNumber numberWithBool:[prefs boolForKey:@"youtube"]];
NSNumber *vine = [NSNumber numberWithBool:[prefs boolForKey:@"vine"]];
NSNumber *soundcloud = [NSNumber numberWithBool:[prefs boolForKey:@"soundcloud"]];
NSArray *medias = @[facebook, twitter, instagram, tumblr, gmail, linkedin, github, youtube, vine, soundcloud];
NSLog(@"%@", medias);
int i;
NSMutableArray *includedMedias = [[NSMutableArray alloc] init];
for (i=0; i <= 10; i+=1) {
if ([medias[i] intValue] == 1) {
[includedMedias addObject:medias[i]];
}else{
return;
}
}
NSLog(@"Included Media: %@", includedMedias);
self.tableData = [includedMedias mutableCopy];
self.oneTableView.editing = YES;
修改
ViewController.m(来自存储密钥的上一个视图控制器):
- (void)viewDidLoad
{
[super viewDidLoad];
NSDictionary *socialValues = @{@"facebook": @NO,
@"twitter": @NO,
@"instagram": @NO,
@"tumblr": @NO,
@"gmail": @NO,
@"linkedin": @NO,
@"github": @NO,
@"youtube": @NO,
@"vine": @NO,
@"soundcloud": @NO
};
}
- (void)setupOneCell:(setupOneCell *)cell switchValueDidChange:(BOOL)switchValue {
NSIndexPath *indexPath = [self.oneTableView indexPathForCell:cell];
NSString *label = [self.medias[indexPath.row] socialMedia];
NSString *newLabel = [label.lowercaseString stringByReplacingOccurrencesOfString:@" " withString:@""];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[self.socialValues setValue:[NSNumber numberWithBool:switchValue] forKey:newLabel];
[defaults setObject:self.socialValues forKey:@"valueDict"];
[defaults synchronize];
}
ViewController.m(从UserDefaults中提取数据):
- (void)viewDidLoad
{
[super viewDidLoad];
self.oneTableView.dataSource = self;
self.oneTableView.delegate = self;
NSMutableArray *includedMedias = [[NSMutableArray alloc] init];
NSDictionary *mediaDict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"valueDict"];
for (NSString *key in mediaDict.allKeys) {
if ([mediaDict[key] intValue] == 1) {
[includedMedias addObject:key];
}
}
}
答案 0 :(得分:0)
代码的问题是这部分
}else{
return;
}
只要该代码找到未选中的内容,它就会从方法返回,因此永远不会到达NSLog
或mutableCopy
。
话虽如此,您可以通过创建一个媒体名称数组来简化代码,然后通过迭代来创建includedMedias
数组。
self.oneTableView.dataSource = self;
self.oneTableView.delegate = self;
NSArray *medias = @[@"facebook", @"twitter", @"instagram", @"tumblr", @"gmail",
@"linkedin", @"github", @"youtube", @"vine", @"soundcloud"];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableArray *includedMedias = [[NSMutableArray alloc] init];
for ( NSString *media in medias )
{
if ( [prefs boolForKey:media] )
[includedMedias addObject:media];
}
NSLog(@"Included Media: %@", includedMedias);
self.tableData = [includedMedias mutableCopy];
self.oneTableView.editing = YES;
答案 1 :(得分:-1)
最好让您的用户默认值存储一个字典,其中的键将是媒体名称,并且值将是BOOL值的NSNumbers。然后,您可以遍历键(从allKeys方法接收)并将键字符串添加到可变数组中(如果与该键关联的值为1)。
NSMutableArray *includedMedias = [[NSMutableArray alloc] init];
NSDictionary *mediaDict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"mediaDict"];
for (NSString *key in mediaDict.allKeys) {
if ([mediaDict[key] intValue] == 1) {
[includedMedias addObject:key];
}
}