Convert NSString to String

NSDate *now = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy"]; NSString *stringFromDate = [formatter stringFromDate:now]; CGContextShowTextAtPoint(context, 50, 50, stringFromDate, 5); 

Am I not getting the exact date? also getting compilation warning
warning: pass argument 4 of 'CGContextShowTextAtPoint' from an incompatible pointer type

+4
source share
2 answers

What is the value of stringFromDate ? What do you expect?

also gets a warning when compiling a warning: passing argument 4 of 'CGContextShowTextAtPoint' of an incompatible pointer type

If you look at the docs for CGContextShowTextAtPoint , you will see that the fourth parameter should be char* , not NSString*.

You have:

 GContextShowTextAtPoint(context, 50, 50, stringFromDate, 5); 

Do you want to:

 GContextShowTextAtPoint(context, 50, 50, [stringFromDate UTF8String], 5); 
+1
source

Function Prototype:

 void CGContextShowTextAtPoint ( CGContextRef c, CGFloat x, CGFloat y, const char *string, size_t length ); 

but in the 4th argument you pass * NSString ** (and not * const char **, as required by the function prototype).

You can convert NSString to string C using the cStringUsingEncoding method for NSString, for example:

 CGContextShowTextAtPoint(context, 50, 50, [stringFromDate cStringUsingEncoding:NSASCIIStringEncoding]); 
+2
source

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


All Articles