JohnBigs
JohnBigs

Reputation: 2811

Getting error trying to parse NSString into NSDate

I'm total newbie so please forgive me if im doing something terrible here.

Im trying to take the user birthday in the format I specify in the example, but getting error in the scanf section: format specified type char * but the argument has type NSString

How can I fix it? this is my code:

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

    @autoreleasepool {

        NSString *input;

        NSLog(@"Please enter your birthday, for example: 09-01-1984");

        scanf("%s", &input);

        NSDate *today = [NSDate date];

        NSDate *bDay = [NSDate dateWithNaturalLanguageString:input];

        NSLog(@"%lu", [bDay elapsedDays:today]);

    }
    return 0;
}

Upvotes: 2

Views: 152

Answers (1)

trojanfoe
trojanfoe

Reputation: 122391

sscanf() is part of the C-library and NSString is part of the Foundation Objective-C framework and you cannot mix them like that.

Try this:

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        char input[80];
        NSLog(@"Please enter your birthday, for example: 09-01-1984");
        scanf("%s", input);
        NSDate *today = [NSDate date];
        NSDate *bDay = [NSDate dateWithNaturalLanguageString:[NSString stringWithUTF8String:input]];
        NSLog(@"%lu", [bDay elapsedDays:today]);
    }
    return 0;
}

Upvotes: 3

Related Questions