【问题标题】:Using MagicalRecord, how do I get data out of NSArray and into a NSManagedObject?使用 MagicalRecord,如何将数据从 NSArray 中取出并放入 NSManagedObject?
【发布时间】:2013-02-28 21:30:15
【问题描述】:

我正在使用 MagicalRecord 来处理 Core Data 对象。我在使用 MR 将它们放入的 NSArray 检索核心数据对象时遇到问题……我似乎无法将数据从数组中取出并放入 NSManagedObject 中,因此我可以使用它。这是我的代码:

        //  create the predicate
    NSArray *apptDataArray = [NSMutableArray new];
    NSPredicate *predicate =  ([NSPredicate predicateWithFormat:@"((aStartTime > %@) AND (aStartTime <= %@))", startDate, endDate]);

    //  find all appointments with that selected date
    apptDataArray = [AppointmentInfo MR_findAllWithPredicate:predicate];

    //  now find the appointments for the selected date and put them in the schedule
    if(apptDataArray.count > 0) {
        for (NSManagedObject *AppointmentInfo in apptDataArray) {
            NSLog(@"\n\n-->apptDataArray: %@", apptDataArray);

        }
    }

这是另一个类中的 AppointmentInfo 的定义:

@interface AppointmentInfo : NSManagedObject

@property (nonatomic, retain) NSDate * aStartTime;
@property (nonatomic, retain) NSDate * aEndTime;
@property (nonatomic, retain) NSString * aServiceTech;
@property (nonatomic, retain) NSString * aApptKey;
@property (nonatomic, retain) NSDate *aShortDate;

不知何故,我需要获取返回数组中的数据并将其放在 AppointmentInfo 中。我已经尝试了各种排列,查看了谷歌和 SO,但我找不到任何东西。我难住了!我该怎么做?

【问题讨论】:

  • 失败的具体症状是什么?
  • 这不是失败...我只是不知道如何将 MR 返回的 NSArray 数据获取到 AppointmentInfo 中,以便获取单独的字段(即 aStartTime、aEndTime)并使用它们。

标签: objective-c core-data magicalrecord


【解决方案1】:

我不确定我是否正确理解了您的问题,但是 获取请求的结果apptDataArray 只是AppointmentInfo 对象的数组,因此您可以使用

AppointmentInfo *appt = [apptDataArray objectAtIndex:i]; // 0 <= i < apptDataArray.count

或用

枚举结果数组的所有对象
for (AppointmentInfo *appt in apptDataArray) {
    NSLog(@"startTime=%@, endTime=%@", appt.aStartTime, appt.aEndTime);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多