相关疑难解决方法(0)

如何从像@"xxx =%@,yyy =%@"和NSArray对象这样的格式字符串创建NSString?

有没有办法从像@"xxx =%@,yyy =%@"和NSArray对象这样的格式字符串创建一个新的NSString?

在NSSTring类中有许多方法,如:

- (id)initWithFormat:(NSString *)format arguments:(va_list)argList
- (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList
+ (id)stringWithFormat:(NSString *)format, ...
Run Code Online (Sandbox Code Playgroud)

但是没有它们将NSArray作为参数,我找不到从NSArray创建va_list的方法......

cocoa objective-c string-formatting nsstring nsarray

30
推荐指数
3
解决办法
5万
查看次数

ARC中的虚假va_list

我需要在iOS应用程序中创建一个假va_list的传递给NSString initWithFormat:arguments:函数,这是我的代码:

NSArray *fixedArguments = [[NSArray alloc] initWithArray:arguments]; 

NSRange range = NSMakeRange(0, [fixedArguments count]);

va_list fakeArgList = (va_list)malloc(sizeof(NSString *) * [fixedArguments count]);

__unsafe_unretained id *ptr = (__unsafe_unretained id *)fakeArgList;

[fixedArguments getObjects:ptr range:range];

content = [[NSString alloc] initWithFormat:outputFormat
                                            arguments:(va_list)fakeArgList];
free(fakeArgList);
Run Code Online (Sandbox Code Playgroud)

编译器在转换线上抱怨此消息:

error: cast of a non-Objective-C pointer type 'va_list' (aka 'char *') to '__unsafe_unretained id *' is disallowed with ARC
Run Code Online (Sandbox Code Playgroud)

getObjects:range:功能定义如下:

- (void)getObjects:(id __unsafe_unretained [])objects range:(NSRange)range;
Run Code Online (Sandbox Code Playgroud)

我已经尝试了一切,但仍然无法摆脱这个错误......

是否有va_list启用ARC 创建假的解决方案?我究竟做错了什么?

objective-c variadic-functions ios automatic-ref-counting

17
推荐指数
1
解决办法
6046
查看次数