1

The code is as follows, in the OC to get the object type using [touch.view class], in Swift 3 how to get it.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
    return NO;
} else {
    return YES;
}
}
Sahil
  • 9,096
  • 3
  • 25
  • 29
RaymondWoo
  • 21
  • 1
  • 4
  • type(of: yourObject) use this – Sivajee Battina Apr 10 '17 at 16:54
  • In this case you would use `is` or `as?` as in these answers http://stackoverflow.com/a/26384597/1187415, http://stackoverflow.com/a/30304590/1187415 to the duplicate. – Martin R Apr 10 '17 at 16:56
  • You shouldn't have been using Strings to compare classes like this, in the first place. See my answer here: http://stackoverflow.com/a/40388434/3141234 – Alexander Apr 10 '17 at 17:01
  • Everybody is way overcomplicating this. All you need is `return !(touch.view is UITableViewCellContentView)` – Alexander Apr 10 '17 at 17:02

2 Answers2

5

Expanding on @jglasse's answer, you can get the type of an object by using

let theType = type(of: someObject)

You can then get a string from that by

let typeString = String(describing: type)

Or in one line:

let typeString = String(describing: type(of: someObject))
Pedro Castilho
  • 10,174
  • 2
  • 28
  • 39
1

In Swift 3, to determine type of your object, you can use:

type(of: yourObject)

Example:

let myString = "I'm a String!"

let myType = type(of: myString)

print(myType) // prints "String\n"
jglasse
  • 1,188
  • 12
  • 23