繁体   English   中英

将NSString转换为字符串

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

我没有得到具体日期? 在编译时也会收到警告
警告:从不兼容的指针类型传递'CGContextShowTextAtPoint'的参数4

该函数的原型是:

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

但是在第4个参数中,您传递的是* NSString **(而不是函数原型所需的* const char **)。

您可以使用NSString的cStringUsingEncoding方法将NSString转换为C字符串,例如:

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

stringFromDate的值是stringFromDate 你有什么期待?

编译警告时也会收到警告:从不兼容的指针类型传递'CGContextShowTextAtPoint'的参数4

如果查看CGContextShowTextAtPoint的文档,您将看到第四个参数需要是char* ,而不是NSString*.

你有:

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

你要:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM