0

I have a struct which consist of some elements which are in String, Bool and Int lets call it Struct A. I also have an another struct lets called it Struct B.

Struct A{
let name: String
let surname: String
let age: Int   
}

Struct B{
let columnId: Int
let value: String
}

I have an array of B and I want to assign values of A to B. Example array;

FirstElement(columnId:1,value:"name=myname") SecondElement(columnId:2,value:"surname=mysurname") ThirdElement(columnId:3,value:"age=20")

I can get all properties of a struct with below extension;

extension Loopable {
func allProperties() throws -> [String: Any] {

    var result: [String: Any] = [:]

    let mirror = Mirror(reflecting: self)

    // Optional check to make sure we're iterating over a struct or class
    guard let style = mirror.displayStyle, style == .struct || style == .class else {
        throw NSError()
    }

    for (property, value) in mirror.children {
        guard let property = property else {
            continue
        }

        result[property] = value
    }

    return result
}
}

After I get the all properties I want some kind of iteration; My question is that how can I get value and key like "value=key" string in below "classA[i]"

 func setArray(){
 let classA = classA().getAllProperties()
    for i in 0..<deviceLogs.count{
        myArray.append(B(columnId: i, value: classA[i]) )
    }
}

EDIT: I want to get all the properties with its values inside Struct A and use it as String for value property when creating Struct B object.

2
  • classA["name"] is not working? Commented Nov 27, 2018 at 14:33
  • 1
    If you have to use Mirror, you are doing something wrong. Commented Nov 27, 2018 at 15:37

2 Answers 2

1

Modify your setArray function

func setArray(){
    let keysArray = Array(classA.keys)
    let valuesArray = Array(classA.values)

    let classA = classA().getAllProperties()
    for i in 0..<deviceLogs.count{
        let valueString = keysArray[i] + "=" + valuesArray[i]
        myArray.append(B(columnId: i, value: valueString) )
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Works like a charm. I have only one question. Can we be sure that keysArray and valuesArray will be same in all situations. I mean first value of keys array equals to values arrays first key?
By definition, a dictionary has no order. So I would recommend you to eventually improve your data structure instead of using a dictionary. In your allProperties function, you are creating a dictionary called result.
Aaa ok sorry. Actually, I guess, this is not a bad approach. I'm just not sure If both arrays' has same order every time.
1

Check out this

struct B {
    let columnId: Int
    let value: Any
}

protocol Properties {
    var allProperties: [Any] { get }
}

extension Properties {
    var allProperties: [Any] {
        return Mirror(reflecting: self).children.flatMap { $0 }
    }
}    

struct A: Properties {
    let name: String
    let surname: String
    let age: Int
}

Here is the example how you can use it:

let properties = A(name: "name", surname: "surname", age: 18).allProperties

// NOTE: your value property in B should be of type Any in place of String

var list = [B]()
for (index, property) in properties.enumerated() {
    list.append(B(columnId: index, value: property))
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.