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.
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.