使用可变数量的键和格式构建字符串

发布于 2024-11-10 13:03:59 字数 423 浏览 1 评论 0原文

我有一个包含我的数据的 NSDictionary 对象。我正在传递一个键名称数组和一个数据字符串表示形式的显示格式。

[self displayMyDataWithTheseKeys:myKeyArray inThisFormat:myFormat];

例如,

myKeyArray = [NSArray arrayWithObjects: @"Key1", @"Key2", nil];

myFormat = [NSString stringWithString: @"%@ to the %@ degree"];

但是,myFormat 可能会发生变化,并且数组中的键数也可能会发生变化。

如果数组中的元素数量始终为 2,那么这将是微不足道的。但是,如何处理可变数量的元素?

I have an NSDictionary object that contains my data. I am passing in an array of key names and a display format for a string representation of my data.

[self displayMyDataWithTheseKeys:myKeyArray inThisFormat:myFormat];

where, for example,

myKeyArray = [NSArray arrayWithObjects: @"Key1", @"Key2", nil];

myFormat = [NSString stringWithString: @"%@ to the %@ degree"];

However, myFormat may change and the number of keys in the array may vary as well.

If the number of elements in the array was always 2, this would be trivial. However, how can I handle a variable number of elements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2024-11-17 13:03:59

实际上并没有一个内置方法可以实现此目的,但使用 NSScanner 解析格式字符串相对容易。这是一个简单的示例,它仅处理 %@ 格式说明符,但由于 NSArray 中的所有元素都是对象而不是原始类型,因此这应该不重要:

NSArray *myKeyArray = [NSArray arrayWithObjects: @"Key1", @"Key2", nil];
NSString *myFormat = [NSString stringWithString: @"%@ to the %@ degree"];

NSMutableString *result = [NSMutableString string];
NSScanner *scanner = [NSScanner scannerWithString:myFormat];
[scanner setCharactersToBeSkipped:[NSCharacterSet illegalCharacterSet]];
int i = 0;
while (![scanner isAtEnd]) {
    BOOL scanned = [scanner scanString:@"%@" intoString:NULL];
    if (scanned) {
        if (i < [myKeyArray count]) {
            [result appendString:[myKeyArray objectAtIndex:i]];
            i++;
        } else {
            //Handle error: Number of format specifiers doesn't 
            //match number of keys in array...
        }
    }
    NSString *chunk = nil;
    [scanner scanUpToString:@"%@" intoString:&chunk];
    if (chunk) {
        [result appendString:chunk];
    }
}

There isn't really a built-in method for this, but it's relatively easy to parse format strings with NSScanner. Here's a simple example, it only handles %@ format specifiers, but as all elements in an NSArray are objects and not primitive types anyway, it shouldn't matter:

NSArray *myKeyArray = [NSArray arrayWithObjects: @"Key1", @"Key2", nil];
NSString *myFormat = [NSString stringWithString: @"%@ to the %@ degree"];

NSMutableString *result = [NSMutableString string];
NSScanner *scanner = [NSScanner scannerWithString:myFormat];
[scanner setCharactersToBeSkipped:[NSCharacterSet illegalCharacterSet]];
int i = 0;
while (![scanner isAtEnd]) {
    BOOL scanned = [scanner scanString:@"%@" intoString:NULL];
    if (scanned) {
        if (i < [myKeyArray count]) {
            [result appendString:[myKeyArray objectAtIndex:i]];
            i++;
        } else {
            //Handle error: Number of format specifiers doesn't 
            //match number of keys in array...
        }
    }
    NSString *chunk = nil;
    [scanner scanUpToString:@"%@" intoString:&chunk];
    if (chunk) {
        [result appendString:chunk];
    }
}
樱花落人离去 2024-11-17 13:03:59

使用: stringByAppendingString

以下是如何使用它的示例:

NSString *someString = @"String";

someString = [someString stringByAppendingString:[NSString stringWithFormat:@"%@",variable1]];
someString = [someString stringByAppendingString:[NSString stringWithFormat:@"%@",variable2]];
someString = [someString stringByAppendingString:[NSString stringWithFormat:@"%@",variable3]];

...等等

如果您有一个键数组,您可以使用它想要放入一个字符串:

NSString *string = @"And the keys are:\n";

    for(int i = 0; i < [array count]; i++)
    {
        NSString *thisKey = (NSString *)[array objectAtIndex:i];

        string = [string stringByAppendingString:[NSString stringWithFormat:@"Key number %d is %@",i,thisKey]];
    }

Use: stringByAppendingString

Here's an example on how to use it:

NSString *someString = @"String";

someString = [someString stringByAppendingString:[NSString stringWithFormat:@"%@",variable1]];
someString = [someString stringByAppendingString:[NSString stringWithFormat:@"%@",variable2]];
someString = [someString stringByAppendingString:[NSString stringWithFormat:@"%@",variable3]];

...and so on

If you have an array of keys which you want to put in a string:

NSString *string = @"And the keys are:\n";

    for(int i = 0; i < [array count]; i++)
    {
        NSString *thisKey = (NSString *)[array objectAtIndex:i];

        string = [string stringByAppendingString:[NSString stringWithFormat:@"Key number %d is %@",i,thisKey]];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文