-1

How would I test the type of a primitive data type in Objective-C?

- (IBAction)coor1X: (id)sender {
if(typeof(<value of sender>) == double) {
         ...
}
}

The sender is a UITextField in the Interface Builder. I need the input to be a double. Iv'e tried typeof() but it doesn't work the way I intended. I have also looked to find how to get the value of the text field, I'm having trouble finding that also.

Thanks!

carlilejs
  • 11
  • 4
  • What types are you checking for? – Milo Jun 24 '14 at 04:46
  • Your `sender` probably will never be a `double`... What are you trying to do here? – esqew Jun 24 '14 at 04:50
  • possible duplicate of [In Objective-C, how do I test the object type?](http://stackoverflow.com/questions/1144629/in-objective-c-how-do-i-test-the-object-type) – nielsbot Jun 24 '14 at 04:54
  • 1
    double is not an obj-c object type. also there is no typeof() operator on objective-c. there is no run time type checking in C either. Obj-C provides an *-isKindOfClass:* method (among others). – nielsbot Jun 24 '14 at 04:55
  • The real question is: what are you building that needs you to do a type check? Most of the time they are unnecessary. – nielsbot Jun 24 '14 at 04:56
  • The input from a UILabel will never be a double. It is a string. – matt Jun 24 '14 at 05:04

4 Answers4

1

you can check it like this

for example

[myObject isKindOfClass:[NSString class]]

Ashish Ramani
  • 1,214
  • 1
  • 17
  • 30
1

In your code sender is going to be a pointer to an Objective-C class. It's safe to assume that the class will be a subclass of NSObject, so you can use the NSObject method -isKindOfClass: like so:

if ([sender isKindOfClass:<some class name>])
{
    ...
}

A double is not an Objective-C object, so you can't test it with -isKindOfClass:. Likewise, you can't test an Objective-C object with typeof().

user1118321
  • 25,567
  • 4
  • 55
  • 86
1

Using this you know data type of any .. try this may be helpfull

NSLog(@"Variable type get %@",[variableObject class]);

Thanks & Cheers...

Jogendra.Com
  • 6,394
  • 2
  • 28
  • 35
0

It sounds like what you mean is that you are receiving a string from a UILabel (its text) and you are hoping that it is the string representation of a number. In the simplest cases, you can call doubleValue or a similar NSString method to determine whether this is true. It will return 0.0 if the string is not the string representation of a double. See the documentation here: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/doubleValue

In more complicated situations, you can use NSScanner to help analyze the string (e.g. to learn whether it contains a number, even if it is not a number).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    Or instead of NSScanner use an NSNumberFormatter or more easily use NSRegularExpression. – uchuugaka Jun 24 '14 at 06:17
  • Does NSNumberFormatter change the type of Strings to NSNumbers? @uchuugaka – carlilejs Jun 24 '14 at 22:20
  • No. It formats the string in a locale sensitive way to present test representing a number according to display rules. – uchuugaka Jun 25 '14 at 05:35
  • @uchuugaka The thing is that I don't know the regex pattern for the string representation of a double. But NSScanner does! – matt Jun 25 '14 at 15:09
  • Well, it's potentially more complex when we consider locale differences and that the string might be a valid number entered by the user. So NSScanner won't find that as easily as a regular expression. Regular expressions allow patterns that represent whole classes of Unicode characters. But an NSNumberFormatter is the most appropriate approach to simply limit the field input to a valid input. It also handles locale differences. Keep in mind at the very least that . And , are used differently in different locales. Not to mention other scripts entirely. – uchuugaka Jun 25 '14 at 15:53
  • Also I misspoke about NSNumberFormatter earlier. It easily converts number to string and vice versa. – uchuugaka Jun 25 '14 at 15:55