iphone - 通过 tableview 单元格删除 NSUserDefault 对象

标签 iphone objective-c ios nsuserdefaults

我有一个存储在 NSUserDefaults 中的收藏夹列表,单个收藏夹由 NSDictionary 存储:

NSUserDefaults *userPref = [NSUserDefaults standardUserDefaults];

NSMutableDictionary *bookmark = [NSMutableDictionary new];

[bookmark setValue:author.text forKey:@"author"];
[bookmark setValue:book.text forKey:@"book"];
[bookmarks addObject:bookmark];

[userPref setObject:bookmarks forKey:@"bookmarks"];
[userPref synchronize];

一切都很好,但现在我想通过滑动单元格来删除单个收藏夹。我已经添加了显示删除滑动的方法,但我不知道如何从 nsuserdefaults 中删除单个书签。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

NSUInteger row = [indexPath row];
[self.listBookamrks removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                 withRowAnimation:UITableViewRowAnimationFade];
}

最佳答案

    NSUserDefaults *userPref = [NSUserDefaults standardUserDefaults];

    NSMutableDictionary *storedBookMark = [[userPref ObjectForKey:@"bookmarks"]mutable copy];

// If you want to remove an object from the dictionary 

[storedBookMark removeObjectForKey:@"Your KEy"];

// if you want to empty the dictionary 

    [storedBookMark removeAllObjects];


    [userPref setObject: storedBookMark forKey:@"bookmarks"];

    [userPref synchronize];

// if you want to store nil // go back to default state ..


    [userPref setNilValueForKey:@"bookmarks];

关于iphone - 通过 tableview 单元格删除 NSUserDefault 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9646403/

相关文章:

iPhone UISearchBar View 没有立即更新?

ios - UISearchController 搜索栏出现但不工作/触发

ios - 在后台收到静默推送后,无法向 Alamofire 发出请求

iphone - 核心数据 : Fetching an Object from an unsaved Context

iphone - 应用程序在 xcode 中释放数组对象时崩溃

iphone - iOS 5 中的 View Controller

ios - NSFetchedResultsController(和 UITableView)委托(delegate)方法调用不断增长

ios - 异步服务调用的 TableView 单元测试

ios - NSData 和 UIImage 未在 iOS7 中发布

ios - 从 Alamofire 捕获数据