(Translated from English.) I am trying to get the title and description from a JSON resource from the REALM database. I can get the title without any problems, but I can't get the description, because this is a reserved name in SWIFT, so I tried ObjectMapper. And still, I can not get a description ... Code such ...

I also have another problem. How can I write a source object from NewRealmSourceLib to a NewRealmLib object. My way does not work!

{ "author": "http://www.abc.net.au/news/mazoe-ford/6525834, http://www.abc.net.au/news/dom--vukovic/7637226", "title": "Jury takes just 32 minutes to convict two men over Daley's violent death", "description": "Two men are found guilty over the death of Lynette Daley, whose naked body was found bruised and bloodied after a boozy 2011 camping trip to a remote beach in northern NSW.", "url": "http://www.abc.net.au/news/2017-09-06/lynette-daley-trial-delivers-guilty-verdicts/8878848", "urlToImage": "http://www.abc.net.au/news/image/7392268-1x1-700x700.jpg", "publishedAt": "2017-09-06T09:14:02Z" }, class NewRealmLib : Object, Mappable { dynamic var title: String = "" dynamic var descript: String = "" var source: NewRealmSourceLib? required convenience init?(map: Map) { self.init() } func mapping(map: Map) { descript <- map["description"] title <- map["title"] } override static func primaryKey() -> String? { return "title" } } class NewRealmSourceLib: Object { dynamic var source: String = "" override static func primaryKey() -> String? { return "source" } } 
  • one
    Welcome to StackOverflow in English! As the name implies, this site. Please either translate the question in English . If you choose to translate it. - Mikhail Rebrov

1 answer 1

You can get the title and description using protocol Decodable and JSONDecoder() in Swift 4 . The code is below. Swift reserved words can be used as their identifiers by enclosing them in backticks ( `` ) characters.

 import Foundation let json = """ { "author": "http://www.abc.net.au/news/mazoe-ford/6525834, http://www.abc.net.au/news/dom--vukovic/7637226", "title": "Jury takes just 32 minutes to convict two men over Daley's violent death", "description": "Two men are found guilty over the death of Lynette Daley, whose naked body was found bruised and bloodied after a boozy 2011 camping trip to a remote beach in northern NSW.", "url": "http://www.abc.net.au/news/2017-09-06/lynette-daley-trial-delivers-guilty-verdicts/8878848", "urlToImage": "http://www.abc.net.au/news/image/7392268-1x1-700x700.jpg", "publishedAt": "2017-09-06T09:14:02Z" } """ 

Decodable protocol class declaration:

 class NewRealmLib : Decodable { var author : String var title : String var description : String var url : URL var urlToImage : URL var publishedAt : String } 

A variant of using JSON data with Decodable – class and JSONDecoder () decoder:

 func __test_so_715056() { print("[TEST]: \(#function)") let data = json.data(using: .utf8) let decoder = JSONDecoder() let lib = try! decoder.decode(NewRealmLib.self, from:data!) let `description` = lib.description // Используем backticks print(lib.title) print(`description`) print("[/TEST]") } 

The result of the title and description will be as follows.

 [TEST]: __test_so_715056() Jury takes just 32 minutes to convict two men over Daley's violent death Two men are found guilty over the death of Lynette Daley, whose naked body was found bruised and bloodied after a boozy 2011 camping trip to a remote beach in northern NSW. [/TEST]