如何从像@"xxx=%@, yyy=%@"这样的格式字符串创建一个 NSString和一个 NSArray 的对象? [英] How to create a NSString from a format string like @"xxx=%@, yyy=%@" and a NSArray of objects?

查看:27
本文介绍了如何从像@"xxx=%@, yyy=%@"这样的格式字符串创建一个 NSString和一个 NSArray 的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以新建一个NSString 来自像 @"xxx=%@, yyy=%@" 这样的格式字符串和一个 NSArray 对象?

Is there any way to create a new NSString from a format string like @"xxx=%@, yyy=%@" and a NSArray of objects?

在 NSSTring 类中有很多方法,例如:

In the NSSTring class there are many methods like:

- (id)initWithFormat:(NSString *)format arguments:(va_list)argList
- (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList
+ (id)stringWithFormat:(NSString *)format, ...

但他们都没有将 NSArray 作为参数,我找不到从 NSArray 创建 va_list 的方法...

but non of them takes a NSArray as an argument, and I cannot find a way to create a va_list from a NSArray...

推荐答案

从 NSArray 创建 va_list 实际上并不难.请参阅 Matt Gallagher 关于该主题的优秀文章.

It is actually not hard to create a va_list from an NSArray. See Matt Gallagher's excellent article on the subject.

这是一个 NSString 类别来做你想做的事:

Here is an NSString category to do what you want:

@interface NSString (NSArrayFormatExtension)

+ (id)stringWithFormat:(NSString *)format array:(NSArray*) arguments;

@end

@implementation NSString (NSArrayFormatExtension)

+ (id)stringWithFormat:(NSString *)format array:(NSArray*) arguments
{
    char *argList = (char *)malloc(sizeof(NSString *) * arguments.count);
    [arguments getObjects:(id *)argList];
    NSString* result = [[[NSString alloc] initWithFormat:format arguments:argList] autorelease];
    free(argList);
    return result;
}

@end

那么:

NSString* s = [NSString stringWithFormat:@"xxx=%@, yyy=%@" array:@[@"XXX", @"YYY"]];
NSLog( @"%@", s );

不幸的是,对于 64 位,va_list 格式已更改,因此上述代码不再有效.并且可能无论如何都不应该使用它,因为它取决于显然可能会发生变化的格式.鉴于没有真正可靠的方法来创建 va_list,更好的解决方案是简单地将参数数量限制为合理的最大值(比如 10),然后使用前 10 个参数调用 stringWithFormat,如下所示:

Unfortunately, for 64-bit, the va_list format has changed, so the above code no longer works. And probably should not be used anyway given it depends on the format that is clearly subject to change. Given there is no really robust way to create a va_list, a better solution is to simply limit the number of arguments to a reasonable maximum (say 10) and then call stringWithFormat with the first 10 arguments, something like this:

+ (id)stringWithFormat:(NSString *)format array:(NSArray*) arguments
{
    if ( arguments.count > 10 ) {
        @throw [NSException exceptionWithName:NSRangeException reason:@"Maximum of 10 arguments allowed" userInfo:@{@"collection": arguments}];
    }
    NSArray* a = [arguments arrayByAddingObjectsFromArray:@[@"X",@"X",@"X",@"X",@"X",@"X",@"X",@"X",@"X",@"X"]];
    return [NSString stringWithFormat:format, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9] ];
}

这篇关于如何从像@"xxx=%@, yyy=%@"这样的格式字符串创建一个 NSString和一个 NSArray 的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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