菜鸟教程小白 发表于 2022-12-12 17:57:48

ios - RestKit 映射错误的对象


                                            <p><p>我正在使用 RestKit,这就是我初始化它并添加路由和描述符的方式:</p>

<pre><code> - (void)initRestClient
{
   NSURL *baseURL = ;
   self.manager = ;
   ;
   ;
   .responseDescriptors];
   .requestDescriptors];
   ;
   ;

   //    AFHTTPClient *client = .HTTPClient;

   ;
   ;
}

- (void)initRoutes
{   
   RKRoute *bannersRoute = pathPattern:@&#34;Banners?categoryID=:categoryID&#34; method:RKRequestMethodGET];
   bannersRoute.shouldEscapePath = YES;
   ;

   RKRoute *branchesRoute = pathPattern:@&#34;Branches?city=:city&amp;type=:type&#34; method:RKRequestMethodGET];
   branchesRoute.shouldEscapePath = YES;
   ;

   RKRoute *shortTokenRoute = pathPattern:@&#34;users/login/quick/shortToken?phone=:phone&amp;extraCode=:extraCode&#34; method:RKRequestMethodGET];
   shortTokenRoute.shouldEscapePath = YES;
   ;

   RKRoute *longTokenRoute = pathPattern:@&#34;users/login/quick/userDetails?shortToken=:shortToken&#34; 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:@[@&#34;idNum&#34;, @&#34;name&#34;, @&#34;addressURL&#34;, @&#34;bannerPosition&#34;, @&#34;imageALT&#34;, @&#34;imageHeight&#34;,
                                                 @&#34;imageURL&#34;, @&#34;imageWidth&#34;, @&#34;subtitle&#34;, @&#34;targetURL&#34;, @&#34;textURL&#34;, @&#34;title&#34;]
                                       forKeys:@[@&#34;ID&#34;, @&#34;Name&#34;, @&#34;AddressURL&#34;, @&#34;BannerPosition&#34;, @&#34;ImageALT&#34;, @&#34;ImageHeight&#34;,
                                                 @&#34;ImageURL&#34;, @&#34;ImageWidth&#34;, @&#34;SubTitle&#34;, @&#34;TargetURL&#34;, @&#34;TextURL&#34;, @&#34;Title&#34;]];
}
@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:@[@&#34;idNum&#34;, @&#34;branchTitle&#34;, @&#34;address&#34;, @&#34;branchType&#34;, @&#34;city&#34;, @&#34;fax&#34;,
                                                 @&#34;isCanOrder&#34;, @&#34;isMe&#34;, @&#34;openHours&#34;, @&#34;orderCode&#34;, @&#34;phone&#34;, @&#34;remarks&#34;]
                                       forKeys:@[@&#34;ID&#34;, @&#34;Name&#34;, @&#34;Address&#34;, @&#34;BranchType&#34;, @&#34;City&#34;, @&#34;Fax&#34;,
                                                 @&#34;IsCanOrder&#34;, @&#34;IsMe&#34;, @&#34;OpenHours&#34;, @&#34;OrderCode&#34;, @&#34;Phone&#34;, @&#34;Remarks&#34;]];
}
@end

@interface RKShortToken : NSObject
@property (strong, nonatomic) NSString *responseError;
@property (strong, nonatomic) NSString *shortToken;
+ (NSDictionary*)getAttributes;
@end

@implementation RKShortToken
+ (NSDictionary*)getAttributes
{
    return
                                       forKeys:@[@&#34;responseError&#34;, @&#34;shortToken&#34;]];
}
@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:@[@&#34;responseError&#34;, @&#34;responseMessage&#34;, @&#34;responseHttpCode&#34;, @&#34;responseUserMessage&#34;,
                                                 @&#34;abroadInd&#34;, @&#34;accountType&#34;, @&#34;customerID&#34;, @&#34;longToken&#34;]
                                       forKeys:@[@&#34;responseError&#34;, @&#34;responseError.DeveloperMessage&#34;, @&#34;responseError.HttpCode&#34;, @&#34;responseError.UserMessage&#34;,
                                                 @&#34;abroadInd&#34;, @&#34;accountType&#34;, @&#34;customerId&#34;, @&#34;longToken&#34;]];
}
@end
</code></pre>

<p>当我尝试调用 RKShortToken 时,我得到了很好的响应,但是我得到的对象是 RKLongToken:</p>

<pre><code>- (void)quickLoginWithTelephone:(NSString*)telephone extraCode:(NSString *)extraCode completionBlock:(quickLoginExtraCodeCompletionBlock)success
{
    NSDictionary *params = @{ @&#34;phone&#34; : telephone, @&#34;extraCode&#34; : extraCode };
    [ getObjectsAtPath:@&#34;users/login/quick/shortToken?&#34;
                                           parameters:params
                                              success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                                NSLog(@&#34;%@&#34;, operation.HTTPRequestOperation.responseString);
                                                NSLog(@&#34;%@&#34;, mappingResult.array);
                                                success(YES);
                                              } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                NSLog(@&#34;Error occurred.&#34;);
                                              }];
}
</code></pre>

<p>这是日志:</p>

<pre><code>2014-01-06 15:46:01.428 Online I restkit.network:RKObjectRequestOperation.m:180 GET &#39;http://10.174.10.35/service/service.svc/users/login/quick/shortToken?&amp;extraCode=null&amp;phone=0505717596&#39;

2014-01-06 15:50:03.999 Online {&#34;responseError&#34;:null,&#34;shortToken&#34;:&#34;I55933325601458654742&#34;}
2014-01-06 15:46:01.577 Online (
   &#34;&lt;RKLongToken: 0x14dd3a50&gt;&#34;
)

2014-01-06 15:46:04.490 Online I restkit.network:RKObjectRequestOperation.m:250 GET &#39;http://10.174.10.35/service/service.svc/users/login/quick/shortToken?&amp;extraCode=null&amp;phone=0505717596&#39; (200 OK / 1 objects)
</code></pre>

<p>这里似乎有什么问题?</p>

<p>更新:</p>

<p>我已按照@Wain 告诉我的那样将 <code>pathPattern</code> 添加到描述符中,但现在我遇到了错误。它看起来像这样:</p>

<pre><code>&#34;No mappable object representations were found at the key paths searched.&#34;

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]
查看完整版本: ios - RestKit 映射错误的对象