Skip to content

Instantly share code, notes, and snippets.

@kmdarshan
Last active September 23, 2019 07:16
Show Gist options
  • Save kmdarshan/897f3e54ab4f133c25af4512be7e4ff1 to your computer and use it in GitHub Desktop.
Save kmdarshan/897f3e54ab4f133c25af4512be7e4ff1 to your computer and use it in GitHub Desktop.
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 = postContentString.data(using: .utf8)!
let deltasData = deltasString.data(using: .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