首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS Mantle +大衣

iOS Mantle +大衣
EN

Stack Overflow用户
提问于 2014-08-12 03:25:56
回答 1查看 2.5K关注 0票数 5

我正在看大衣库,从我收集的资料来看,它是一个扩展Mantle图书馆的图书馆。

地幔:https://github.com/Mantle/Mantle/大衣:https://github.com/gonzalezreal/Overcoat

Mantle和大衣github页面不断提到创建Mantle模型,但我想知道如何生成Mantle模型?我是手动输入它,还是使用Xcode xcdatamodel文件可视化地构建它,然后生成子程序+修改该文件?

在create中,使用Interface在xcdatamodel文件中创建实体,然后使用Xcode的编辑器> Create NSManagedObject子类。

我们是否对Mantle做了同样的操作,然后从NSManagedObject更改为MTLModel?

当我们决定更新xcdatamodel文件中的Core数据实体时会发生什么?如果我们再次重新生成模型文件,难道不需要将所有这些更改重新添加到NSManagedObject类中吗?

对这个过程非常困惑。

EN

回答 1

Stack Overflow用户

发布于 2014-08-12 09:19:36

好吧,我想我开始更多地理解它了。经过几个小时的尝试和错误,我能够得到一个基本的大衣演示应用程序与核心数据从我的REST提取。

我让它像这样工作:

1)我们在xcdatamodel文件中创建实体,但不使用Editor > create NSManagedObject类菜单生成NSManagedObject类。

2)根据普通的右单击项目文件夹(在Xcode中)> New >选择MTLModel作为子类创建Mantle模型子类,然后手动输入属性。特别要注意的是,子类标题应该类似于:

代码语言:javascript
复制
@interface Book : MTLModel <MTLJSONSerializing, MTLManagedObjectSerializing> 

@property (nonatomic, copy) NSString *title;
...

@end

3)如果您意外地生成了像我这样的核心数据实体,那么xcdatamodel文件实际上在xcdatamodel中的"Configuration“下的”默认“部分中添加了类名。

您需要删除"Class“列中的任何值,否则会导致严重的崩溃:

代码语言:javascript
复制
"XYZ" is not a subclass of NSManagedObject.

4)确保在Mantle模型类中为MTLJSONSerialization和MTLManagedObjectSerializing实现序列化方法。

代码语言:javascript
复制
#pragma mark - MTLJSONSerialization -

+(NSDictionary *)JSONKeyPathsByPropertyKey
{    
    return @{
             @"title": @"title",
             ...
             };
}

#pragma mark - MTLManagedObjectSerializing -

+(NSString *)managedObjectEntityName
{
    // ------------------------------------------------
    // If you have a Core Data entity called "Book"
    // then you return @"Book";
    //
    // Don't return the Mantle model class name here.
    // ------------------------------------------------
    return @"TheCoreDataEntityName";
}

+(NSDictionary *)managedObjectKeysByPropertyKey
{
    // ------------------------------------------------
    // not really sure what this does, I just put 
    // it in as the example does it too
    // ------------------------------------------------
    return @{};
}

这些方法本质上是将JSON响应从服务器映射到Core数据实体的胶水。

5)让我感到更重要的是服务器返回响应的方式。

您的服务器可能使用HTTP状态代码,而没有顶级JSON字典

例如:

代码语言:javascript
复制
// no top level JSON dictionary, purely just an array of results
{
    {
        title: "ABC",
        ...
    },
    {
        title: "ABC",
        ...
    },
    {
        title: "ABC",
        ...
    },
}

然而,其他类型的REST服务器可能返回一个顶级JSON字典,其结果键路径位于子级别,如下所示:

代码语言:javascript
复制
{
    count: 20,
    next: "http://www.server.com/api/resource?page=2",
    previous: null,
    results:(
    {
        title: "ABC",
        ...
    },
    {
        title: "ABC",
        ...
    },
    {
        title: "ABC",
        ...
    })
}

在后一种情况下,从我的模糊理解来看,这是一种被称为“信封”式的反应。对于这些类型的服务器响应,还有一个额外的步骤,它涉及到告诉覆盖层结果键路径数组在JSON响应中的位置。

为此,我们需要:

( 5a)创建一个ServerResponse类,它是OVCresponse的一个子类:

代码语言:javascript
复制
// .h file
#import "OVCResponse.h"

@interface ServerResponse : OVCResponse

@end

// .m file
@implementation ServerResponse

+(NSString *)resultKeyPathForJSONDictionary:(NSDictionary *)JSONDictionary
{
    // --------------------------------------------------------------------
    // we're telling Overcoat, the array of entities is found under the
    // "results" key-value pair in the server response JSON dictionary
    // --------------------------------------------------------------------
    return @"results";
}

@end

5b)在APIClient类(应该是OVCHTTPSessionManager的子类)中,重写方法:

代码语言:javascript
复制
+(Class)responseClass
{
    // --------------------------------------------------
    // ServerResponse class will let Overcoat know 
    // where to find the results array
    // --------------------------------------------------
    return [ServerResponse class];
}

希望这能帮助其他人,谁有同样的问题试图让大衣工作。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25255671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档