【问题标题】:RestKit 0.20.0 rc1 - Create request post with array of StringsRestKit 0.20.0 rc1 - 使用字符串数组创建请求帖子
【发布时间】:2013-03-20 14:06:08
【问题描述】:

有没有办法在请求中创建字符串数组? 我确实找到了一些响应解决方案,但其中大多数是 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”。 “关系”是与其他托管对象的关系,“字符串”是一个属性。我想将“属性”作为属性,但无法将“属性”设置为字符串数组。所以“属性”是与具有“文本”属性的实体“子属性”的一对多关系。

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

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

【问题讨论】:

  • 您要发送的请求不是有效的 JSON 格式(请参阅jsonlint.com)。也许在发送对象之前尝试配置attribute 的内容有效?
  • JSON 只是展示我想做的一个例子。所以我知道,“一些对象数据”不是一个有效的表达方式。 ;-) 但感谢您的回答。就像你说的那样,我在发送之前配置我的内容。

标签: ios ios6 request restkit


【解决方案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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多