我正在尝试为Mac应用程序编写代码,该软件在按下按钮时会显示随机图像。我的代码如下。
import Cocoa
var tapInt = 0
class ViewController: NSViewController {
let defaults = UserDefaults.standard
@IBOutlet weak var imageView: NSImageView!
@IBOutlet weak var scoreDisplay: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.wantsLayer = true
tapInt = defaults.integer(forKey: "counterKey")
scoreDisplay.stringValue = "\(tapInt)"
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
@IBAction func buttonWasTapped(_ sender: NSButton) {
tapInt += 1
scoreDisplay.stringValue = "\(tapInt)"
defaults.setValue(tapInt, forKey: "counterKey")
let random = arc4random_uniform(85)
switch(random){
case 0: imageView.image = NSImage(named: "doggo1.jpg")
break
case 1: imageView.image = NSImage(named: "doggo2.jpg")
break
case 2: imageView.image = NSImage(named: "doggo3.jpg")
break
case 3: imageView.image = NSImage(named: "doggo4.jpg")
break
case 4: imageView.image = NSImage(named: "doggo5.jpg")
break
case 5: imageView.image = NSImage(named: "doggo6.jpg")
break
case 6: imageView.image = NSImage(named: "doggo7.jpg")
break
case 7: imageView.image = NSImage(named: "doggo8.jpg")
break
case 8: imageView.image = NSImage(named: "doggo9.jpg")
break
case 9: imageView.image = NSImage(named: "doggo10.jpg")
break
case 10: imageView.image = NSImage(named: "doggo11.jpg")
break
case 11: imageView.image = NSImage(named: "doggo12.jpg")
break
case 12: imageView.image = NSImage(named: "doggo13.jpg")
break
case 13: imageView.image = NSImage(named: "doggo14.jpg")
break
case 14: imageView.image = NSImage(named: "doggo15.jpg")
break
case 15: imageView.image = NSImage(named: "doggo16.jpg")
break
case 16: imageView.image = NSImage(named: "doggo17.jpg")
break
case 17: imageView.image = NSImage(named: "doggo18.jpg")
break
case 18: imageView.image = NSImage(named: "doggo19.jpg")
break
case 19: imageView.image = NSImage(named: "doggo20.jpg")
break
case 20: imageView.image = NSImage(named: "doggo21.jpg")
break
case 21: imageView.image = NSImage(named: "doggo22.jpg")
break
case 22: imageView.image = NSImage(named: "doggo23.jpg")
break
case 23: imageView.image = NSImage(named: "doggo24.jpg")
break
case 24: imageView.image = NSImage(named: "doggo25.jpg")
break
case 25: imageView.image = NSImage(named: "doggo26.jpg")
break
case 26: imageView.image = NSImage(named: "doggo27.jpg")
break
case 27: imageView.image = NSImage(named: "doggo28.jpg")
break
case 28: imageView.image = NSImage(named: "doggo29.jpg")
break
case 29: imageView.image = NSImage(named: "doggo30.jpg")
break
case 30: imageView.image = NSImage(named: "doggo31.jpg")
break
case 31: imageView.image = NSImage(named: "doggo32.jpg")
break
case 32: imageView.image = NSImage(named: "doggo33.jpg")
break
case 33: imageView.image = NSImage(named: "doggo34.jpg")
break
case 34: imageView.image = NSImage(named: "doggo35.jpg")
break
case 35: imageView.image = NSImage(named: "doggo36.jpg")
break
case 36: imageView.image = NSImage(named: "doggo37.jpg")
break
case 37: imageView.image = NSImage(named: "doggo38.jpg")
break
case 38: imageView.image = NSImage(named: "doggo39.jpg")
break
case 39: imageView.image = NSImage(named: "doggo40.jpg")
break
case 40: imageView.image = NSImage(named: "doggo41.jpg")
break
case 41: imageView.image = NSImage(named: "doggo42.jpg")
break
case 42: imageView.image = NSImage(named: "doggo43.jpg")
break
case 43: imageView.image = NSImage(named: "doggo44.jpg")
break
case 44: imageView.image = NSImage(named: "doggo45.jpg")
break
case 45: imageView.image = NSImage(named: "doggo46.jpg")
break
case 46: imageView.image = NSImage(named: "doggo47.jpg")
break
case 47: imageView.image = NSImage(named: "doggo48.jpg")
break
case 48: imageView.image = NSImage(named: "doggo49.jpg")
break
case 49: imageView.image = NSImage(named: "doggo50.jpg")
break
case 50: imageView.image = NSImage(named: "doggo51.jpg")
break
case 51: imageView.image = NSImage(named: "doggo52.jpg")
break
case 52: imageView.image = NSImage(named: "doggo53.jpg")
break
case 53: imageView.image = NSImage(named: "doggo54.jpg")
break
case 54: imageView.image = NSImage(named: "doggo55.jpg")
break
case 55: imageView.image = NSImage(named: "doggo56.jpg")
break
case 56: imageView.image = NSImage(named: "doggo57.jpg")
break
case 57: imageView.image = NSImage(named: "doggo58.jpg")
break
case 58: imageView.image = NSImage(named: "doggo59.jpg")
break
case 59: imageView.image = NSImage(named: "doggo60.jpg")
break
case 60: imageView.image = NSImage(named: "doggo61.jpg")
break
case 61: imageView.image = NSImage(named: "doggo62.jpg")
break
case 62: imageView.image = NSImage(named: "doggo63.jpg")
break
case 63: imageView.image = NSImage(named: "doggo64.jpg")
break
case 64: imageView.image = NSImage(named: "doggo65.jpg")
break
case 65: imageView.image = NSImage(named: "doggo66.jpg")
break
case 66: imageView.image = NSImage(named: "doggo67.jpg")
break
case 67: imageView.image = NSImage(named: "doggo68.jpg")
break
case 68: imageView.image = NSImage(named: "doggo69.jpg")
break
case 69: imageView.image = NSImage(named: "doggo70.jpg")
break
case 70: imageView.image = NSImage(named: "doggo71.jpg")
break
case 71: imageView.image = NSImage(named: "doggo72.jpg")
break
case 72: imageView.image = NSImage(named: "doggo73.jpg")
break
case 73: imageView.image = NSImage(named: "doggo74.jpg")
break
case 74: imageView.image = NSImage(named: "doggo75.jpg")
break
case 75: imageView.image = NSImage(named: "doggo76.jpg")
break
case 76: imageView.image = NSImage(named: "doggo77.jpg")
break
case 77: imageView.image = NSImage(named: "doggo78.jpg")
break
case 78: imageView.image = NSImage(named: "doggo79.jpg")
break
case 79: imageView.image = NSImage(named: "doggo80.jpg")
break
case 80: imageView.image = NSImage(named: "doggo81.jpg")
break
case 81: imageView.image = NSImage(named: "doggo82.jpg")
break
case 82: imageView.image = NSImage(named: "doggo83.jpg")
break
case 83: imageView.image = NSImage(named: "doggo84.jpg")
break
case 84: imageView.image = NSImage(named: "doggo85.jpg")
break
default:
break
}
}
}
每当我运行代码然后按按钮时,它什么都不显示。我将imageView
设置为资产中的图像,然后按下按钮,它将其从我选择的图像更改为空白图像视图。我已经用(其中的)一些图像制作了一个数组,并且具有随机函数,如下所示:
let doggoImagesArray = [
"doggo1.jpg",
"doggo2.jpg",
"doggo3.jpg",
"doggo4.jpg",
"doggo5.jpg",
"doggo6.jpg",
"doggo7.jpg",
"doggo8.jpg",
"doggo9.jpg",
"doggo10.jpg",
"doggo11.jpg",
"doggo12.jpg",
"doggo13.jpg",
"doggo14.jpg",
"doggo15.jpg",
"doggo16.jpg",
"doggo18.jpg",
"doggo19.jpg",
"doggo20.jpg",
"doggo21.jpg",
"doggo22.jpg",
"doggo23.jpg",
"doggo24.jpg",
"doggo25.jpg",
"doggo26.jpg",
"doggo27.jpg",
"doggo28.jpg",
"doggo29.jpg",
"doggo30.jpg",
"doggo31.jpg",
"doggo32.jpg",
"doggo33.jpg"
]
func randomDoggoImage() -> NSImage {
let unsignedArrayCount = UInt32(doggoImagesArray.count)
let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
let randomNumber = Int(unsignedRandomNumber)
return NSImage(named: doggoImagesArray[randomNumber])!
}
有什么想法吗?
答案 0 :(得分:0)
在您不设置图像的开关中,您仅比较图像集是否是您需要的图像。只要做:
Error using mat2cell (line 106)
Input arguments, D1 through D2, must sum to each dimension of the input matrix size, [5124 2].'
答案 1 :(得分:0)
您的图像可能没有扩展名val data = List((1,17,0,Map("TV" -> 4, "Blender" -> 2)),(2,1,1,Map("Cloths" -> 4)),(3,0,10,Map("TV" -> 4)))
val df = data.toDF("customerId","totalPurchase","totalRent","itemTypeCounts")
//Only good if you can enumerate the keys
def countMapKey(name:String) = {
count(when($"itemTypeCounts".getItem(name).isNotNull,lit(1))).as(s"itemTypeCounts_$name")
}
val keysToCount = List("TV","Blender","Cloths").map(key => countMapKey(key))
df.select(keysToCount :_*).show
+-----------------+----------------------+---------------------+
|itemTypeCounts_TV|itemTypeCounts_Blender|itemTypeCounts_Cloths|
+-----------------+----------------------+---------------------+
| 2| 1| 1|
+-----------------+----------------------+---------------------+
//More generic
val pivotData = df.select(explode(col("itemTypeCounts"))).groupBy(lit(1).as("tmp")).pivot("key").count.drop("tmp")
val renameStatement = pivotData.columns.map(name => col(name).as(s"itemTypeCounts_$name"))
pivotData.select(renameStatement :_*).show
+----------------------+---------------------+-----------------+
|itemTypeCounts_Blender|itemTypeCounts_Cloths|itemTypeCounts_TV|
+----------------------+---------------------+-----------------+
| 1| 1| 2|
+----------------------+---------------------+-----------------+
.jpg
无论如何,您的逻辑可以简化很多。您可以删除阵列并切换
NSImage(named: "doggo1")