用法一
replaceSubrange(_:with:)
声明
mutating func replaceSubrange<C, R>(
_ subrange: R,
with newElements: C
) where C : Collection, R : RangeExpression, Self.Element == C.Element, Self.Index == R.Bound
参数
subrange
要替换的集合的子范围。范围的边界必须是集合的有效索引。
newElements
要添加到集合中的新元素。
详述
此方法具有从集合中删除指定范围的元素并在同一位置插入新元素的效果。新元素的数量不必与要移除的元素的数量相匹配。
在此示例中,整数数组中间的三个元素被 Repeated<Int>
实例的五个元素替换。
var nums = [10, 20, 30, 40, 50]
nums.replaceSubrange(1...3, with: repeatElement(1, count: 5))
print(nums)
// Prints "[10, 1, 1, 1, 1, 1, 50]"
如果您将零长度范围作为 subrange
参数传递,此方法会在 subrange.startIndex
处插入 newElements
的元素。最好调用 insert(contentsOf:at:)
方法。
同样,如果您将零长度集合作为newElements
参数传递,则此方法将删除给定子范围中的元素而不进行替换。最好调用removeSubrange(_:)
方法。
调用此方法可能会使用于此集合的任何现有索引无效。
可用版本
用法二
replaceSubrange(_:with:)
声明
mutating func replaceSubrange<C>(
_ subrange: Range<Substring.UnicodeScalarView.Index>,
with replacement: C
) where C : Collection, C.Element == Unicode.Scalar
参数
subrange
要替换的集合的子范围。范围的边界必须是集合的有效索引。
newElements
要添加到集合中的新元素。
详述
此方法具有从集合中删除指定范围的元素并在同一位置插入新元素的效果。新元素的数量不必与要移除的元素的数量相匹配。
在此示例中,整数数组中间的三个元素被 Repeated<Int>
实例的五个元素替换。
var nums = [10, 20, 30, 40, 50]
nums.replaceSubrange(1...3, with: repeatElement(1, count: 5))
print(nums)
// Prints "[10, 1, 1, 1, 1, 1, 50]"
如果您将零长度范围作为 subrange
参数传递,此方法会在 subrange.startIndex
处插入 newElements
的元素。最好调用 insert(contentsOf:at:)
方法。
同样,如果您将零长度集合作为newElements
参数传递,则此方法将删除给定子范围中的元素而不进行替换。最好调用removeSubrange(_:)
方法。
调用此方法可能会使用于此集合的任何现有索引无效。
可用版本
相关用法
- Swift Substring.UnicodeScalarView reversed()用法及代码示例
- Swift Substring.UnicodeScalarView reduce(into:_:)用法及代码示例
- Swift Substring.UnicodeScalarView reduce(_:_:)用法及代码示例
- Swift Substring.UnicodeScalarView removeSubrange(_:)用法及代码示例
- Swift Substring.UnicodeScalarView remove(at:)用法及代码示例
- Swift Substring.UnicodeScalarView removeAll(where:)用法及代码示例
- Swift Substring.UnicodeScalarView removeFirst(_:)用法及代码示例
- Swift Substring.UnicodeScalarView removeFirst()用法及代码示例
- Swift Substring.UnicodeScalarView randomElement(using:)用法及代码示例
- Swift Substring.UnicodeScalarView randomElement()用法及代码示例
- Swift Substring.UnicodeScalarView insert(contentsOf:at:)用法及代码示例
- Swift Substring.UnicodeScalarView suffix(from:)用法及代码示例
- Swift Substring.UnicodeScalarView append(_:)用法及代码示例
- Swift Substring.UnicodeScalarView first用法及代码示例
- Swift Substring.UnicodeScalarView append(contentsOf:)用法及代码示例
- Swift Substring.UnicodeScalarView min(by:)用法及代码示例
- Swift Substring.UnicodeScalarView last(where:)用法及代码示例
- Swift Substring.UnicodeScalarView endIndex用法及代码示例
- Swift Substring.UnicodeScalarView starts(with:)用法及代码示例
- Swift Substring.UnicodeScalarView first(where:)用法及代码示例
- Swift Substring.UnicodeScalarView split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代码示例
- Swift Substring.UnicodeScalarView contains(_:)用法及代码示例
- Swift Substring.UnicodeScalarView contains(where:)用法及代码示例
- Swift Substring.UnicodeScalarView min()用法及代码示例
- Swift Substring.UnicodeScalarView isEmpty用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Substring.UnicodeScalarView replaceSubrange(_:with:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。