Search code examples
objective-cxcodeargv

Using arg[v] as int? - Objective C


I'm trying to pass arguments into and integer variable at the start of my program in xcode using Objective C. So far most methods I've found have just given me a variety of error messages or crashed my VM.

int numcols, numrows;

int main(int argc, const char * argv[]) {
@autoreleasepool {

    if (argc != 3){
        NSLog(@"%2s", "Wrong number of arguments");
        exit(1);
    }

    //numrows = (int)(argv[1] - '0'); 
    //numcols = (int)(argv[2] - '0'); These lines cause a crash
    numrows = 3;
    numcols = 4;

I want numrows to take the first argument and numcols to take the second. I went into the settings in xcode to change the starting arguments to 3 and 4, but if I check them or print them they come out as random numbers.


Solution

  • First of all your question is not clear, how much i understand i can say. As argc means argument count and argv means argument value. If you want to cast char to int. try this

    [[NSString stringWithFormat:@"%s", argv[2]] intValue];