coure2011
coure2011

Reputation: 42514

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);

I am not getting the exact date? also getting warning while compiling
warning: passing argument 4 of 'CGContextShowTextAtPoint' from incompatible pointer type

Upvotes: 4

Views: 2134

Answers (2)

Shaggy Frog
Shaggy Frog

Reputation: 27601

What is the value of stringFromDate? What are you expecting?

also getting warning while compiling warning: passing argument 4 of 'CGContextShowTextAtPoint' from incompatible pointer type

If you look at the docs for CGContextShowTextAtPoint, you'll see that the fourth parameter needs to be a char*, not an NSString*.

You have:

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

You want:

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

Upvotes: 1

diciu
diciu

Reputation: 29343

The function's prototype is:

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

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

You can convert the NSString to a C string using the cStringUsingEncoding method of the NSString, e.g.:

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

Upvotes: 2

Related Questions