Ios 请查看此代码,并告诉我是否存在';有什么我可以改变的吗

Ios 请查看此代码,并告诉我是否存在';有什么我可以改变的吗,ios,swift,xcode6,swift-playground,Ios,Swift,Xcode6,Swift Playground,昨天我问了一个问题,得到了一个很棒的答案,解决了我的大部分问题。我试图将特定的数值与11种颜色配对,这些颜色以12的增量分配,形状每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 依此类推,直到每个数字都被指定为

昨天我问了一个问题,得到了一个很棒的答案,解决了我的大部分问题。我试图将特定的数值与11种颜色配对,这些颜色以12的增量分配,形状每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) 最后,这在操场上很好用,但从colorsArray中colorItems的
行开始,在常规项目中,它会生成一个
语句,在顶层不允许出现
错误,对处理该错误的最佳方法有何建议

我添加了一个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)
    }
}
导入UIKit
结构值结构{
var numValue:Int
变量颜色:字符串
变量形状:字符串
init(numValue:Int,color:String,shape:String){
self.numValue=numValue
self.color=颜色
self.shape=shape
}
}
类ViewController:UIViewController、UITableViewDelegate{
var startingNumValue=0
让颜色光线=[“黑色”、“棕色”、“红色”、“黄色”、“橙色”、“绿色”、“灰色”、“蓝色”、“紫色”、“粉色”、“白色”]
让形状排列=[“圆”、“十”、“星”、“矩形”、“三角形”、“正方形”、“心”、“冠”、“线”、“钻石”、“椭圆”、“太阳”]
var containingArray:[ValueStruct]=[]
重写func viewDidLoad(){
super.viewDidLoad()
对于colorsArray中的colorItems{
对于ShapeArray中的shapeItems{
如果启动NUM值<128{
containingArray.append(ValueStruct(numValue:startingNumValue,color:colorItems,shape:shapeItems))
startingNumValue+=1
}
}
}
println(起始数值)
}
}

我添加了一个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)
    }
}
导入UIKit
结构值结构{
var numValue:Int
变量颜色:字符串
变量形状:字符串
init(numValue:Int,color:String,shape:String){
self.numValue=numValue
self.color=颜色
self.shape=shape
}
}
类ViewController:UIViewController、UITableViewDelegate{
var startingNumValue=0
让颜色光线=[“黑色”、“棕色”、“红色”、“黄色”、“橙色”、“绿色”、“灰色”、“蓝色”、“紫色”、“粉色”、“白色”]
让形状排列=[“圆”、“十”、“星”、“矩形”、“三角形”、“正方形”、“心”、“冠”、“线”、“钻石”、“椭圆”、“太阳”]
var containingArray:[ValueStruct]=[]
重写func viewDidLoad(){
super.viewDidLoad()
对于colorsArray中的colorItems{
对于ShapeArray中的shapeItems{
如果启动NUM值<128{
containingArray.append(ValueStruct(numValue:startingNumValue,color:colorItems,shape:shapeItems))
startingNumValue+=1
}
}
}
println(起始数值)
}
}

我添加了一个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)
    }
}
导入UIKit
结构值结构{
var numValue:Int
变量颜色:字符串
变量形状:字符串
init(numValue:Int,color:String,shape:String){
self.numValue=numValue
self.color=颜色
self.shape=shape
}
}
类ViewController:UIViewController、UITableViewDelegate{
var startingNumValue=0
让颜色光线=[“黑色”、“棕色”、“红色”、“黄色”、“橙色”、“绿色”、“灰色”、“蓝色”、“紫色”、“粉色”、“白色”]
让形状排列=[“圆”、“十”、“星”、“矩形”、“三角形”、“正方形”、“心”、“冠”、“线”、“钻石”、“椭圆”、“太阳”]
var containingArray:[ValueStruct]=[]
重写func viewDidLoad(){
super.viewDidLoad()
对于colorsArray中的colorItems{
对于ShapeArray中的shapeItems{
如果启动NUM值<128{
containingArray.append(ValueStruct(numValue:startingNumValue,color:colorItems,shape:shapeItems))
startingNumValue+=1
}
}
}
println(起始数值)
}
}

我添加了一个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)
    }
}
导入UIKit
结构值结构{
var numValue:Int
变量颜色:字符串
变量形状:字符串
init(numValue:Int,color:String,shape:String){
self.numValue=numValue
self.color=颜色
self.shape=shape
}
}
类ViewController:UIViewController、UITableViewDelegate{
var startingNumValue=0
让颜色光线=[“黑色”、“棕色”、“红色”、“黄色”、“橙色”、“绿色”、“灰色”、“蓝色”、“紫色”、“粉色”、“白色”]
让形状排列=[“圆”、“十”、“星”、“矩形”、“三角形”、“正方形”、“心”、“冠”、“线”、“钻石”、“椭圆”、“太阳”]
var containingArray:[ValueStruct]=[]
重写func viewDidLoad(){
super.viewDidLoad()
对于colorsArray中的colorItems{
对于ShapeArray中的shapeItems{
如果启动NUM值<128{