Skip to content

Instantly share code, notes, and snippets.

Last active September 23, 2019 07:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
struct PostContent: Codable {
struct Paragraph: Codable {
let id: String
let text: String
struct Section: Codable {
let id: String
let startIndex: Int
var paragraphs: [Paragraph]
var sections: [Section]
struct Delta: Codable {
struct Paragraph: Codable {
let text: String
let type: String
let paragraphIndex: Int
let paragraph: Paragraph?
func solution(postContentString: String, deltasString: String) -> String {
let postContentData = .utf8)!
let deltasData = .utf8)!
let decoder = JSONDecoder()
let postContent = try! decoder.decode(PostContent.self, from: postContentData)
let deltas = try! decoder.decode([Delta].self, from: deltasData)
var paraCnt = 0
let postContentParagraphs = postContent.paragraphs
let postContentSections = postContent.sections
var newParagraphs = PostContent.init(paragraphs: postContentParagraphs, sections: postContentSections)
for post in postContent.paragraphs {
// writing data to json
let jsonData = try! JSONEncoder().encode(newParagraphs)
let jsonString = String(data: jsonData, encoding: .utf8)!
return solution2(postContentString: jsonString)
override func viewDidLoad() {
let filePath = Bundle.main.url(forResource: "testjson", withExtension: "json")
do {
print(solution(postContentString: "{\"paragraphs\": [{\"id\": \"aaa\",\"text\": \"aaa\"},{\"id\": \"bbb\",\"text\": \"bbb\"},{\"id\": \"ccc\",\"text\": \"ccc\"},{\"id\": \"ddd\",\"text\": \"ddd\"},{\"id\": \"eee\",\"text\": \"eee\"},{\"id\": \"fff\",\"text\": \"fff\"}],\"sections\": [{\"id\": \"s0\",\"startIndex\": 0},{\"id\": \"s1\",\"startIndex\": 2},{\"id\": \"s2\",\"startIndex\": 4}]}", deltasString: "[{\"type\": \"deleteParagraph\",\"paragraphIndex\": 1}, {\"type\": \"deleteParagraph\",\"paragraphIndex\": 1}]"))
} catch {
print("error reading file")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment