Search code examples
iphoneobjective-cxcode4ios5

Restricting what is passed into a method


Right lets say I have a method something like :

 - (void)doStuff:(NSString *)doStuffWith;

Can I make it so that doStuffWith will only accept certain words like lets say "DoSomething1" and "DoSomething2", so when i call it like :

 [self doStuff:@"DoSomething1"];

it will run but if I call it like :

 [self doStuff:@"HelloWorld"];

it will give a warning or something?


Solution

  • You should use an enum, like:

    typedef enum {
        MyStuffOne,
        MyStuffTwo,
        MyStuffThree
    } MyStuff;
    
    
    - (void)doStuff:(MyStuff)stuff;
    

    thus you will be able to pass only "MyStuff" (MyStuffOne, MyStuffTwo, MyStuffThree)... these are integers and if you want to play with strings, in your method you have to do something like:

    NSString *string;
    
        switch (stuff)
        {
          case MyStuffOne:
            string = @"StuffOneString";
          break;
    
         default:
         ...
        }