地幔+核心数据:此类不是密钥的密钥值编码兼容

时间:2015-12-26 15:11:47

标签: ios json cocoa-touch core-data github-mantle

我试图使用Mantle和Core Data为reddit构建一个客户端,但我不断被*** Caught exception setting key "upvotes" : [<Thread 0x14c61ba10> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key upvotes.抛出,我无法弄清楚错误是什么...... < / p>

我不认为我错过了Mantle文档中的任何内容。

发+ CoreDataProperties.h

#import "Thread.h"

NS_ASSUME_NONNULL_BEGIN

@interface Thread (CoreDataProperties)

@property (nullable, nonatomic, retain) NSString *approvedBy;
@property (nullable, nonatomic, retain) NSNumber *isArchived;
@property (nullable, nonatomic, retain) NSString *author;
@property (nullable, nonatomic, retain) NSString *title;
@property (nullable, nonatomic, retain) NSDate *createdDate;
@property (nullable, nonatomic, retain) NSString *subreddit;
@property (nullable, nonatomic, retain) NSString *domain;
@property (nullable, nonatomic, retain) NSNumber *upvotes;

@end

NS_ASSUME_NONNULL_END

发+ CoreDataProperties.m

#import "Thread+CoreDataProperties.h"

@implementation Thread (CoreDataProperties)

@dynamic approvedBy;
@dynamic isArchived;
@dynamic author;
@dynamic title;
@dynamic createdDate;
@dynamic subreddit;
@dynamic domain;
@dynamic upvotes;

@end

Thread.h

@import Foundation;
@import CoreData;
@import Mantle;
@import MTLManagedObjectAdapter;

NS_ASSUME_NONNULL_BEGIN

@interface Thread : MTLModel <MTLJSONSerializing, MTLManagedObjectSerializing>

// Insert code here to declare functionality of your managed object subclass

@end

NS_ASSUME_NONNULL_END

#import "Thread+CoreDataProperties.h"

Thread.m

#import "Thread.h"
#import <Mantle/MTLValueTransformer.h>

@implementation Thread

#pragma mark MTLManagedObjectSerializing Protocols

+ (NSString *)managedObjectEntityName
{
    return @"Thread";
}

+ (NSDictionary *)managedObjectKeysByPropertyKey
{
    return @{
             @"approvedBy": @"approvedBy",
             @"isArchived": @"isArchived",
             @"author": @"author",
             @"title": @"title",
             @"createdDate": @"createdDate",
             @"subreddit": @"subreddit",
             @"domain": @"domain",
             @"upvotes": @"upvotes"
             };
}

+ (NSSet *)propertyKeysForManagedObjectUniquing {
    return [NSSet setWithObjects:@"approvedBy",@"isArchived",@"author",@"title",@"createdDate",@"subreddit",@"domain",@"upvotes", nil];
}

#pragma mark MTLJSONSerializing Protocols

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"approvedBy": @"approved_by",
             @"isArchived": @"archived",
//             @"author": @"author",
//             @"title": @"title",
             @"createdDate": @"created",
//             @"subreddit": @"subreddit",
//             @"domain": @"domain",
             @"upvotes": @"ups"
             };
}

+ (NSValueTransformer *)JSONTransformerForKey:(NSString *)key {
    if ([key isEqualToString:@"createdDate"]) {
        return [Thread dateJSONTransformer];
    }

    return nil;
}

+ (NSValueTransformer *)dateJSONTransformer {
    return [MTLValueTransformer transformerUsingForwardBlock:^id(NSNumber *unixTimestamp, BOOL *success, NSError *__autoreleasing *error) {
        return [NSDate dateWithTimeIntervalSince1970:unixTimestamp.doubleValue];
    } reverseBlock:^id(NSDate *date, BOOL *success, NSError *__autoreleasing *error) {
        return [NSNumber numberWithDouble:[date timeIntervalSince1970]];
    }];
}

@end

1 个答案:

答案 0 :(得分:0)

我无法看到您Thread课程的声明与NSManagedObject的关系。您已将其重新声明为MTLModel,它是NSObject的子类。当然它没有upvotesCoreDataProperties类别中定义的任何其他属性,因为这些属性是动态的,并且没有Core Data运行时支持来为它们提供实现。

您应该将更改撤消到Thread.h,或者只删除这四个文件并使用Xcode重新创建它们。

关于Mantle:我不认为它直接支持Core Data。请参阅here