0

I am using the event structure of the netmera analytics tool, it automatically generates code for any even as below.

Unfortunately, according to the structure, it has to get the value of the variable with the selector and when I test it it brings the name of the variable instead of the value of the variable.

Can you please help about this problem.

class SearchControlEvent: NetmeraEvent {
    
    @objc let kSearchControlEvent: String = "xyxyxy"
    
    @objc var name: String = "test_data_1"
    @objc var screen: String = "test_data_2"
    @objc var current: String = "test_data_3"
    
    override class func keyPathPropertySelectorMapping() -> [AnyHashable: Any] {
        
        let name = NSStringFromSelector(#selector(getter: self.name))
        let screen = NSStringFromSelector(#selector(getter: self.screen))
        let current = NSStringFromSelector(#selector(getter: self.current))
    
        print(name) // print: name
        print(screen) // print: screen
        print(current) // print: current
        
        return[
            "xxx": NSStringFromSelector(#selector(getter: self.name)),
            "yyy": NSStringFromSelector(#selector(getter: self.screen)),
            "zzz": NSStringFromSelector(#selector(getter: self.current))
        ]
    }
    
    override var eventKey : String {
        let key = NSStringFromSelector(#selector(getter: self.kSearchControlEvent))

        print(key) // print: kSearchControlEvent

        return key
    }
}
enjektor0
  • 483
  • 2
  • 8
  • 1
    It looks like you're looking for `value(forKey:)` https://developer.apple.com/documentation/objectivec/nsobject/1412591-value If you need a non-object result (i.e a value type), it's a bit cumbersome to do, but still feasible https://stackoverflow.com/a/43714950/5329717 – Kamil.S Dec 22 '20 at 15:16

3 Answers3

1

NSStringFromSelector returns only the string representation of the selector: https://developer.apple.com/documentation/foundation/1395257-nsstringfromselector?language=objc If you need the value, you need to invoke the selector.

BoygeniusDexter
  • 2,154
  • 1
  • 16
  • 14
0

Accessing the property will use the selector under the hood, you dont need to do this explicitly. This should work:

override var eventKey : String {
    return self.kSearchControlEvent
}
Nick
  • 2,361
  • 16
  • 27
  • Thank you this works. However, the keyPathPropertySelectorMapping function cannot be accessed as self.screen – enjektor0 Dec 22 '20 at 14:12
  • that function is a Class function, so you'd access it like `let mapping = SearchControlEvent. keyPathPropertySelectorMapping()` – Nick Dec 22 '20 at 14:45
0

You need to perform the selector:

let nameSEL = NSStringFromSelector(#selector(getter: self.name))
let name = self.performSelector(nameSEL)?.takeUnretainedValue() as? String
arturdev
  • 10,884
  • 2
  • 39
  • 67