हो सकता है कि एक सा पार्टी के लिए देर हो चुकी है, लेकिन इस समाधान प्रतिबिंब का उपयोग और मिरर 100% काम कर रहे है:
class YourClass : NSObject {
var title:String
var url:String
...something other...
func properties() -> [[String: Any]] {
let mirror = Mirror(reflecting: self)
var retValue = [[String:Any]]()
for (_, attr) in mirror.children.enumerated() {
if let property_name = attr.label as String! {
retValue.append([property_name:attr.value])
}
}
return retValue
}
}
और कहीं अपने कोड में ...
var example = MoreRow(json: ["title":"aTitle","url":"anURL"])
print(example.listPropertiesWithValues())
काफी समान (डुप्लिकेट?) प्रश्न: [स्विफ्ट में वर्ग की संपत्तियों की सूची] (http://stackoverflow.com/questions/24844681/list-of-classs-properties-in-swift) –
इस तरह: http://stackoverflow.com/questions/24844681/list -फ-क्लास-प्रॉपर्टी-इन-स्विफ्ट? ओह - ऐसा लगता है :-) –
मैंने कोशिश की लेकिन यह मेरे लिए काम नहीं किया, मेरे मामले में मुझे गुणों को चर नहीं प्राप्त करने की आवश्यकता है। –