gpt4 book ai didi

Swift 字符串初始值设定项?

转载 作者:行者123 更新时间:2023-11-28 09:06:58 25 4
gpt4 key购买 nike

我在一本 Swift 教学书中看到了这两个语句。

  1. let padding = String(count: spaces, repeatedValue: Character(""))
  2. println(String(format:"%@%@", "Year", "Test"))

我的问题是:

  1. 都是 String(count:repeatedValue:)String(format:) 的初始化器
    字符串?
  2. 我找不到 String(format:) 的联机文档你能推荐任何链接吗?
  3. 还有哪些可用的 String 初始值设定项?我只能找到 init()和 Apple 文档中的 init(count:repeatedValue)

非常感谢。

最佳答案

String 是 Swift 中的结构体。您可以在 Xcode 本身中获取文档。

下面是详细信息。

/// Objective-C Bridge /// ================== /// /// String is bridged to Objective-C as NSString, and a String /// that originated in Objective-C may store its characters in an /// NSString. Since any arbitrary subclass of NSSString can /// become a String, there are no guarantees about representation or /// efficiency in this case. Since NSString is immutable, it is /// just as though the storage was shared by some copy: the first in /// any sequence of mutating operations causes elements to be copied /// into unique, contiguous storage which may cost O(N) time and /// space, where N is the length of the string representation (or /// more, if the underlying NSString is has unusual performance /// characteristics).

struct String {
init()
}

这里是字符串的扩展。

extension String {

/// Construct an instance that is the concatenation of `sz` copies
/// of `repeatedValue`
init(count sz: Int, repeatedValue c: Character)

/// Construct an instance that is the concatenation of `sz` copies
/// of `Character(repeatedValue)`
init(count: Int, repeatedValue c: UnicodeScalar)

/// `true` iff `self` contains no characters.
var isEmpty: Bool { get }
}

关于Swift 字符串初始值设定项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30557211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com