gpt4 book ai didi

objective-c - 将 NSString 转换为字符串

转载 作者:搜寻专家 更新时间:2023-10-30 19:45:21 25 4
gpt4 key购买 nike

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

最佳答案

函数的原型(prototype)是:

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

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

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

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

关于objective-c - 将 NSString 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2926726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com