Objective-C types to QString
-
wrote on 27 Apr 2020, 10:49 last edited by
I feel a little bit lost here :/
What I am trying to do is just print values in out log output, for debug reasons:
(void) someMethodWithNSParam:(NSInteger)param { //how to convert param to a QString? myLogger("myfunction", "received param: " + QString::fromStdString(std::to_string(param))); }
I have to call myLogger, which takes QStrings as parameters.
Is there an easy way to convert Objective-C types to QStrings?As seen, I managed to make a conversion to c string and than to QString, but I can't imagine, this is really necessary? xD
I would like to avoid the creation of a variable, because its only a simple output and if there is a proper way to do this, this would be much more readable for a lot of params.Also, I know, that I just could print the values using qDebug, but I have to propagate all output to our custom log function.
Is there a documentation of supported Objective-C type conversions in Qt or at least proper best practices available anywhere?
best regards
SyntaX -
You'll have two conversions anyway however using the Objective-C types all the way will likely be simpler.
See this stack overflow answer. You can make it a one liner to get your QString.
-
Hi,
As a rule of thumb, look at the classes that have names resembling. Not all NSXXX types are going to be supported but the classics like NSString, NSURL have methods for that in Qt.
-
wrote on 27 Apr 2020, 15:35 last edited by
Thanks @SGaist,
the conversion between QString and NSString is straight forward, I think,
but I was wondering if other non-primitive types have a proper conversion method offered by Qt?For me is seems like the double conversion (from NSXXX to NSString and then to QString) is necessary :/
Maybe there is a way with work with raw bytes and use QString::FromRawData or FromLocal8Bit or something... if anyone know a more efficient solution than my approach, pls let me know :D
-
What primitives do you have in mind ? Qt won't cover all of macOS specific data types because it's out of scope.
-
wrote on 28 Apr 2020, 15:12 last edited by
In that special case I am looking for a proper NSInteger to QString conversion.
As mentioned above, I can get around with -> to std string -> to QString,
but this seems a bit of a overkill to me and I don't know any better alternatives :/ -
You'll have two conversions anyway however using the Objective-C types all the way will likely be simpler.
See this stack overflow answer. You can make it a one liner to get your QString.
-
wrote on 29 Apr 2020, 11:38 last edited by
@SGaist said in Objective-C types to QString:
stack overflow answer
Thank you @SGaist, I am posting the answer from SO here, for traceability reasons.
Modern Objective-C
An NSInteger has the method stringValue that can be used even with a literal
NSString *integerAsString1 = [@12 stringValue]; NSInteger number = 13; NSString *integerAsString2 = [@(number) stringValue];
Very simple. Isn't it?
Swift
var integerAsString = String(integer)
1/7