Error[8]: Undefined offset: 7, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述在 Python中,我可以创建一个这样的重复列表: >>> [1,2,3]*3[1, 2, 3, 1, 2, 3, 1, 2, 3] 在Swift中有一个简洁的方法吗? 我能做的最好的事情是: 1> var r = [Int]()r: [Int] = 0 values 2> for i in 1...3 { 3. r += [1,2,3] 4. } 5> print( 在 Python中,我可以创建一个这样的重复列表:

>>> [1,2,3]*3[1,3,1,3]

在Swift中有一个简洁的方法吗?

我能做的最好的事情是:

1> var r = [Int]()r: [Int] = 0 values  2> for i in 1...3 {   3.     r += [1,3]  4. }  5> print(r)[1,3]
解决方法 您可以创建一个2D数组,然后使用flatMap将其转换为一维数组:

let array = [Int](repeating: [1,3],count: 3).flatMap{
extension Array {  init(repeating: [Element],count: Int) {    self.init([[Element]](repeating: repeating,count: count).flatMap{
let array = Array(repeating: [1,count: 3) // Error: Ambiguous use of ‛init(repeating:count:)‛
}) } func repeated(count: Int) -> [Element] { return [Element](repeating: self,count: count) }}let array = [1,3].repeated(count: 3) // => [1,3]
}

这是一个扩展,它添加了一个init方法和一个重复方法,该方法采用一个使得它更清洁的数组:

let array = [Int](repeating: [1,count: 3) // => [1,3]

请注意,使用新的初始化程序,如果在不提供预期类型的​​情况下使用它,则可以获得模糊的方法调用:

let array:[Int] = Array(repeating: [1,3]

改为使用:

[+++]

要么

[+++]

如果将方法签名更改为init(repeatingContentsOf:[Element],count:Int)或类似方法,则可以避免这种歧义.

总结

以上是内存溢出为你收集整理的数组 – 在Swift中重复数组全部内容,希望文章能够帮你解决数组 – 在Swift中重复数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 8, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述在 Python中,我可以创建一个这样的重复列表: >>> [1,2,3]*3[1, 2, 3, 1, 2, 3, 1, 2, 3] 在Swift中有一个简洁的方法吗? 我能做的最好的事情是: 1> var r = [Int]()r: [Int] = 0 values 2> for i in 1...3 { 3. r += [1,2,3] 4. } 5> print( 在 Python中,我可以创建一个这样的重复列表:

>>> [1,2,3]*3[1,3,1,3]

在Swift中有一个简洁的方法吗?

我能做的最好的事情是:

1> var r = [Int]()r: [Int] = 0 values  2> for i in 1...3 {   3.     r += [1,3]  4. }  5> print(r)[1,3]
解决方法 您可以创建一个2D数组,然后使用flatMap将其转换为一维数组:

let array = [Int](repeating: [1,3],count: 3).flatMap{
extension Array {  init(repeating: [Element],count: Int) {    self.init([[Element]](repeating: repeating,count: count).flatMap{
let array = Array(repeating: [1,count: 3) // Error: Ambiguous use of ‛init(repeating:count:)‛
}) } func repeated(count: Int) -> [Element] { return [Element](repeating: self,count: count) }}let array = [1,3].repeated(count: 3) // => [1,3]
}

这是一个扩展,它添加了一个init方法和一个重复方法,该方法采用一个使得它更清洁的数组:

let array = [Int](repeating: [1,count: 3) // => [1,3]

请注意,使用新的初始化程序,如果在不提供预期类型的​​情况下使用它,则可以获得模糊的方法调用:

let array:[Int] = Array(repeating: [1,3]

改为使用:

要么

[+++]

如果将方法签名更改为init(repeatingContentsOf:[Element],count:Int)或类似方法,则可以避免这种歧义.

总结

以上是内存溢出为你收集整理的数组 – 在Swift中重复数组全部内容,希望文章能够帮你解决数组 – 在Swift中重复数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
数组 – 在Swift中重复数组_app_内存溢出

数组 – 在Swift中重复数组

数组 – 在Swift中重复数组,第1张

概述在 Python中,我可以创建一个这样的重复列表: >>> [1,2,3]*3[1, 2, 3, 1, 2, 3, 1, 2, 3] 在Swift中有一个简洁的方法吗? 我能做的最好的事情是: 1> var r = [Int]()r: [Int] = 0 values 2> for i in 1...3 { 3. r += [1,2,3] 4. } 5> print( 在 Python中,我可以创建一个这样的重复列表:

>>> [1,2,3]*3[1,3,1,3]

在Swift中有一个简洁的方法吗?

我能做的最好的事情是:

1> var r = [Int]()r: [Int] = 0 values  2> for i in 1...3 {   3.     r += [1,3]  4. }  5> print(r)[1,3]
解决方法 您可以创建一个2D数组,然后使用flatMap将其转换为一维数组:

let array = [Int](repeating: [1,3],count: 3).flatMap{
extension Array {  init(repeating: [Element],count: Int) {    self.init([[Element]](repeating: repeating,count: count).flatMap{
let array = Array(repeating: [1,count: 3) // Error: Ambiguous use of ‛init(repeating:count:)‛
}) } func repeated(count: Int) -> [Element] { return [Element](repeating: self,count: count) }}let array = [1,3].repeated(count: 3) // => [1,3]
}

这是一个扩展,它添加了一个init方法和一个重复方法,该方法采用一个使得它更清洁的数组:

let array = [Int](repeating: [1,count: 3) // => [1,3]

请注意,使用新的初始化程序,如果在不提供预期类型的​​情况下使用它,则可以获得模糊的方法调用:

let array:[Int] = Array(repeating: [1,3]

改为使用:

要么

如果将方法签名更改为init(repeatingContentsOf:[Element],count:Int)或类似方法,则可以避免这种歧义.

总结

以上是内存溢出为你收集整理的数组 – 在Swift中重复数组全部内容,希望文章能够帮你解决数组 – 在Swift中重复数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1209375.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存