Strings in Objective-C ++

I just switched my code from Objective-C to Objective-C ++. Everything goes smoothly, with the exception of two lines.

NSString * text1=[[NSString stringWithFormat:@"%.2f",ymax] UTF8String];

This line complains that

error: cannot convert 'const char*' to 'NSString*' in initialization

The second error associated with the first is related to the line:

CGContextShowTextAtPoint(context, 2, 8, text1, strlen(text1));

He complains that

error: cannot convert 'NSString*' to 'const char*' for argument '1' to 'size_t strlen(const char*)'

Is there something I missed in the differences between ObjC and ObjC ++?

+3
source share
3 answers

Do you want to:

const char * text1 = [[NSString stringWithFormat:@"%.2f", ymax] UTF8String];

Not:

NSString *text1 = [[NSString stringWithFormat:@"%.2f", ymax] UTF8String];

(Note the return value is UTF8String .)

+7
source

But did you know that you can also just say NSString to draw yourself, for example?

NSString *text = [NSString stringWithFormat:@"%.2f", ymax];

//  Send a message to the string to draw itself at the given point with
//  the provided font.
//
[text drawAtPoint:CGPointMake(20.0, 30.0)
         withFont:[UIFont systemFontOfSize:36.0]];
+2
source

char * ( UTF8String) NSString *. ; ++, , . , ; UTF8String ; CGContextShowTextAtPoint(context, 2, 8, text1, strlen([text1 UTF8String]));

+1

Source: https://habr.com/ru/post/1736003/


All Articles