相关疑难解决方法(0)

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
查看次数

可可缺少什么?

如果你可以向Cocoa添加任何东西,它会是什么?是否有任何功能,主要或次要,你会说在Cocoa中缺少.也许有一个轮子你不得不反复发明因为框架中的遗漏?

cocoa cocoa-touch api-design objective-c

13
推荐指数
2
解决办法
1494
查看次数

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

我们今天有一些代码接受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符合要求的任何建议?或者我们甚至愿意接受更好的方式.

objective-c variadic-functions automatic-ref-counting

13
推荐指数
1
解决办法
4689
查看次数

可以在Obj-c中为变量参数函数发送一个数组吗?

在python中,很容易构建一个字典或数组,并将其解压缩到具有可变参数的函数

我有这个:

- (BOOL) executeUpdate:(NSString*)sql, ... {
Run Code Online (Sandbox Code Playgroud)

手动方式是这样的:

[db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)" ,
  @"hi'", // look!  I put in a ', and I'm not escaping it!
  [NSString stringWithFormat:@"number %d", i],
  [NSNumber numberWithInt:i],
  [NSDate date],
  [NSNumber numberWithFloat:2.2f]];
Run Code Online (Sandbox Code Playgroud)

但我不能硬编码我正在调用的参数,我想:

NSMutableArray *values = [NSMutableArray array];

for (NSString *fieldName in props) {
  ..
  ..
  [values addObject : value]
}
[db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, …
Run Code Online (Sandbox Code Playgroud)

objective-c

12
推荐指数
2
解决办法
1万
查看次数