0

有没有办法在请求中创建字符串数组?我确实找到了一些响应解决方案,但其中大多数是 restkit 0.10.x 或者我不理解它们。(例如,通过 ID 数组映射 RestKit 中的关系不起作用

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

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

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

我的休息套件映射: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"
}

但这不是我需要的。请你帮助我好吗?

4

1 回答 1

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];
于 2013-03-22T08:14:48.747 回答