有没有一种方法来获取对象中属性值的数组?

时间:2020-06-01 09:39:08

标签: arrays swift

我有一个存储URL的对象。在下面的示例中,该对象只有4个属性,但就我而言,还有更多属性,所以我想知道有没有一种方法可以使它更加优雅。


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]
  }
}

我找到了NSObject的扩展名,以将属性名称的数组作为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
  }
}

但是它返回属性名称的数组,例如["firstURL", "secondURL", "thirdURL", "fourthURL"]。我想返回值而不是名称。

2 个答案:

答案 0 :(得分:1)

您可以使用Mirror并遍历所有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]

答案 1 :(得分:1)

您可以使用镜像

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/]