给出以下简单模型类(基于 Mantle):

// .h
#import <Mantle.h>

@interface JAIInterestingPhonesCategory : MTLModel <MTLJSONSerializing>

@property (copy, nonatomic, readonly) NSString *categoryId;
@property (copy, nonatomic, readonly) NSString *title;

@end

// .m
#import "JAIInterestingPhonesCategory.h"

@implementation JAIInterestingPhonesCategory

+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{
             @"categoryId"  : @"id",
             };
}

@end

我创建以下测试用例:

#import <XCTest/XCTest.h>
#import "JAIInterestingPhonesCategory.h"

@interface JAIInterestinPhoneTestCase : XCTestCase

@end

@implementation JAIInterestinPhoneTestCase

- (void)setUp
{
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown
{
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testCreateModelWithJSONDictionary
{
    NSDictionary *JSONModelDictionary = @{
                                          @"id"     : @"catId",
                                          @"title"  : @"Category title"
                                          };

    NSError *error;
    JAIInterestingPhonesCategory *category = [MTLJSONAdapter modelOfClass:[JAIInterestingPhonesCategory class] fromJSONDictionary:JSONModelDictionary error:&error];
    XCTAssertNotNil(category, @"The instantiated category must not be nil");

}

@end

我收到以下运行时错误:

*** Assertion failure in -[MTLJSONAdapter initWithJSONDictionary:modelClass:error:] Invalid parameter not satisfying: [modelClass isSubclassOfClass:MTLModel.class]

而且,正如你所看到的, modelClass(又名 JAIInterestingPhonesCategory) 是一个子类 MTLModel.

我已将 Mantle 作为 Pod 添加到项目中。

知道这里发生了什么吗?谢谢你!!!

有帮助吗?

解决方案

这里的问题是 Mantle 被注入到您的测试和主要目标中。

这里的解决方案是相应地更改你的 podfile:

platform :ios, '7.0'

target :app do
    pod 'Mantle', '~> 1.4'
end

target :appTests do
  pod 'Expecta',     '~> 0.3'
end

查看 https://github.com/Mantle/Mantle/issues/217 了解更多信息。祝你好运!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top