【问题标题】:Take a look at this code and let me know if there's anything I can change看看这段代码,如果有什么我可以改变的,请告诉我
【发布时间】:2014-10-05 13:24:28
【问题描述】:

我昨天问了一个问题,得到了一个很棒的答案,解决了我的大部分问题。我试图将特定的数值与以 12 为增量分配的 11 种颜色以及每 12 次增量重复的形状配对。

ex: 0:black:circle, 1:black:cross, 2:black:star...12
    0:brown:circle, 1:brown:cross, 2:brown:star...12
    0:red:circle,   1:red:cross,   2:red:star...12

以此类推,直到每个数字都被分配给一个颜色和形状。下面的代码就是这样做的。但它以一种我没想到的方式做到了,输出如下。

struct ValueStruct {
var numValue: Int
var color: String
var shape: String

init(numValue: Int, color: String, shape: String) {
    self.numValue = numValue
    self.color = color
    self.shape = shape
  }
}

var startingNumValue = 0
let colorsArray = ["Black", "Brown", "Red", "Yellow", "Orange", "Green", "Grey", "Blue", "Purple", "Pink", "White"]
let shapesArray = ["Circle", "Cross", "Star", "Rectangle", "Triangle", "Square", "Heart", "Crown", "Line", "Diamond", "Ellipse", "Sun"]


var containingArray:[ValueStruct] = []

for colorItems in colorsArray {
for shapeItems in shapesArray {
    containingArray.append(ValueStruct(numValue: startingNumValue, color: colorItems, shape: shapeItems))
    startingNumValue += 1
}

这是在操场上输出的样子,所以有几个问题。

1) 这是最简洁的方法吗?正常循环的输出通常都在一个窗口中,并且看起来这是以一种停止而不是重新开始直到完成的方式循环。

2) 有没有办法设置startingNumValue 的上限,我只需要将它设置为 128,我担心以后可能出现错误。

3) 最后,这在操场上运行良好,但从常规项目的for colorItems in colorsArray 行开始,它会生成Statements are not allowed at the top level 错误,有什么建议可以解决这个问题吗?

【问题讨论】:

  • Re #2:11 种颜色和 12 份共享构成 11*12 = 132 种可能的组合。您如何期望仅使用 128 个数字对其进行编码?
  • 我知道我只是想尽可能停止计数。

标签: ios swift xcode6 swift-playground


【解决方案1】:

我添加了一个 if 语句来将您的数组限制为 128 个项目。在项目中,变量赋值以外的代码需要在函数中。试试这个:

import UIKit

struct ValueStruct {
    var numValue: Int
    var color: String
    var shape: String

    init(numValue: Int, color: String, shape: String) {
        self.numValue = numValue
        self.color = color
        self.shape = shape
    }
}

class ViewController: UIViewController, UITableViewDelegate {

    var startingNumValue = 0
    let colorsArray = ["Black", "Brown", "Red", "Yellow", "Orange", "Green", "Grey", "Blue", "Purple", "Pink", "White"]
    let shapesArray = ["Circle", "Cross", "Star", "Rectangle", "Triangle", "Square", "Heart", "Crown", "Line", "Diamond", "Ellipse", "Sun"]
    var containingArray:[ValueStruct] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        for colorItems in colorsArray {
            for shapeItems in shapesArray {
                if startingNumValue < 128 {
                    containingArray.append(ValueStruct(numValue: startingNumValue, color: colorItems, shape: shapeItems))
                    startingNumValue += 1
                }
            }
        }
        println(startingNumValue)
    }
}

【讨论】:

  • 不客气。您也可以接受答案。点击投票向上/向下箭头下方的复选标记。
  • 等着看有没有其他人有什么要补充的
【解决方案2】:

另一种选择是使用单个 for 循环,并使用模数算法从数组中进行选择,如下所示:

for i in 0..<128 {
    let colorIndex = i % (colorsArray.count)
    let shapesIndex = i / colorsArray.count
    containingArray.append(ValueStruct(numValue: i, color: colorsArray[colorIndex], shape: shapesArray[shapesIndex]))
}

但要对此进行案例强化,您需要注意不要超出 shapeArray 的范围。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-21
    • 2020-10-30
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多