Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 是否有使用Swift格式的字符串函数字符串(格式:…)的等效项_String_Swift - Fatal编程技术网

String 是否有使用Swift格式的字符串函数字符串(格式:…)的等效项

String 是否有使用Swift格式的字符串函数字符串(格式:…)的等效项,string,swift,String,Swift,我开始喜欢Swift字符串格式,因为它在字符串中使用变量名,而不是像“%@”这样模棱两可的格式标记 我想从一个包含Swift格式的文件中加载一个大字符串(如下所示) 然后我想将该字符串变量的内容输入到一个语句中,以备替换 \(who) 在运行时使用常量/变量的内容 下面的代码使用字符串常量作为格式化字符串 let who = "programmers" let aString = "Now is the time for all good \(who) to come to babble in

我开始喜欢Swift字符串格式,因为它在字符串中使用变量名,而不是像“%@”这样模棱两可的格式标记

我想从一个包含Swift格式的文件中加载一个大字符串(如下所示)

然后我想将该字符串变量的内容输入到一个语句中,以备替换

\(who)
在运行时使用常量/变量的内容

下面的代码使用字符串常量作为格式化字符串

let who = "programmers"
let aString = "Now is the time for all good \(who) to come to babble incoherently."
该代码对代码中出现的带引号的字符串进行格式化

相反,我想要类似代码的东西

let formatString = "Now is the time for all good %@ to come to babble incoherently."
aString = String(format: formatString, who)
但我可以通过从文件中读取的常量/变量传递Swift格式的字符串。 可能吗?我没有任何运气搜索它,因为我不太确定使用什么搜索词


如果有必要,我总是可以使用C风格的字符串格式和字符串类'initWithFormat方法…

我不这么认为。编译器需要能够在编译时解析插值变量。

我认为没有办法做到这一点。字符串插值是通过遵守
StringInterpolationConvertible
协议来实现的,您可能希望以与使用
StringLiteralConvertible
所需的方法相同的方式使用该协议,即:

let someString = toString(42)

// this is the method String implements to conform to StringLiteralConvertible
let anotherString = String(stringLiteral: someString)

// anotherString will be "42"
print(anotherString)
不幸的是,您不能用
StringInterpolationConvertible
完成相同的技巧。了解协议的工作原理可能有助于:

struct MyString: Printable {
    let actualString: String
    var description: String { return actualString }
}

extension MyString: StringInterpolationConvertible {
    // first, this will get called for each "segment"
    init<T>(stringInterpolationSegment expr: T) {
        println("Processing segment: " + toString(expr))
        actualString = toString(expr)
    }
    // here is a type-specific override for Int, that coverts
    // small numbers into words:
    init(stringInterpolationSegment expr: Int) {
        if (0..<4).contains(expr) {
            println("Embigening \(expr)")
            let numbers = ["zeo","one","two","three"]
            actualString = numbers[expr]
        }
        else {
            println("Processing segment: " + toString(expr))
            actualString = toString(expr)
        }
    }
    // finally, this gets called with an array of all of the
    // converted segments
    init(stringInterpolation strings: MyString...) {
        // strings will be a bunch of MyString objects
        actualString = "".join(strings.map { $0.actualString })
    }
}

let number = 3
let aString: MyString = "Then shalt thou count to \(number), no more, no less."
println(aString)
// prints "Then shalt thou count to three, no more, no less."
struct MyString:可打印{
让actualString:字符串
变量说明:字符串{return actualString}
}
扩展MyString:StringInterpolationConvertible{
//首先,将为每个“段”调用此函数
初始(stringInterpolationSegment表达式:T){
println(“处理段:+toString(expr))
actualString=toString(expr)
}
//这里是Int的一个特定类型重写,它将
//将小数字转换为文字:
初始化(stringInterpolationSegment表达式:Int){

特别是,如果(0..我不是一个Swift程序员,但我认为您可以使用标准的字符串替换和拆分方法来解决这个问题:

var replacement = [String: String]()
replacement["who"] = "programmers"

有了这些,你可以试着找到“\(”,读取a“)”之前和之后的内容,(有助于分割部分和替换部分),在字典中找到它,并从你得到的片段中重建你的字符串。

这一个很有魅力:

let who = "programmers"
let formatString = "Now is the time for all good %@ to come to babble incoherently."
let aString = String(format: formatString, who)

谢谢空速。这是我想的,但我认为值得一问。遗憾的是,因为Swift字符串格式语法已经命名了参数。这会让你的意图比一个包含大量
%@
等的大字符串球更清楚。我同意,这会很好。也许你永远不知道,你可以申请雷达。而且我猜它们会扩展到interpolation将在将来覆盖更多的格式,因此可能值得请求。我会将更高的优先级放在使NSDates可比较相等以及能够在switch语句中列出元组常量上。我已经编写了扩展来实现这两个功能,但在我看来,这些功能应该融入到语言中……实际上,我想你写的代码让我可以在switch语句中使用元组常量。我真的应该说“我找到了扩展…”这不是我要问的。我要问的是,是否有一种方法可以使用Swift样式的字符串格式,比如
“现在是时候让所有优秀的\(personClass)语无伦次地唠叨了。”
AFAIK-不,因为命名变量。上面的问题是什么?好吧,这是我问题的全部要点。回去重新阅读我的问题。我清楚地陈述了目标。
let who = "programmers"
let formatString = "Now is the time for all good %@ to come to babble incoherently."
let aString = String(format: formatString, who)