我无法理解Objective-C中多个参数的语法.我已经看到了这个问题,但答案并没有帮助我(还).
这是我的代码(实际上我希望最终传递给NSString stringWithFormat,但是让NSLog工作现在已经足够了):
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[self log:@"blah blah %d", 32];
}
- (void)log:(NSString *)text, ... {
va_list args;
va_start(args, text);
NSLog(text, args);
}
Run Code Online (Sandbox Code Playgroud)
参数(或一些参数)通过,但它有一些奇怪的值(输出是blah blah 1606412704).我应该如何传递通过的值...?
Dav*_*ong 21
有一种变体NSLog可以接受一个va_list被叫NSLogv:
- (void) log:(NSString *)text, ... {
va_list args;
va_start(args, text);
NSLogv(text, args);
va_end(args);
}
Run Code Online (Sandbox Code Playgroud)
转发实际...(不是va_list)的唯一方法是使用宏.例如:
#define MyLog(f, ...) { \
NSLog(f, ##__VA_ARGS__); \
[someObject doSomething:f, ##__VA_ARGS__]; \
}
Run Code Online (Sandbox Code Playgroud)
但是,这应该非常谨慎地使用,因为宏可以使代码真正混淆.
mip*_*adi 12
你可以使用-[NSString initWithFormat:arguments:]:
- (void)log:(NSString *)text, ...
{
va_list args;
va_start(args, text);
NSString *log_msg = [[[NSString alloc] initWithFormat:text arguments:args] autorelease];
NSLog(@"%@", log_msg);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5233 次 |
| 最近记录: |