skip to Main Content

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

3

Answers


  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.

    Login or Signup to reply.
  2. 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
    }
    
    Login or Signup to reply.
  3. You need to perform the selector:

    let nameSEL = NSStringFromSelector(#selector(getter: self.name))
    let name = self.performSelector(nameSEL)?.takeUnretainedValue() as? String
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search