0

I have the following question about swift, let say I have an class like so:

class Person {
    var firstName, lastName, eyeColor: String

    init(firstName: String = "", lastName: String = "", eyeColor: String = "") {
        self.firstName = firstName
        self.lastName = lastName
        self.eyeColor = eyeColor
    }
}

I could for example create a person like so:

var myFriend = Person(firstName: "Robin", eyeColor: "Yellow")

However as you can tell now I have not assigned a value to lastName aka this will be empty or "".

What I want to do is iterate over all values in myFriend and check if one is empty, if thats true I would like it to replace the empty value with something like "No information".

I did find something like

    let mirroredRecord = Mirror(reflecting: myFriend)
    for (index, attr) in mirroredRecord.children.enumerated() {
        if var value = attr.label as String? {
            if value == "" {
                value = "No info"
            }
            print("Attr \(index): \(value) = \(attr.value)")
        }
    }

However that does not change the value in the myFriend variable.

In my head the following seems logical but i'm unsure how to perform this action in swift.

for (index, attr) in mirroredRecord.children.enumerated() {
    if var value = attr.label as String? {
        if value == "" {
            myFriend.value = "No info"
        }
        print("Attr \(index): \(value) = \(attr.value)")
    }
}

However because .value is not one of the options (firstName, lastName, eyeColor) it doesn't like it.

Does anyone have any options or solutions regarding this?

4
  • How about assigning "No Information" as default value? init(firstName: String = "No Information", lastName: String = "No Information", eyeColor: String = "No Information") { Commented Aug 7, 2019 at 14:24
  • I thought of this, however in my case I want to be able to show these fields in labels, and so I prefer the label to show empty instead of "no info" what I could do of course is have some kind of logic where if the value is "no info" put "" in the label but it would be a lot of checking for this. Commented Aug 7, 2019 at 14:39
  • Alternatively declare the properties as optional which can be empty. Commented Aug 7, 2019 at 14:43
  • This would be a total misuse of both String and Mirror. This is precisely what optionals are for. Commented Aug 7, 2019 at 16:38

1 Answer 1

1

You can update the class with a for loop if you use KeyPaths to represent the fields you want to update:

func update() {
    let kpaths: [ReferenceWritableKeyPath<Person, String>] = [\.firstName, \.lastName, \.eyeColor]

    for path in kpaths {
        if self[keyPath: path].isEmpty {
            self[keyPath: path] = "No information"
        }
    }
}

That said, I would suggest just using optionals with nil representing "No Information".

class Person {
    var firstName, lastName, eyeColor: String?

    init(firstName: String? = nil, lastName: String? = nil, eyeColor: String? = nil) {
        self.firstName = firstName
        self.lastName = lastName
        self.eyeColor = eyeColor
    }
}

Then when you access the fields, you can use the nil coalescing operator ?? to unwrap the value and present nil ones however you like:

someLabel.text = self.firstName ?? ""

or:

someLabel.text = self.firstName ?? "No information"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I ended up providing these values nil instead of "" then indeed in a later stage provide them the "No information" label!

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.