[SOLVED] How would I do this in Objective-C?

Discussion in 'Mac Programming' started by chrono1081, Oct 26, 2011.

  1. chrono1081, Oct 26, 2011
    Last edited: Oct 26, 2011

    chrono1081 macrumors 604


    Jan 26, 2008
    Isla Nublar
    Hi guys,

    Is there a way in Objective-C to check if a value is of a certain data type? For example:

    Say I have a value a user enters and it could be a number or a char, and I want to ensure its one or the other, is there an easy way to do this? Obviously the below won't work but is there any way to do this that is just as simple?

    char myVar;

    if(myVar != char data type)

    I can think of a work around using C's atoi function but I'd rather not do it that way if there is a more elegant solution.

    EDIT: Well, I learned something new today. I NEVER knew about the isdigit, isalpha, and isalnum operators. How I never knew about them I don't know but they work like a charm in this situation. I was just about to write a custom function using atoi too.

    If anyone is reading this and is unfamiliar with programming here is how it works:

    int myVar = 5;
    NSLog(@"This is a variable");
    Same goes for the other operators.
  2. Mac_Max macrumors 6502

    Mar 8, 2004
  3. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    All data entered by a user is ultimately characters. It's only a question of what interpretation you give the characters.

    For example, if a user entered "3d", it might be a hex number, a number followed by a representation of the base (e.g. 'd' for decimal), or a string identifying how to render a scene (2d vs. 3d anaglyphic). Or it could be interpreted any other way, depending on what the program is asking for.

    You also need to identify exactly what action occurs when the user enters a value. Do you mean something typed in Terminal as standard input? Something entered into a Cocoa text field? Something else, such as speech-to-text conversion? Something else entirely?

    If you're working with text, then everything is ultimately characters, and probably in a string. For a text field or similar, that usually means an NSString* representation. For other things, you'll have to provide more detail.

    If your input is an NSString*, then there are any number of ways to interpret its contents. NSFormatter, NSValueTransformer, or an NSRegularExpression pattern, or various things parsed from NSScanner. No single class will suit every interpretation, so it's important to explain exactly what you're trying to do. For example, exactly what do you mean by "characters", and how does that differ from "number". Remember that any "number" is nothing but an interpretation of a sequence of "digits" and "numeric symbols", for an arbitrary definition of "digit" and "numeric symbol". The string "vi" might mean "six" if the interpretation is Roman numerals, or it might mean an editor program if the interpretation is a Unix command name.

    Way. Too. Slow. And. Punctilious.
    Note to self: programming forums and Glenlivet don't mix.
  4. chrono1081 thread starter macrumors 604


    Jan 26, 2008
    Isla Nublar
    Thanks guys :)

    I usually program in C++ but since I am now done with college I can focus on the languages I want to learn.

    Although I've done a lot of Objective-C in the past I've forgotten a good amount of it so I am going back through and relearning it. Thankfully its easy to pick back up.

Share This Page