ios - RestKit 映射错误的对象
<p><p>我正在使用 RestKit,这就是我初始化它并添加路由和描述符的方式:</p>
<pre><code> - (void)initRestClient
{
NSURL *baseURL = ;
self.manager = ;
;
;
.responseDescriptors];
.requestDescriptors];
;
;
// AFHTTPClient *client = .HTTPClient;
;
;
}
- (void)initRoutes
{
RKRoute *bannersRoute = pathPattern:@"Banners?categoryID=:categoryID" method:RKRequestMethodGET];
bannersRoute.shouldEscapePath = YES;
;
RKRoute *branchesRoute = pathPattern:@"Branches?city=:city&type=:type" method:RKRequestMethodGET];
branchesRoute.shouldEscapePath = YES;
;
RKRoute *shortTokenRoute = pathPattern:@"users/login/quick/shortToken?phone=:phone&extraCode=:extraCode" method:RKRequestMethodGET];
shortTokenRoute.shouldEscapePath = YES;
;
RKRoute *longTokenRoute = pathPattern:@"users/login/quick/userDetails?shortToken=:shortToken" method:RKRequestMethodGET];
longTokenRoute.shouldEscapePath = YES;
;
}
- (void)initMappingObjectsAndDiscriptors
{
RKObjectMapping *bannerMapping = ];
];
RKResponseDescriptor *bannerDescriptor = ;
RKObjectMapping *branchesMapping = ];
];
RKResponseDescriptor *branchesDescriptor = ;
RKObjectMapping *shortTokenMapping = ];
];
RKResponseDescriptor *shortTokenDescriptor = ;
RKObjectMapping *longTokenMapping = ];
];
// longTokenMapping.setDefaultValueForMissingAttributes = YES;
// longTokenMapping.setNilForMissingRelationships = YES;
RKResponseDescriptor *longTokenDescriptor = ;
];
}
</code></pre>
<p>这是每个类的构建方式:</p>
<pre><code>@interface RKBanner : NSObject
@property (strong, nonatomic) NSNumber *idNum;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *addressURL;
@property (strong, nonatomic) NSString *bannerPosition;
@property (strong, nonatomic) NSString *imageALT;
@property (strong, nonatomic) NSNumber *imageHeight;
@property (strong, nonatomic) NSString *imageURL;
@property (strong, nonatomic) NSNumber *imageWidth;
@property (strong, nonatomic) NSString *subtitle;
@property (strong, nonatomic) NSString *targetURL;
@property (strong, nonatomic) NSString *textURL;
@property (strong, nonatomic) NSString *title;
+ (NSDictionary*)getAttributes;
@end
@implementation RKBanner
+ (NSDictionary*)getAttributes
{
return [NSDictionary dictionaryWithObjects:@[@"idNum", @"name", @"addressURL", @"bannerPosition", @"imageALT", @"imageHeight",
@"imageURL", @"imageWidth", @"subtitle", @"targetURL", @"textURL", @"title"]
forKeys:@[@"ID", @"Name", @"AddressURL", @"BannerPosition", @"ImageALT", @"ImageHeight",
@"ImageURL", @"ImageWidth", @"SubTitle", @"TargetURL", @"TextURL", @"Title"]];
}
@end
@interface RKBranches : NSObject
@property (strong, nonatomic) NSNumber *idNum;
@property (strong, nonatomic) NSString *branchTitle;
@property (strong, nonatomic) NSString *address;
@property (strong, nonatomic) NSNumber *branchType;
@property (strong, nonatomic) NSString *city;
@property (strong, nonatomic) NSString *fax;
@property (assign, nonatomic) BOOL isCanOrder;
@property (assign, nonatomic) BOOL isMe;
@property (strong, nonatomic) NSString *openHours;
@property (strong, nonatomic) NSString *orderCode;
@property (strong, nonatomic) NSString *phone;
@property (strong, nonatomic) NSString *remarks;
+ (NSDictionary*)getAttributes;
@end
@implementation RKBranches
+ (NSDictionary*)getAttributes
{
return [NSDictionary dictionaryWithObjects:@[@"idNum", @"branchTitle", @"address", @"branchType", @"city", @"fax",
@"isCanOrder", @"isMe", @"openHours", @"orderCode", @"phone", @"remarks"]
forKeys:@[@"ID", @"Name", @"Address", @"BranchType", @"City", @"Fax",
@"IsCanOrder", @"IsMe", @"OpenHours", @"OrderCode", @"Phone", @"Remarks"]];
}
@end
@interface RKShortToken : NSObject
@property (strong, nonatomic) NSString *responseError;
@property (strong, nonatomic) NSString *shortToken;
+ (NSDictionary*)getAttributes;
@end
@implementation RKShortToken
+ (NSDictionary*)getAttributes
{
return
forKeys:@[@"responseError", @"shortToken"]];
}
@end
@interface RKLongToken : NSObject
@property (strong, nonatomic) NSString *responseError;
@property (strong, nonatomic) NSString *responseMessage;
@property (strong, nonatomic) NSString *responseHttpCode;
@property (strong, nonatomic) NSString *responseUserMessage;
@property (strong, nonatomic) NSString *abroadInd;
@property (strong, nonatomic) NSString *accountType;
@property (strong, nonatomic) NSString *customerID;
@property (strong, nonatomic) NSString *longToken;
+ (NSDictionary*)getAttributes;
@end
@implementation RKLongToken
+ (NSDictionary*)getAttributes
{
return [NSDictionary dictionaryWithObjects:@[@"responseError", @"responseMessage", @"responseHttpCode", @"responseUserMessage",
@"abroadInd", @"accountType", @"customerID", @"longToken"]
forKeys:@[@"responseError", @"responseError.DeveloperMessage", @"responseError.HttpCode", @"responseError.UserMessage",
@"abroadInd", @"accountType", @"customerId", @"longToken"]];
}
@end
</code></pre>
<p>当我尝试调用 RKShortToken 时,我得到了很好的响应,但是我得到的对象是 RKLongToken:</p>
<pre><code>- (void)quickLoginWithTelephone:(NSString*)telephone extraCode:(NSString *)extraCode completionBlock:(quickLoginExtraCodeCompletionBlock)success
{
NSDictionary *params = @{ @"phone" : telephone, @"extraCode" : extraCode };
[ getObjectsAtPath:@"users/login/quick/shortToken?"
parameters:params
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", operation.HTTPRequestOperation.responseString);
NSLog(@"%@", mappingResult.array);
success(YES);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Error occurred.");
}];
}
</code></pre>
<p>这是日志:</p>
<pre><code>2014-01-06 15:46:01.428 Online I restkit.network:RKObjectRequestOperation.m:180 GET 'http://10.174.10.35/service/service.svc/users/login/quick/shortToken?&extraCode=null&phone=0505717596'
2014-01-06 15:50:03.999 Online {"responseError":null,"shortToken":"I55933325601458654742"}
2014-01-06 15:46:01.577 Online (
"<RKLongToken: 0x14dd3a50>"
)
2014-01-06 15:46:04.490 Online I restkit.network:RKObjectRequestOperation.m:250 GET 'http://10.174.10.35/service/service.svc/users/login/quick/shortToken?&extraCode=null&phone=0505717596' (200 OK / 1 objects)
</code></pre>
<p>这里似乎有什么问题?</p>
<p>更新:</p>
<p>我已按照@Wain 告诉我的那样将 <code>pathPattern</code> 添加到描述符中,但现在我遇到了错误。它看起来像这样:</p>
<pre><code>"No mappable object representations were found at the key paths searched."
NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: Banners, Branches, BranchTypes, Cities, MenuList
The representation inputted to the mapper was found to contain nested object representations at the following key paths: responseError, shortToken
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}`
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您的所有响应描述符都有 <code>pathPattern:nil</code>,因此 RestKit 无法过滤并将应用所有这些描述符并查看会发生什么。因此,对于所有 JSON,您将获得多个对象,其中仅包含与 JSON 匹配的每个映射的部分。</p>
<p>要解决此问题,请添加路径模式以允许 RestKit 确定哪个响应描述符与您的每个请求匹配。</p></p>
<p style="font-size: 20px;">关于ios - RestKit 映射错误的对象,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20951445/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20951445/
</a>
</p>
页:
[1]