1

我正在尝试将我的核心数据绑定到 NSTableView。我从 API 获取信息,然后想将其添加到 NSTableView。看起来它设置正确,因为每次我让它调用 API 并获取信息时,都会在 NSTableView 数据中添加一个空行。

为什么它添加一个空行而不是我绑定的数据?

应用控制器.h

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;

然后我在自动合成器的地方使用新的 Xcode。

项目.h

@class TimeLog;

@interface Items : NSManagedObject

@property (nonatomic, retain) NSNumber * itemId;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * itemType;
@property (nonatomic, retain) TimeLog *relationship;

@end

项目.m

@implementation Items

@dynamic itemId;
@dynamic title;
@dynamic itemType;
@dynamic relationship;

@end

项目对象.h

@interface ItemObject : NSObject

@property (nonatomic, retain) NSString * itemId;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * itemType;

@end

项目对象.m

@implementation ItemObject

@end

方法制作 API 调用

此方法进行 API 调用并将其添加到临时对象。然后它将该临时对象添加到核心数据中。

+ (void)searchForItemByType:(NSString *)itemType andId:(NSString *)searchId
{
    NSLog(@"Search Feature By ID: %@", searchId);
    RKObjectMapping *itemMapping = [RKObjectMapping mappingForClass:[ItemObject class]];

    [itemMapping addAttributeMappingsFromDictionary:@{
        @"id": @"itemId",
        @"name": @"title",
        @"item_type": @"itemType"
     }];


    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:itemMapping pathPattern:nil keyPath:@"data" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
    // The entire value at the source key path containing the errors maps to the message
    [errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];
    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError);
    // Any response in the 4xx status code range with an "errors" key path uses this mapping
    RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping pathPattern:nil keyPath:@"error_description" statusCodes:statusCodes];
    RKObjectManager *manager = [RKObjectManager sharedManager];
    NSLog(@"HTTP Client: %@", manager.HTTPClient);

    [manager addResponseDescriptorsFromArray:@[ responseDescriptor, errorDescriptor ]];
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"false", @"with_lock"
                            , nil];
    NSString *path = [NSString stringWithFormat:@"/api/v1/%@/%@", [itemType lowercaseString], searchId];
    NSLog(@"Manager: %@", manager);
    [manager getObjectsAtPath:path parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {

        NSLog(@"Results: %@", [result firstObject]);
        Items *insertItem = [NSEntityDescription insertNewObjectForEntityForName:@"Items" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
        insertItem = [result firstObject];
        NSLog(@"Name: %@", [insertItem title]);

        // Handled with articleDescriptor
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        // Transport error or server error handled by errorDescriptor
        NSLog(@"Error: %@", [error localizedDescription]);
        NSAlert *alert = [NSAlert alertWithMessageText:@"Error" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@", [error localizedDescription]];
        [alert runModal];

    }];
}

从上面的代码记录

2013-03-15 10:15:21.817 Project[59074:403] Results: <ItemObject: 0x1034ab360>
2013-03-15 10:15:21.818 Project[59074:403] ManagedObjectContext
2013-03-15 10:15:21.818 Project[59074:403] Name: Custom Mod is missing from Face Lift

国际文凭组织

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

1

我认为问题可能出在这段代码中:

NSLog(@"Results: %@", [result firstObject]);
Items *insertItem = [NSEntityDescription insertNewObjectForEntityForName:@"Items" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
insertItem = [result firstObject];
NSLog(@"Name: %@", [insertItem title]);

在您的日志中,看起来 [result firstObject] 是“ItemObject”类而不是“Items”类的一部分。即使它们共享相同的结构,“ItemObject”也不会从 NSManagedObject 继承,而是被分配给一个。系统不知道如何将“ItemObject”对象转换为“Items”对象,因此它只是将 insertItem 中的所有值保留为空白,这将转换为显示在表中的空白行。试试这个:

Items *insertItem = [NSEntityDescription insertNewObjectForEntityForName:@"Items" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
ItemObject *tempObject = [result firstObject];
insertItem.itemID = tempObject.itemID;
insertItem.title = tempObject.title;
insertItem.itemType = tempObject.itemType;
于 2013-03-15T19:22:25.387 回答