如何在Objective-C中创建可变参数方法 [英] How to create variable argument methods in Objective-C
问题描述
NSString
的 stringWithFormat:
或 NSLog()
提醒:
pre $
[NSString stringWithFormat:@example tekst%i%@%.2f,122 ,@sth,3.1415];
NSLog(@account ID%i email%@,accountID,email);
我想创建类似于 用 在这一点上,你可能想把它包装在一个 使用 另一方面,如果您不使用字符串,而是像 事情变得容易多了。 在这种情况下,不要使用 当然,最后一个例子假定va_args列表没有终止。 注意:为了使这项工作成功,您可以包含 maybe this will be obviously simple for most of you, but could you please give an example how to create similar methods (in objective-C) and functions in C to create functions like Just to remind: I'd like to create the similar to Thank you in advance. What these are called, generally, is "variadic functions" (or methods, as it were). To create this, simply end your method declartion with At this point you probably want to wrap it in a Note the use of If, on the other hand, you are not working with strings, but rather something like things get a lot easier. In that case, instead of a This last sample, of course, assumes that the va_args list is nil-terminated. Note: In order to make this work you might have to include 这篇关于如何在Objective-C中创建可变参数方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! NSString
的方法 stringWithFormat:$ c $
$ b $ b $ b $ b
NSURL - urlWithFormat
一般来说,这些被称为可变参数函数(或方法,因为它)。
,...
来创建这个方法,就像在
中一样 end logMessage:(NSString *)message,...;
printf
类似的函数中,从头开始尝试,最好。
$ p $ - (void)logMessage:(NSString *)格式,... {
va_list args;
va_start(args,format);
NSLogv(format,args);
va_end(args);
$ / code> NSLogv
而不是 NSLog
;考虑 NSLog(NSString *,...);
vs NSLogv(NSString *,va_list);
想要一个字符串; initWithFormat:参数:
在 NSString *
。
+(NSArray *)arrayWithObjects:(id)object,... NS_REQUIRES_NIL_TERMINATION;
vprintf
风格的函数,而是使用循环 args
,并在任何循环中解析它们。
- (void)logMessage:(NSString *)format,... {
va_list args;
va_start(args,format);
id arg = nil;
while((arg = va_arg(args,id))){
///用arg在这里做你的事情
}
va_end(args);
$ b
< ; STDARG.H>
;但是如果内存服务,这将被包含在NSLogv中,这意味着它通过Foundation.h,因此还有AppKit.h和Cocoa.h以及其他一些内容。所以这应该开箱即用。NSString
's stringWithFormat:
, or NSLog()
[NSString stringWithFormat:@"example tekst %i %@ %.2f",122,@"sth",3.1415"];
NSLog(@"account ID %i email %@",accountID,email);
NSString
's method stringWithFormat:
, NSURL - urlWithFormat
., ...
, as in - (void)logMessage:(NSString *)message, ...;
printf
-like function, as implementing one of those from scratch is trying, at best.- (void)logMessage:(NSString *)format, ... {
va_list args;
va_start(args, format);
NSLogv(format, args);
va_end(args);
}
NSLogv
and not NSLog
; consider NSLog(NSString *, ...);
vs NSLogv(NSString *, va_list);
, or if you want a string; initWithFormat:arguments:
on NSString *
.
+ (NSArray *)arrayWithObjects:(id)object, ... NS_REQUIRES_NIL_TERMINATION;
vprintf
-style function, use a loop going through args
, assuming id as you go, and parse them as you would in any loop.- (void)logMessage:(NSString *)format, ... {
va_list args;
va_start(args, format);
id arg = nil;
while ((arg = va_arg(args,id))) {
/// Do your thing with arg here
}
va_end(args);
}
<stdarg.h>
; but if memory serves, this gets included in connection with NSLogv, meaning it comes down by way of "Foundation.h", therefore also "AppKit.h" and "Cocoa.h", as well as a number of others; so this should work out of the box.