RestKit 0.20.0 rc1 - 使用字符串数组创建请求发布

时间:2013-03-20 14:06:08

标签: ios ios6 request restkit

有没有办法在请求中创建字符串数组? 我确实找到了一些响应的解决方案,但其中大多数是restkit 0.10.x或者我不理解它们。 (例如Mapping relationships in RestKit through an array of IDs doesn't work

这是我的设定: 我使用iOS 6与核心数据和托管对象。我的要求应该是

{
 "attribute":["some text", "some more text", "and one more text"],
 "relationship":[some object data],
 "string":"some text"
}

该实体名为“doRequest”。 “Relationship”是与其他托管对象的关系,“string”是属性。我想将“attribute”作为属性,但是没有办法将“attribute”设置为字符串数组。因此,“属性”是与具有属性“文本”的实体“子属性”的一对多关系。

我的休息套件映射:     RKObjectMapping * subAttributeMapping = [RKObjectMapping requestMapping];     [subAttributeMapping addAttributeMappingsFromArray:@ [@“text”]];

RKObjectMapping *doRequestMapping = [RKObjectMapping requestMapping];
[doRequestMapping addAttributeMappingsFromDictionary:@{
 @"string": @"string"}];

[doRequestMapping addRelationshipMappingWithSourceKeyPath:@"attribute" mapping:subAttributeMapping];
[doRequestMapping addRelationshipMappingWithSourceKeyPath:@"relationship" mapping:relationshipMapping];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:doRequestMapping objectClass:[DoRequest class] rootKeyPath:nil];

[objectManager addRequestDescriptor:requestDescriptor];

Restkit将其映射为:

{
 "attribute":[{"text":"some text"}, {"text":"some more text"}, {"text":"and one more text"}],
 "relationship":[some object data],
 "string":"some text"
}

但这不是我需要的。 你能帮我吗?

1 个答案:

答案 0 :(得分:1)

所以,我通过仅使用不使用核心数据的对象来修复它。 我的对象现在有一个新属性NSArray *attributeArray来restkit让我们创建一个字符串数组。

我的DoRequest.h:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface DoRequest : NSManagedObject

@property (nonatomic, retain) NSString * string;
@property (nonatomic, retain) NSSet * relationship;
@property (nonatomic, retain) NSSet *attribute;
@property (nonatomic, retain) NSArray *attributeArray;
@end

@interface DoRequest (CoreDataGeneratedAccessors)

- (void)addRelationshipObject:(RelationshipReference *)value;
- (void)removeRelationshipObject:(RelationshipReference *)value;
- (void)addRelationship:(NSSet *)values;
- (void)removeRelationship:(NSSet *)values;

- (void)addAttributeObject:(NSManagedObject *)value;
- (void)removeAttributeObject:(NSManagedObject *)value;
- (void)addAttribute:(NSSet *)values;
- (void)removeAttribute:(NSSet *)values;

@end

我的DoRequest.m:

#import "DoRequest.h"
#import "RelationshipReference.h"
#import "Attribute.h"

@implementation DoRequest

@dynamic string;
@dynamic relationship;
@dynamic attribute;
@dynamic attributeArray;

- (NSArray *)attributeArray
{
    NSMutableArray *array = [[NSMutableArray alloc] init];

    for (Attribute.h *object in self.attribute) {
        [array addObject:object.attributeName];
    }

    return [array copy];
}

@end

我的对象映射器如下所示:

RKObjectMapping *doRequestMapping = [RKObjectMapping requestMapping];
[doRequestMapping addAttributeMappingsFromDictionary:@{
 @"string": @"string",
 @"attributeArray": @"attribute"}];

[doRequestMapping addRelationshipMappingWithSourceKeyPath:@"relationship" mapping:[self relationshipReferenceObjectMapping]];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:doRequestMapping objectClass:[DoQueryRequest class] rootKeyPath:nil];

[objectManager addRequestDescriptor:requestDescriptor];