###前提・実現したいこと
こんにちは
swift4から使えるようになったDiscで、.jsonに保存する場合は
Disk.appendでobjectを一つずつ追加できるのに、一つずつ削除する方法がわかりません。
####環境
Xcode 9.2
swift4
Disc 0.3.3
###前提となるDiscの登録(https://github.com/saoudrizwan/Disk参考)
###ViewController.swift
class ViewController: UIViewController { // MARK: Properties var posts = [Post]() @IBAction func saveTapped(_ sender: Any) { do { try Disk.save(self.posts, to: .documents, as: "posts.json") } catch let error as NSError { // エラー処理 } for i in 1..<5 { let newPost = Stock(userId: i , id: "id(i)", title: "title(i)", body: "body(i)") try? Disk.append(newPost, to: "posts.json", in: .documents) } }
###Post.swift
import Foundation struct Post: Codable { let userId: Int let id: Int let title: String let body: String }
###やりたいこと
この後posts.jsonに入っているuserId:3のオブジェクトを削除したいのですが
Discのremoveには.jsonごと削除しか用意されてないようで、どのようにすれば簡単に一つだけ削除できるかわかりません。
####Disc.remove
/// - Parameters: /// - path: path of file relative to directory /// - directory: directory where file is located /// - Throws: Error if file could not be removed static func remove(_ path: String, from directory: Directory) throws { do { let url = try getExistingFileURL(for: path, in: directory) try FileManager.default.removeItem(at: url) } catch { throw error } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/14 13:20