开发者

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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜