Adeel Ahmed
Adeel Ahmed

Reputation: 77

how to convert const char * argv[] in NSArray in Objective C

I just want to convert the const char * argv[] in main method to NSArray or NSMutableArray. But I am really stuck and couldn't find any solution.

Any help will be appreciated.

Upvotes: 0

Views: 1650

Answers (1)

rmaddy
rmaddy

Reputation: 318824

You have a C-array of char * (C strings). You need to convert each C string into an NSString. Then you can add each NSString to your NSMutableArray.

int main(int argc, char *argv[]) {
    NSMutableArray *results = [NSMutableArray array];
    for (int i = 0; i < argc; i++) {
        NSString *str = [[NSString alloc] initWithCString:argv[i] encoding:NSUTF8StringEncoding];
        [results addObject:str];
    }
}

Upvotes: 4

Related Questions