ios - NSArray writeFileToPath 失败
<p><p>我无法找到将 <code>NSDictionary</code> 对象数组写入文件的问题。</p>
<p><code>NSDictionary</code> 对象中的每个键都是 <code>NSString</code>,值也是如此。因此,该数组应该可写入 plist,因为文档状态是必要的。无论如何,这是我的代码:</p>
<pre><code>BOOL success = atomically:YES];
//success is NO
</code></pre>
<p>filePath 方法如下所示:</p>
<pre><code>+ (NSString *)filePathForCacheWithCacheID:(NSString *)cacheID
{
NSURL *cachesDirectory = [[ URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
return [ absoluteString];
}
</code></pre>
<p>cacheID 是字符串“objects”。在运行时,<code>filePathForCacheWithCacheID:</code> 方法返回如下字符串:</p>
<pre><code>file:///Users/MyName/Library/Application%20Support/iPhone%20Simulator/7.0/Applications/3A57A7B3-A522-4DCC-819B-DC8DEEDCD041/Library/Caches/objects
</code></pre>
<p>这里可能出了什么问题?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>代码正在尝试将文件写入表示文件 URL 而不是文件系统路径的字符串。如果您希望在需要路径字符串的地方使用该方法的返回值,则应将 <code>absoluteString</code> 调用替换为 <code>path</code>:</p>
<pre><code>+ (NSString *)filePathForCacheWithCacheID:(NSString *)cacheID
{
NSURL *cachesDirectory = [[ URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
return [ path];
}
</code></pre>
<p>或者,让 <code>filePathForCacheWithCacheID:</code> 方法返回一个 <code>NSURL</code>,然后使用 <code>writeToURL:atomically:</code> 方法。</p>
<pre><code>+ (NSURL *)fileURLForCacheWithCacheID:(NSString *)cacheID
{
NSURL *cachesDirectory = [[ URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
return ;
}
...
BOOL success = atomically:YES];
</code></pre></p>
<p style="font-size: 20px;">关于ios - NSArray writeFileToPath 失败,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18295305/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18295305/
</a>
</p>
页:
[1]