Important: Please read the Qt Code of Conduct -

Convert QString to NSString*

  • Hi all,

    I'm working on an iOS app. I need to convert a QString to a NSString* but I can't figure out how it works. The following code produces a NSException:

    QString qs = "test";
    NSString* nss = qs.toNSString();
    NSLog(@"qs to ns: %@", [NSString nss]);
    @catch (NSException *theException)
    NSLog(@"An exception occurred: %@",;
    NSLog(@"Here are some details: %@", theException.reason);

    log output:

    2016-07-25 13:58:13.676 MyApp[3832:1414942] +[NSString nss]: unrecognized selector sent to class 0x3b5b42dc
    2016-07-25 13:58:13.677 MyApp[3832:1414942] An exception occurred: NSInvalidArgumentException
    2016-07-25 13:58:13.677 MyApp[3832:1414942] Here are some details: +[NSString nss]: unrecognized selector sent to class 0x3b5b42dc

    What do I miss?

    Thank you in advance.

  • This post is deleted!

  • @BjoernK You can probably do it using NSString methods and then filter to QString
    NSString reference

    In particular, look at the "Getting C Strings" portion.

  • Lifetime Qt Champion


    If you want to log nss why not use NSLog(@"qs to ns: %@", nss); ?

  • Thank you for your replies.

    @SGaist you're right - the string conversion is correct but the NSLog() call was wrong.

    I should take a look at the objective C basics.

Log in to reply