菜鸟教程小白 发表于 2022-12-13 15:08:40

ios - 在 Sqlite 中存储 NSMutableArray


                                            <p><p>我有一个 NSMutableArray,其中包含多个 CGRect 的坐标和大小。我想把它存储到sqlite中。是否可以存储整个数组?还是我应该手动将 CGRects 存储在数据库中?</p>

<p><strong>更新:</strong></p>

<p>这是我从数据库中检索到的数据:</p>

<pre><code>62706c69 73743030 d4010203 04050817 18542474 6f705824 6f626a65 63747358 24766572
73696f6e 59246172 63686976 6572d106 0754726f 6f748001 a3090a0f 55246e75 6c6cd20b
0c0d0e5a 4e532e6f 626a6563 74735624 636c6173 73a08002 d2101112 16582463 6c617373
65735a24 636c6173 736e616d 65a31314 155e4e53 4d757461 626c6541 72726179 574e5341
72726179 584e534f 626a6563 745e4e53 4d757461 626c6541 72726179 12000186 a05f100f
4e534b65 79656441 72636869 76657208 11161f28 32353a3c 40464b56 5d5e6065 6e797d8c
949dacb1 00000000 00000101 00000000 00000019 00000000 00000000 00000000 000000c3
</code></pre>

<p>它和我放在数据库中的<em>之前</em>完全一样。 </p>

<p>代码:</p>

<p>这是我用来检查 NSArray 是否正在存档的代码。我还没有将它放入数据库中,因为我只想确保 NSKeyedUnarchive 正常工作。这里是:</p>

<pre><code>    NSData *toData = ;
    NSLog(@&#34;The data %@&#34;, toData);
    NSArray *fromData = ;
    NSLog(@&#34;The data %@&#34;, fromData);
</code></pre>

<p><code>toData</code> 显示归档的表单,但 <code>fromData</code> 根本不显示任何内容。为什么会这样?</p>

<p>这是 <code>rectsArray</code> 的示例:</p>

<pre><code>(
&#34;NSRect: {{83.389008, 179.61432}, {8.775116, -10.616217}}&#34;,
&#34;NSRect: {{83.389008, 179.61432}, {8.775116, -10.616217}}&#34;,
&#34;NSRect: {{83.389008, 179.61432}, {8.775116, -10.616217}}&#34;,
&#34;NSRect: {{96.085426, 179.61432}, {5.2029028, -10.616217}}&#34;,
&#34;NSRect: {{101.28833, 179.61432}, {5.8532658, -10.616217}}&#34;,
&#34;NSRect: {{107.1416, 179.61432}, {4.5525398, -10.616217}}&#34;,
&#34;NSRect: {{115.61544, 179.61432}, {3.2518141, -10.616217}}&#34;,
&#34;NSRect: {{115.61544, 179.61432}, {3.2518141, -10.616217}}&#34;,
&#34;NSRect: {{118.86726, 179.61432}, {6.5036283, -10.616217}}&#34;,
&#34;NSRect: {{118.86726, 179.61432}, {6.5036283, -10.616217}}&#34;
)
</code></pre>

<p>如您所见,它是一个 CGRect 数组。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>好的——使用 Core Data 会更容易一些,但基本原理是一样的。您需要使用存档将数组数据存储到 NSData 对象中:</p>

<pre><code>NSData *dataFromArray = ;
</code></pre>

<p>然后将该数据保存为数据库中的 blob。根据您使用的 SQLite 包装器,它可能有一个获取 NSData 对象并将其转换为字节的方法,否则您可以通过以下方式获取原始字节:</p>

<pre><code>const char *arrayBytes = ;
</code></pre>

<p>要取回数据,您可以将数据库中的字节取回一个 NSData 对象,然后将其解压缩。假设您可以将 blob 从数据库中取出并放入名为 dataFromDB 的 NSData 对象中,您将执行以下操作:</p>

<pre><code>NSArray *myArrayFromDB = ;
</code></pre>

<p>你会得到你的数组!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 Sqlite 中存储 NSMutableArray,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/11622690/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/11622690/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 Sqlite 中存储 NSMutableArray