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

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

问题描述

有没有办法从格式字符串如@xxx =%@,yyy =%@和对象的NSArray创建一个新的
NSString?



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

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

但是没有一个使用NSArray作为参数,我找不到一种方法来创建来自NSArray的va_list ...

解决方案

从NSArray创建一个va_list实际上并不困难。请参阅Matt Gallagher的优秀文章。 / p>

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

  @接口NSString(NSArrayFormatExtension)

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

@end

@implementation NSString(NSArrayFormatExtension)

+(id)stringWithFormat:(NSString *)format array:
{
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 =%@数组:@ [@XXX,@ YYY]]; 
NSLog(@%@,s);不幸的是,对于64位,va_list格式已经改变,所以上面的代码不再工作了。并且可能不应该使用反正给它取决于格式,显然可以改变。给定没有真正可靠的方法来创建一个va_list,一个更好的解决方案是简单地将参数的数量限制在一个合理的最大值(例如10),然后调用stringWithFormat与前10个参数,像这样:

  +(id)stringWithFormat:(NSString *)format array:(NSArray *)arguments 
{
if(arguments。计数> 10){
@throw [NSException exceptionWithName:NSRangeException原因:@允许最多10个参数userInfo:@ {@collection:arguments}
}
NSArray * a = [arguments arrayByAddingObjectsFromArray:@ [@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 [8],a [9],a [10]];
}


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

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

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

解决方案

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

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

Then:

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

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], a[10] ];
}

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

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