NSInteger到const char *

时间:2012-10-23 18:08:42

标签: objective-c cgcontext uiprintpagerenderer

我递了(NSInteger) pageIndex,需要在

的页面上打印

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

如何获取const char *string而不忘记该字符串的长度

我倾向于最终使用令人讨厌的技巧将整数转换为字符串,然后执行cStringUsingEncoding:NSMacOSRomanStringEncoding但这不是最优雅的方式

为了完整性,这是代码

const char *pageIndexString = [[NSString stringWithFormat:@"%d", pageIndex] cStringUsingEncoding:NSMacOSRomanStringEncoding];
CGContextShowTextAtPoint(CGContextRef, CGFloat x, CGFloat y, pageIndexString, strlen(pageIndexString));

1 个答案:

答案 0 :(得分:3)

试试这个:

NSString *tmp = [NSString stringWithFormat:@"%ld", pageIndex];
const char *str = [tmp UTF8String];
size_t length = [tmp length];