background image

@end

    listData 属性用于保存数据表中的数据,其中每一个元素都是 Note 对象,一个 Note 对象
代表数据表中的一条数据。

+ (NoteDAO*)sharedManager 方法用于获得 NoteDAO 单例对象 。

dao 组中 NoteDAO.m 代码如下:
@implementation NoteDAO
static NoteDAO *sharedManager = nil;
+ (NoteDAO*)sharedManager
{
static dispatch_once_t once;
dispatch_once(&once, ^{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date1 = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];
Note* note1 = [[Note alloc] init];
note1.date = date1;
note1.content = @”Welcome to MyNote.”;
NSDate *date2 = [dateFormatter dateFromString:@"2011-12-04 16:01:03"];
Note* note2 = [[Note alloc] init];
note2.date = date2;
note2.content = @”欢迎使用 MyNote。”;
sharedManager = [[self alloc] init];
sharedManager.listData = [[NSMutableArray alloc] init];
[sharedManager.listData addObject:note1];
[sharedManager.listData addObject:note2];
});
return sharedManager;
}
//插入 Note 方法
-(int) create:(Note*)model
{
[self.listData addObject:model];
return 0;
}
 
//删除 Note 方法
-(int) remove:(Note*)model
{
for (Note* note in self.listData) {
//比较日期主键是否相等
if ([note.date isEqualToDate:model.date]){
[self.listData removeObject: note];
break;
}