如何在Objective-C中创建可变参数方法 [英] How to create variable argument methods in Objective-C

查看:210
本文介绍了如何在Objective-C中创建可变参数方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这对你们大多数人来说显然是简单的,但是请你举个例子来说明如何创建类似的方法(在Objective-C中)和C中的函数来创建像 NSString stringWithFormat: NSLog()



提醒:

pre $ [NSString stringWithFormat:@example tekst%i%@%.2f,122 ,@sth,3.1415];
NSLog(@account ID%i email%@,accountID,email);

我想创建类似于 NSString 的方法 stringWithFormat: $ 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

当然,最后一个例子假定va_args列表没有终止。



注意:为了使这项工作成功,您可以包含< ; STDARG.H> ;但是如果内存服务,这将被包含在NSLogv中,这意味着它通过Foundation.h,因此还有AppKit.h和Cocoa.h以及其他一些内容。所以这应该开箱即用。


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 NSString's stringWithFormat:, or NSLog()

Just to remind:

[NSString stringWithFormat:@"example tekst %i %@ %.2f",122,@"sth",3.1415"];
NSLog(@"account ID %i email %@",accountID,email);

I'd like to create the similar to NSString's method stringWithFormat:, NSURL - urlWithFormat.

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 , ..., as in

- (void)logMessage:(NSString *)message, ...;

At this point you probably want to wrap it in a 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);
}

Note the use of NSLogv and not NSLog; consider NSLog(NSString *, ...); vs NSLogv(NSString *, va_list);, or if you want a string; initWithFormat:arguments: on NSString *.


If, on the other hand, you are not working with strings, but rather something like

+ (NSArray *)arrayWithObjects:(id)object, ... NS_REQUIRES_NIL_TERMINATION;

things get a lot easier.

In that case, instead of a 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);
}

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 <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.

这篇关于如何在Objective-C中创建可变参数方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆