Dynamic protocol conformance in Swift - swift

Hi I am struggle to solve the problem dynamic protocol conformance in swift language. Please see code.
Protocol:
protocol Object {
init(by object: [String: Any])
}
Custom structs with protocol object conformance:
struct Tree: Object {
let treeName: String
init(by object: [String: Any]) {
self.treeName = object["tree"] as? String ?? "Notree"
}
}
struct Plant: Object {
let plantName: String
init(by object: [String : Any]) {
self.plantName = object["tree"] as? String ?? ""
}
}
The above code just fine until the object is [String: Any]. I can't use [[String: Any]] like below.
let coconut = ["tree":"Coconut"] // => This fine
let allTrees = [["tree":"Apple"],["tree":"Orange"],["tree":"Jakfruit"]] //=> Here is the problem
let aTree = Tree(by: coconut)
let bTree = Tree(by: ["data":allTrees])
let cTree = Plant(by: ["data":allTrees])
I can't use array of objects. So, I used to store objects in to key "data". Now I used extension: Array confirm protocol object.
extension Array: Object where Element == Object{
init(by object: [String : Any]) {
if let data = object["data"] as? [[String: Any]]{
self = data.map({ (object) -> Object in
// return Plant.init(by: object) // => Works, But I need dynamic confirmance
// return Tree.init(by: object) // => Works, But I need dynamic confirmance
return Object.init(by: object) //=> How can I do?
})
}else{
self = []
}
}
}
The return Object shows error Protocol type 'Object' cannot be instantiated. I tried lot to solve but not able.
Can someone suggest better idea or solution for this problem? Thank you in advance...

First, you should not use the constraint == Object. You want to say that not only [Object] is an Object, but also [Plant] and [Tree] are Objects too, right? For that, you should use the : Object constraint. Second, you can use Element.init to initialise a new Element of the array. Because of the constraint Element : Object, we know that a init(by:) initialiser exists:
extension Array: Object where Element: Object{
init(by object: [String : Any]) {
if let data = object["data"] as? [[String: Any]]{
self = data.map({ (object) in
return Element.init(by: object)
})
}else{
self = []
}
}
}
Usage:
let trees = [Tree](by: ["data": allTrees])
Here's what I think a more Swifty version of your code, making use of failable initialisers - initialisers that return nil when they fail to initialise the object:
protocol Object {
init?(by object: [String: Any])
}
struct Tree: Object {
let treeName: String
init?(by object: [String: Any]) {
if let treeName = object["tree"] as? String {
self.treeName = treeName
} else {
return nil
}
}
}
struct Plant: Object {
let plantName: String
init?(by object: [String : Any]) {
if let plantName = object["tree"] as? String {
self.plantName = plantName
} else {
return nil
}
}
}
extension Array: Object where Element: Object{
init?(by object: [String : Any]) {
if let data = object["data"] as? [[String: Any]]{
self = data.compactMap(Element.init)
}else{
return nil
}
}
}

Related

How convert Realm data to Json on Swift? Realm version 10.11.0

until version 10.7.6 of Realm I could convert to dictionary and then to json with this code below, but the ListBase class no longer exists.
extension Object {
func toDictionary() -> NSDictionary {
let properties = self.objectSchema.properties.map { $0.name }
let dictionary = self.dictionaryWithValues(forKeys: properties)
let mutabledic = NSMutableDictionary()
mutabledic.setValuesForKeys(dictionary)
for prop in self.objectSchema.properties as [Property] {
// find lists
if let nestedObject = self[prop.name] as? Object {
mutabledic.setValue(nestedObject.toDictionary(), forKey: prop.name)
} else if let nestedListObject = self[prop.name] as? ListBase { /*Cannot find type 'ListBase' in scope*/
var objects = [AnyObject]()
for index in 0..<nestedListObject._rlmArray.count {
let object = nestedListObject._rlmArray[index] as! Object
objects.append(object.toDictionary())
}
mutabledic.setObject(objects, forKey: prop.name as NSCopying)
}
}
return mutabledic
}
}
let parameterDictionary = myRealmData.toDictionary()
guard let postData = try? JSONSerialization.data(withJSONObject: parameterDictionary, options: []) else {
return
}
List now inherits from RLMSwiftCollectionBase apparently, so you can check for that instead. Also, this is Swift. Use [String: Any] instead of NSDictionary.
extension Object {
func toDictionary() -> [String: Any] {
let properties = self.objectSchema.properties.map { $0.name }
var mutabledic = self.dictionaryWithValues(forKeys: properties)
for prop in self.objectSchema.properties as [Property] {
// find lists
if let nestedObject = self[prop.name] as? Object {
mutabledic[prop.name] = nestedObject.toDictionary()
} else if let nestedListObject = self[prop.name] as? RLMSwiftCollectionBase {
var objects = [[String: Any]]()
for index in 0..<nestedListObject._rlmCollection.count {
let object = nestedListObject._rlmCollection[index] as! Object
objects.append(object.toDictionary())
}
mutabledic[prop.name] = objects
}
}
return mutabledic
}
}
Thanks to #Eduardo Dos Santos. Just do the following steps. You will be good to go.
Change ListBase to RLMSwiftCollectionBase
Change _rlmArray to _rlmCollection
Import Realm

Decoding a nullable heterogeneous dictionary

I need to decode a dictionary of the form [(Heterogeneous object): CGFloat]?. I've got working extensions on the KeyedDecodingContainer that allow me to decode lists of and single heterogeneous objects however I'm struggling to convert these to work with a dictionary. Here is the working method for a optional array:
func decodeArrayIfPresent<T : Decodable, U : ClassFamily>(family: U.Type, forKey key: K) throws -> [T]? {
do {
var container = try self.nestedUnkeyedContainer(forKey: key)
var list = [T]()
var tmpContainer = container
while !container.isAtEnd {
let typeContainer = try container.nestedContainer(keyedBy: Discriminator.self)
let family: U? = try typeContainer.decodeIfPresent(U.self, forKey: U.discriminator)
if let type = family?.getType() as? T.Type {
list.append(try tmpContainer.decode(type))
}
}
return list
} catch DecodingError.keyNotFound {
return nil
} catch DecodingError.valueNotFound {
return nil
}
}
And this is where I've got to for a dictionary:
func decodeDictIfPresent<T : Decodable, U : ClassFamily>(family: U.Type, forKey key: K) throws -> [T: Any]? {
do {
var container = try self.nestedUnkeyedContainer(forKey: key)
var dict = [T: Any]()
var tmpContainer = container
while !container.isAtEnd {
let typeContainer = try container.nestedContainer(keyedBy: Discriminator.self)
let family: U? = try typeContainer.decodeIfPresent(U.self, forKey: U.discriminator)
if let type = family?.getType() as? T.Type {
list.append(try tmpContainer.decode(type))
}
}
return dict
} catch DecodingError.keyNotFound {
return nil
} catch DecodingError.valueNotFound {
return nil
}
}
Requirements:
protocol ClassFamily: Decodable {
/// The discriminator key.
static var discriminator: Discriminator { get }
/// Returns the class type of the object coresponding to the value.
func getType() -> AnyObject.Type
}
/// Discriminator key enum used to retrieve discriminator fields in JSON payloads.
enum Discriminator: String, CodingKey {
case type = "type"
}
Any pointers in the right direction would be appreciated! Thanks in advance

Inheritance and constructors. Self used before super.init

I am new at inheritance in swift and i'd like to understand how to create constructors in the best way.
I get an error Must call a designated initializer of the superclass 'Base'
class Base{
var id:String
var link:String
convenience init?(json: [String: Any]) {
guard let id = json["id"] as? String,
let link = json["link"] as? String else{
print("Failed Base Class Init!")
return nil
}
self.init(id: id, link: link)
}
init(id: String, link: String) {
self.id = id
self.link = link
}
}
class Image: Base{
func addImageToView(imageView: UIImageView){
Alamofire.request(self.link).responseImage { response in
if let img = response.result.value {
imageView.image = img
}
}
}
}
class Gallery:Base{
var images:[Image] = []
init?(json: [String: Any]){
super.init(json: json) // Here i get <- Must call a designated initializer of the superclass 'Base'
guard let tab = json["images"] as? [[String:Any]] else{
print("Failed Gallery Image Init!")
return nil
}
images = tab.compactMap{return Image(json: $0)}
}
}
How to do so that my class gallery creates a table of images from json and also initializes the id and link?
Make init?(json (also) a designated initializer
init?(json: [String: Any]) {
guard let id = json["id"] as? String,
let link = json["link"] as? String else{
print("Failed Base Class Init!")
return nil
}
self.id = id
self.link = link
}
And override it in the subclass
override init?(json: [String: Any]) {
super.init(json: json)
...

Defining swift extensions with generic functions for realm and object mapper

I am working with Realm and ObjectMapper and would like to create some extensions to make my life easier when backing up some data to JSON. I have the following extensions defined:
extension Mappable where Self:Object {
func getCompleteJSONDictionary() throws -> [String: Any]? {
var returnValue: [String: Any]?
if self.isManaged, let realm = self.realm, !realm.isInWriteTransaction {
try realm.write {
var data = self.toJSON()
data["id"] = self.getPrimaryKeyValue()
returnValue = data
}
} else {
var data = self.toJSON()
data["id"] = self.getPrimaryKeyValue()
returnValue = data
}
return returnValue
}
}
extension Results where Element:Object, Element:Mappable {
func getAllCompleteJSONDictionaries() throws -> Array<[String:Any]>? {
var array: Array<[String:Any]> = Array()
for element in self {
if let dictionary = try? element.getCompleteJSONDictionary(), let data = dictionary {
array.append(data)
}
}
if array.count > 0 {
return array
} else {
return nil
}
}
}
extension Realm {
func getJSONBackupData<T>(forTypes types: [T.Type]) throws -> [String: Any] where T:Object, T:Mappable {
var data: [String: Any] = [:]
try self.write {
for type in types {
let entities = self.objects(type)
if let entityJsonData = try entities.getAllCompleteJSONDictionaries() {
data[String(describing: type)] = entityJsonData
}
}
}
return data
}
}
The first two extensions work fine, but as soon as I try to use the last, the country class conforms to both Object and Mappable:
var finalData = realm.getJSONBackupData(forTypes:[Country.self])
I get an error that T cannot be inferred. I still get myself hopelessly muddled when it comes to generics in Swift so am guessing i am just not understanding the problem correctly. Is there an easy fix here or have I asked too much of the compiler?
The problem is that the Realm Objects does not conform to Mappable Protocol. So the Country Object is not Mappable and thus the complier is saying that Type Country.self cannot be inferred as Object and Mappable.

Swift Get all the properties in generic class

I'm trying to get all the members of a generic class T, I can get the properties based on a specific class.
But, how I can do it using Mirror ?
let mirrored_object = Mirror(reflecting: user)
for (index, attr) in mirrored_object.children.enumerated() {
if let propertyName = attr.label as String! {
print("Attr \(index): \(propertyName) = \(attr.value)")
}
}
I added this as extension
extension NSObject {
public func GetAsJson() -> [[String:Any?]] {
var result:[[String: Any?]] = [[String: Any?]]()
for item in self {
var dict: [String: Any?] = [:]
for property in Mirror(reflecting: self).children {
dict[property.label!] = property.value
}
result.append(dict)
}
return result
}
}