iphone - 从 NSUserDefaults 中删除数组时出现内存问题

标签 iphone objective-c ios xcode

我的目标是能够更新 NSUserDefaults 中的对象。

这是我的方法,我担心的是,当我使用 removeObjectForKey 替换数组时,是否存在无法创建的内存问题。数组中的对象通过 initWithArray: 保存在 objectArray 中。 oldSavedArray 是包含 NSUserDefaults 先前内容的数组。

objectArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];

if ([objectArray containsObject:myObject]){

            // remove the current object
            [objectArray removeObject:_currentFood];

            // reset our new array of objects
            [_defaults removeObjectForKey:@"mySavedData"];
            [_defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:objectArray] forKey:@"mySavedData"];
        }
        else{ 

            // add the current object
            [objectArray addObject:_currentFood];                

            // reset our new array of favorite food
            [_defaults removeObjectForKey:@"mySavedData"];
            [_defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:objectArray] forKey:@"mySaveData"];
        }

最佳答案

回答你的问题

Is there any memory issue by using "removeObjectForKey"?.

我的回答是否定的。但这一步不是必需的。

[defaults setObject:forKey:]

足够了。

关于iphone - 从 NSUserDefaults 中删除数组时出现内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9746558/

相关文章:

iphone - NSString drawInRect 与 Core Text

ios - 如何防止 UITabBar 展开转场?

ios - 当取消处理程序引用 self 时,在 -dealloc 中取消 XPC 连接

ios - 是否可以将所有 iOS 应用程序数据存储在 Library/Preferences 文件夹中?

ios - 如何检查 array 在 ios sdk 中是否为 null 或为空?

iphone - NSURL URLWithString : giving nil

ios - 不越狱创建新的iOS语言需要哪些步骤

ios - 检查给定的日期时间是否在 Objective C 中的其他两个日期时间之间

ios - Swift的JSON数据建模

ios - 搜索栏在 segue 到新的 ViewController 后不会消失