将NSArray内容转换为varargs(使用ARC)以与NSString initWithFormat一起使用

Gar*_*lph 13 objective-c variadic-functions automatic-ref-counting

我们今天有一些代码接受NSArray并将其作为参数列表传递给 - [NSString initWithFormat:arguments],我们试图让它与ARC一起工作.这是代码使用的

NSString* format = @"Item %s and Item %s"; // Retrieved elsewhere
NSArray* args = [NSArray arrayWithObjects:@"1", @"2", nil]; // Retrieved elsewhere

// http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html
char* argsList = (char*) malloc(sizeof(NSString*) * args.count);
[args getObjects:(id*) argsList];
NSString* message = [[[NSString alloc] initWithFormat:format arguments:argsList] autorelease];
free(argsList);
Run Code Online (Sandbox Code Playgroud)

有关如何使ARC符合要求的任何建议?或者我们甚至愿意接受更好的方式.

tar*_*mes 0

您唯一需要做的就是删除自动释放。

你自己进行 malloc 和 free 操作 - ARC 并不关心这一点。