当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Swift DefaultStringInterpolation appendInterpolation(_:)用法及代码示例


用法一

实例方法

appendInterpolation(_:)

将给定值的文本表示插入到正在创建的字符串文字中。

声明

mutating func appendInterpolation<T>(_ value: T) where T : CustomStringConvertible, T : TextOutputStreamable

详述

不要直接调用此方法。编译器在解释字符串插值时使用它。相反,使用字符串插值来创建一个新字符串,方法是在括号中包含值、文字、变量或表达式,并以反斜杠 (\( ... ) ) 为前缀。


let price = 2
let number = 3
let message = """
              If one cookie costs \(price) dollars, \
              \(number) cookies cost \(price * number) dollars.
              """
print(message)
// Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

用法二

实例方法

appendInterpolation(_:)

将给定值的文本表示插入到正在创建的字符串文字中。

声明

mutating func appendInterpolation<T>(_ value: T) where T : TextOutputStreamable

详述

不要直接调用此方法。编译器在解释字符串插值时使用它。相反,使用字符串插值来创建一个新字符串,方法是在括号中包含值、文字、变量或表达式,并以反斜杠 (\( ... ) ) 为前缀。


let price = 2
let number = 3
let message = "If one cookie costs \(price) dollars, " +
              "\(number) cookies cost \(price * number) dollars."
print(message)
// Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

用法三

实例方法

appendInterpolation(_:)

将给定值的文本表示插入到正在创建的字符串文字中。

声明

mutating func appendInterpolation<T>(_ value: T) where T : CustomStringConvertible

详述

不要直接调用此方法。编译器在解释字符串插值时使用它。相反,使用字符串插值来创建一个新字符串,方法是在括号中包含值、文字、变量或表达式,并以反斜杠 (\( ... ) ) 为前缀。


let price = 2
let number = 3
let message = """
              If one cookie costs \(price) dollars, \
              \(number) cookies cost \(price * number) dollars.
              """
print(message)
// Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

用法四

实例方法

appendInterpolation(_:)

将给定值的文本表示插入到正在创建的字符串文字中。

声明

mutating func appendInterpolation<T>(_ value: T)

详述

不要直接调用此方法。编译器在解释字符串插值时使用它。相反,使用字符串插值来创建一个新字符串,方法是在括号中包含值、文字、变量或表达式,并以反斜杠 (\( ... ) ) 为前缀。


let price = 2
let number = 3
let message = """
              If one cookie costs \(price) dollars, \
              \(number) cookies cost \(price * number) dollars.
              """
print(message)
// Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

相关用法


注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 DefaultStringInterpolation appendInterpolation(_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。