菜鸟教程小白 发表于 2022-12-12 13:47:45

ios - 如何在 Swift 上使用 RESTKIT 处理来自 JSON 的 Null 映射


                                            <p><p>当从我的服务器接收到包含空值的答案(在 JSON 响应中)时,RestKit 内部映射在处理空值时崩溃。</p>

<p>有什么办法可以克服吗?</p>

<p>我曾尝试扩展 NSNull 以包含“缺失”的方法,但这不起作用并且似乎是错误的。</p>

<p>以下是我的映射和目标代码以及 restkit 输出:</p>

<p>对象映射:</p>

<pre><code>var tempmapping = RKObjectMapping(forClass: PRLAUser.classForCoder())
      tempmapping.addAttributeMappingsFromDictionary([
            &#34;firstName&#34; : &#34;firstName&#34;,
            &#34;secondName&#34; : &#34;lastName&#34;,
            &#34;email&#34; : &#34;email&#34;,
            &#34;id&#34; : &#34;prlaToken&#34;

            ])
</code></pre>

<p>目标代码 - 至少是被映射的变量:</p>

<pre><code>var email : String?
var firstName : String?
var lastName : String?


var prlaToken : String?
</code></pre>

<p>崩溃日志:</p>

<pre><code>Parola -: unrecognized selector sent to instance 0x197b86ba0
</code></pre>

<p>2015-01-20 15:53:37.973 Parola <strong>* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-:无法识别的选择器发送到实例 0x197b86ba0”
*</strong> 首先抛出调用栈:
(0x1867fa59c 0x196f040e4 0x186801664 0x1867fe418 0x186702b6c 0x100707964 0x100023dc8 0x187605b10 0x1000a34a0 0x1000a4078 0x1000a99d4 0x1000a8dfc 0x10009a8f4 0x1000995a8 0x10009bbb4 0x10009c294 0x10009ca84 0x1875f461c 0x1000e4754 0x1000e2acc 0x1875f461c 0x1876b626c 0x1007b4df0 0x1007bf854 0x1007b8120 0x1007c175c 0x1007c2f18 0x1977252e4 0x197724fa8)
libc++abi.dylib:以 NSException 类型的未捕获异常终止</p>

<p>非常感谢您就此事提供任何帮助。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>0.23.1 版适合您。
我在 RestKit github 页面上发布了问题,也进行了临时修复。</p>

<p> <a href="https://github.com/RestKit/RestKit/issues/2155" rel="noreferrer noopener nofollow">https://github.com/RestKit/RestKit/issues/2155</a> </p>

<p>在将 NSNull 转换为 String 的对象中,我做了这样的事情</p>

<pre><code>public override func validateValue(ioValue: AutoreleasingUnsafeMutablePointer&lt;AnyObject?&gt;, forKeyPath inKeyPath: String, error outError: NSErrorPointer) -&gt; Bool {
    if let memory = ioValue.memory {
      if inKeyPath == &#34;hint&#34; &amp;&amp; ioValue.memory is NSNull {
            ioValue.memory = nil
            return true
      }
    }
    return super.validateValue(ioValue, forKeyPath: inKeyPath, error: outError)
}
</code></pre>

<p>您还可以更改 <code>RKMappingOperationDataSource</code> 以在请求时返回委托(delegate) NO </p>

<p><code>- (BOOL)mappingOperationShouldSetUnchangedValues:(RKMappingOperation *)mappingOperation;
</code></p>

<p>顺便说一句。 Stack 上已存在此问题:<a href="https://stackoverflow.com/questions/27917104/restkit-json-null-value-crash" rel="noreferrer noopener nofollow">RestKit JSON null value crash</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Swift 上使用 RESTKIT 处理来自 JSON 的 Null 映射,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/28047237/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/28047237/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Swift 上使用 RESTKIT 处理来自 JSON 的 Null 映射