stringWithFormat中的多个参数:“n $”位置说明符 [英] Multiple arguments in stringWithFormat: “n$” positional specifiers

查看:168
本文介绍了stringWithFormat中的多个参数:“n $”位置说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们当前的实现中,我们想要更改我们的字符串参数(推送通知loc-args)并添加新参数。但我们希望旧版本的用户仍然使用参数#3,而对于新用户,我们希望使用参数#4。所以在我们的新实现中,我们有以下代码:

In our current implementation we want to change our string arguments (Push notification loc-args) and add new arguments. But we want user's of our old Versions to still use argument #3 and for new user's we want to user argument #4. So in our new implementation we have following code :

NSString *format = @"%2$@,  %1$@  ,%4$@";
NSArray *arg = @[@"Argument 1", @"Argument 2",@"Argument 3",@"Argument 4"];
NSString *ouptput = [NSString stringWithFormat:format, arg[0], arg[1], arg[2], arg[3]];




OutPut:参数2,参数1,参数3

OutPut: Argument 2, Argument 1 ,Argument 3

我们预期它是


参数2,参数1,参数4

Argument 2, Argument 1 ,Argument 4

我们如何实现 Argument 4 stringWithFormat的任何其他选择:

注意:Apple锁定屏幕推送通知是正确的(参数2,参数1,参数4 )但是 stringWithFormat:没有那样处理

Note: Apple lock screen push notification is correct (Argument 2, Argument 1 ,Argument 4) but stringWithFormat: not handles it that way

推荐答案

我实现了一个自定义方法来实现预期的输出。此方法可以处理缺少的位置说明符。此方法仅适用于包含位置说明符%n $ @ 的格式。

I implemented a custom method to achieve expected output. This method can handle missing positional specifier. This method will only work for format containing positional specifier %n$@.

/**
 @param format String format with positional specifier
 @param arg Array of arguments to replace positional specifier in format
 @return Formatted output string
 */
+(NSString*)stringWithPositionalSpecifierFormat:(NSString*)format arguments:(NSArray*)arg
{
    static NSString *pattern = @"%\\d\\$@";

    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

    NSMutableString *mString = [[NSMutableString alloc] initWithString:format];
    NSArray *allMatches = [regex matchesInString:format options:0 range:NSMakeRange(0, [format length])];
    if (!error && allMatches>0)
    {
        for (NSTextCheckingResult *aMatch in allMatches)
        {
            NSRange matchRange = [aMatch range];
            NSString *argPlaceholder = [format substringWithRange:matchRange];
            NSMutableString *position = [argPlaceholder mutableCopy];
            [position replaceOccurrencesOfString:@"%" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [position length])];
            [position replaceOccurrencesOfString:@"$@" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [position length])];
            int index = position.intValue;
            //Replace with argument
            [mString replaceOccurrencesOfString:argPlaceholder withString:arg[index-1] options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mString length])];
        }
    }
    return mString;
}

这篇关于stringWithFormat中的多个参数:“n $”位置说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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