Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get an array of the values of the properties in an object?

Tags:

arrays

swift

I have an object that stores URLs. In the example below the object has just 4 properties but in my case there are more so I want to know is there a way to do this with more elegant.


public final class MyObject: NSObject {

  private let firstURL: URL
  private let secondURL: URL
  private let thirdURL: URL
  private let fourthURL: URL

  public func values() -> [URL] {
    return // <--- I need to return URLs from properties like [firstURL, secondURL, thirdURL, fourthURL]
  }
}

I've found an extension of NSObject to return an array of the name of the properties as a String.

Extension Source


public extension NSObject {

  //
  // Retrieves an array of property names found on the current object
  // using Objective-C runtime functions for introspection:
  // https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html
  //
  func propertyNames() -> Array<String> {
    var results: Array<String> = []

    // retrieve the properties via the class_copyPropertyList function
    var count: UInt32 = 0
    let myClass: AnyClass = classForCoder
    let properties = class_copyPropertyList(myClass, &count)

    // iterate each objc_property_t struct
    for i in 0..<count {
      if let property = properties?[Int(i)] {
        // retrieve the property name by calling property_getName function
        let cname = property_getName(property)

        // covert the c string into a Swift string
        results.append(cname.debugDescription)
      }
    }

    // release objc_property_t structs
    free(properties)

    return results
  }
}

But it returns array of the property names like ["firstURL", "secondURL", "thirdURL", "fourthURL"]. I want to return values instead of names.

like image 375
emrcftci Avatar asked Sep 12 '25 00:09

emrcftci


2 Answers

You can use Mirror

public final class MyObject: NSObject {
  private let firstURL: URL
  private let secondURL: URL
  private let thirdURL: URL
  private let fourthURL: URL

  public init(firstURL: URL, secondURL: URL, thirdURL: URL, fourthURL: URL) {
    self.firstURL = firstURL
    self.secondURL = secondURL
    self.thirdURL = thirdURL
    self.fourthURL = fourthURL
  }

  public func values() -> [URL] {
    return Mirror(reflecting: self).children.compactMap({ $0.value as? URL })
  }
}

let url = URL(string: "https://stackoverflow.com/")!
let myObject = MyObject(firstURL: url, secondURL: url, thirdURL: url, fourthURL: url)
print(myObject.values()
// [https://stackoverflow.com/, https://stackoverflow.com/, https://stackoverflow.com/, https://stackoverflow.com/]
like image 196
Vicaren Avatar answered Sep 13 '25 13:09

Vicaren


You can use Mirror and iterate over all children:

struct Foo {
    let a: String
    let b: String
    let x: Int

    func propsAsArray() -> [Any] {
        let mirror = Mirror(reflecting: self)
        return mirror.children.map { $0.value }
    }
}


let f = Foo(a: "foo", b: "bar", x: 42)
print(f.propsAsArray()) // ["foo", "bar", 42]
like image 34
Gereon Avatar answered Sep 13 '25 13:09

Gereon