我目前正在开发一个 swift cocoa osx 项目。在我的项目中,我有 31 个名为 space0、space1、space2 到 space30 的 NSimageView。每个图像视图有 3 个可能的图像,一个绿色图像、一个蓝色图像和一个白色图像。根据变量将图像分配给特定图像视图的最有效方法是什么,而无需写出所有可能的场景。例如,如果变量 x = 10 我想给 space10 分配一个图像,如果变量 y = 20 给 space20 分配一个图像。谢谢
user4064541
问问题
181 次
1 回答
0
func imgSpace(imgNumber:Int) -> NSImage? {
if imgNumber == 0 {return space00.image!}
if imgNumber == 1 {return space01.image!}
if imgNumber == 2 {return space02.image!}
if imgNumber == 3 {return space03.image!}
if imgNumber == 4 {return space04.image!}
if imgNumber == 5 {return space05.image!}
if imgNumber == 6 {return space06.image!}
if imgNumber == 7 {return space07.image!}
if imgNumber == 8 {return space08.image!}
if imgNumber == 9 {return space09.image!}
if imgNumber == 10 {return space10.image!}
if imgNumber == 11 {return space11.image!}
if imgNumber == 12 {return space12.image!}
if imgNumber == 13 {return space13.image!}
if imgNumber == 14 {return space14.image!}
if imgNumber == 15 {return space15.image!}
if imgNumber == 16 {return space16.image!}
if imgNumber == 17 {return space17.image!}
if imgNumber == 18 {return space18.image!}
if imgNumber == 19 {return space19.image!}
if imgNumber == 20 {return space20.image!}
if imgNumber == 21 {return space21.image!}
if imgNumber == 22 {return space22.image!}
if imgNumber == 23 {return space23.image!}
if imgNumber == 24 {return space24.image!}
if imgNumber == 25 {return space25.image!}
if imgNumber == 26 {return space26.image!}
if imgNumber == 27 {return space27.image!}
if imgNumber == 28 {return space28.image!}
if imgNumber == 29 {return space29.image!}
if imgNumber == 30 {return space30.image!}
return nil
}
let x = 3
if let myImage = imgSpace(x) {
yourOutleT.image = myImage
}
let y = 13
if let myImage = imgSpace(y) {
yourOutleT.image = myImage
}
let z = 27
if let myImage = imgSpace(z) {
yourOutleT.image = myImage
}
于 2014-11-05T00:56:08.483 回答