重新创建NSMutableArray

时间:2014-06-11 14:09:50

标签: ios objective-c uitableview nsmutablearray nsuserdefaults

我有一个名为_objects的NSMutableArray存储在NSUserDefaults中。我在app启动时访问_objects并将它们放在带有自定义单元格的CollectionView中(见下文)。我让用户首先进入编辑模式然后点击所需的单元格来编辑每个项目。然后,单元类相应地改变视觉外观并显示UITextField。每当用户在此处输入数据时在键盘上按下完成时,ProjectsViewController(CollectionView viewcontroller)将从自定义类获取通知以重新保存项目。那我怎么能保存新的_objects呢?

具体来说,我需要编辑一个单元格(完成),编辑_objects中的相应项目并重新保存_objects。我怎样才能做到这一点?

我的自定义课程:

ProjectCell.h:

#import <UIKit/UIKit.h>

@interface ProjectCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *projectLabel;
@property (weak, nonatomic) IBOutlet UILabel *projectCount;
@property (weak, nonatomic) IBOutlet UITextField *projectTextField;
@property (weak, nonatomic) IBOutlet UILabel *editModeLabel;
@property (nonatomic, assign) BOOL editMode;;

-(void)editProject;

@end

ProjectCell.m:

#import "ProjectCell.h"
#import "QuartzCore/CALayer.h"

@implementation ProjectCell
@synthesize editMode;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
}
return self;
}

-(void)awakeFromNib {
// Custom initial code
[_projectTextField setDelegate:self];
[_projectTextField setReturnKeyType:UIReturnKeyDone];
[_projectTextField addTarget:self
                   action:@selector(editingFinished:)
         forControlEvents:UIControlEventEditingDidEndOnExit];
}

- (IBAction)editingFinished:(id)sender
{
// Update the project cell visually
_projectLabel.text = _projectTextField.text;
// disable editMode and update the project cell
editMode = NO;
_projectTextField.hidden = YES;
_editModeLabel.hidden = YES;

// Reload the MasterViewController
[[NSNotificationCenter defaultCenter] postNotificationName:@"saveNewProjects" object:self];
}

-(void)editProject {
if (editMode == YES) {
    // disable editMode and update the project cell
    editMode = NO;
    _projectTextField.hidden = YES;
    _editModeLabel.hidden = YES;
} else if (editMode == NO) {
    // Enable editMode and update the project cell
    editMode = YES;
    _projectTextField.hidden = NO;
    _editModeLabel.hidden = NO;
}
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/

@end

ProjectsViewController.m

只要用户按下&#34;完成&#34;就会调用此方法。在编辑单元格时。 我试图循环遍历每个项目并重新创建&#34; NSMutableArray,但我 不要以为这是一个非常好的方法。

- (void)resaveProjects:(NSNotification *)notification {
// Edit the edited projects and save all again
editedProjects = editedProjects - 1;

NSMutableArray *newObjects;
int objectsCount = _objects.count;
for (int i = 0; i < objectsCount; i++) {
    // Initialize objects
    if (!newObjects) {
        newObjects = [[NSMutableArray alloc] init];
    }
}
}

您认为最佳方法是什么?如何继续?

谢谢!

修改

阅读_objects:

     NSMutableArray *_objects;
    _objects = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"myProjects"]];

1 个答案:

答案 0 :(得分:0)

在iOS或Mac OS中,当您读取字典或数组(来自用户默认值或plist)等集合对象时,即使原始对象是可变的,对象也会以不可变的形式读入。

因此,在从用户默认值中读取字典后,您必须将其转换为可变对象。

我建议您从用户默认值中读取您的对象并将其转换为可变版本:

在标题中:

@property (nonatomic, strong) NSMutableDictionary *aDict;

在您的.m文件中,当您阅读字典时:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *aDict = [[defaults objectForKey: @"aDictionaryKey"] mutableCopy];

然后,当用户执行需要您更改数据的内容时:

aDict[key] = newValueFromUser;
[defaults setObject: aDict forKey: @"aDictionaryKey"];

请注意,如果您的字典包含其他集合对象(数组或其他字典),那么从文件读回时这些对象也会变为不可变。这意味着您可能需要遍历&#34;对象图&#34;你的字典,将每个集合内部转换为可变版本并将其保存在其附带的集合中。

我刚发现一些在线帖子声称NSCoding保留了对象的可变性。如果是这种情况,那么您可能希望将字典保存到存档文件,而不是将其保存为用户默认值:

将字典写入文件:

NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
  NSUserDomainMask, YES) lastObject]
NSString *path = [documentsDir stringByAppendingPathComponent: @"dictFile"];
[NSKeyedArchiver archiveRootObject: myDict toFile: path];

读回来:

NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
  NSUserDomainMask, YES) lastObject]
NSString *path = [documentsDir stringByAppendingPathComponent: @"dictFile"];
myDict = [NSKeyedUnarchiver unarchiveObjectWithFile: path];