Swift 中的错误:"unrecognized selector sent to instance 0x7fa208d0a2b0"
Error in Swift: "unrecognized selector sent to instance 0x7fa208d0a2b0"
我尝试按照 CraniumCode 在 YouTube 上的教程学习 Swift 的语法。教程是创建ZigZag游戏。
当我按下“播放”按钮时,我被发送到包含 10 个随机放置的柱子和球的游戏屏幕。然后游戏停止,我在顶部看到错误,还有
Could not load the "" image referenced from a nib in the bundle with
identifier "user.App".
第一次提问,问了很久reader,还没找到可行的解决方案。
代码:
import UIKit
class ViewController: UIViewController {
@IBAction func Play(_ sender: Any) {
self.GameOver.isHidden = true
self.Play.isHidden = true
self.Retry.isHidden = true
self.Logo.isHidden = true
self.Ball.center.x = 178.0
self.Ball.center.y = 390.0
self.Pillar1.center = CGPoint (x: 175.0, y: 436.0)
self.Pillar2.center = CGPoint (x: 214.0, y: 407.0)
timer = Timer.scheduledTimer(timeInterval:0.045, target:self,
selector: Selector("movement"),
userInfo:nil, repeats:true)
self.Pillar3.center = pillarPlacement(x: Pillar2.center.x, y:
Pillar2.center.y)
self.Pillar4.center = pillarPlacement(x: Pillar3.center.x, y:
Pillar3.center.y)
self.Pillar5.center = pillarPlacement(x: Pillar4.center.x, y:
Pillar4.center.y)
self.Pillar6.center = pillarPlacement(x: Pillar5.center.x, y:
Pillar5.center.y)
self.Pillar7.center = pillarPlacement(x: Pillar6.center.x, y:
Pillar6.center.y)
self.Pillar8.center = pillarPlacement(x: Pillar7.center.x, y:
Pillar7.center.y)
self.Pillar9.center = pillarPlacement(x: Pillar8.center.x, y:
Pillar8.center.y)
self.Pillar10.center = pillarPlacement(x: Pillar9.center.x, y:
Pillar9.center.y)
self.Ball.isHidden = false
self.Pillar1.isHidden = false
self.Pillar2.isHidden = false
self.Pillar3.isHidden = false
self.Pillar4.isHidden = false
self.Pillar5.isHidden = false
self.Pillar6.isHidden = false
self.Pillar7.isHidden = false
self.Pillar8.isHidden = false
self.Pillar9.isHidden = false
self.Pillar10.isHidden = false
}
@IBAction func Retry(_ sender: Any) {
}
@IBOutlet var GameView: UIView!
@IBOutlet weak var Pillar1: UIImageView!
@IBOutlet weak var Pillar2: UIImageView!
@IBOutlet weak var Pillar3: UIImageView!
@IBOutlet weak var Pillar4: UIImageView!
@IBOutlet weak var Pillar5: UIImageView!
@IBOutlet weak var Pillar6: UIImageView!
@IBOutlet weak var Pillar7: UIImageView!
@IBOutlet weak var Pillar8: UIImageView!
@IBOutlet weak var Pillar9: UIImageView!
@IBOutlet weak var Pillar10: UIImageView!
@IBOutlet weak var Retry: UIButton!
@IBOutlet weak var Play: UIButton!
@IBOutlet weak var Logo: UIImageView!
@IBOutlet weak var GameOver: UIImageView!
@IBOutlet weak var Ball: UIImageView!
var timer = Timer()
var TapsValid:Bool?
var BallRight:Bool?
var BallChange:Bool?
override func viewDidLoad() {
super.viewDidLoad()
self.GameOver.isHidden = true
self.Play.isHidden = false
self.Retry.isHidden = true
self.Logo.isHidden = false
self.Ball.isHidden = true
self.Pillar1.isHidden = true
self.Pillar2.isHidden = true
self.Pillar3.isHidden = true
self.Pillar4.isHidden = true
self.Pillar5.isHidden = true
self.Pillar6.isHidden = true
self.Pillar7.isHidden = true
self.Pillar8.isHidden = true
self.Pillar9.isHidden = true
self.Pillar10.isHidden = true
}
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if (TapsValid == true) {
if (BallRight == true) {
BallChange = false
} else {
BallChange = true
}
}
}
func movement() {
if (BallChange == false) {
BallRight = false
} else {
BallRight = true
}
if (BallRight == true) {
Ball.center.x += 6.5
Ball.center.y -= 0.5
} else {
Ball.center.x -= 6.5
Ball.center.y -= 0.5
}
Ball.center.y += 0.5
Pillar1.center.y += 5
Pillar2.center.y += 5
Pillar3.center.y += 5
Pillar4.center.y += 5
Pillar5.center.y += 5
Pillar6.center.y += 5
Pillar7.center.y += 5
Pillar8.center.y += 5
Pillar9.center.y += 5
Pillar10.center.y += 5
Pillar1.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 1)
Pillar2.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 2)
Pillar3.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 3)
Pillar4.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 4)
Pillar5.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 5)
Pillar6.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 6)
Pillar7.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 7)
Pillar8.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 8)
Pillar9.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 9)
Pillar10.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 10)
}
func movePillarUp(floatx: CGFloat, floaty: CGFloat, pillarNumber: Int )
-> (CGPoint) {
var center = CGPoint(x: floatx, y: floaty)
if checkPillarPosition(y: floaty) == true {
switch pillarNumber{
case 1:
GameView.sendSubview(toBack:Pillar1)
center = pillarPlacement(x: self.Pillar10.center.x, y: self.Pillar10.center.y)
break
case 2:
GameView.sendSubview(toBack:Pillar2)
center = pillarPlacement(x: self.Pillar1.center.x, y: self.Pillar1.center.y)
break
case 3:
GameView.sendSubview(toBack:Pillar3)
center = pillarPlacement(x: self.Pillar2.center.x, y: self.Pillar2.center.y)
break
case 4:
GameView.sendSubview(toBack:Pillar4)
center = pillarPlacement(x: self.Pillar3.center.x, y: self.Pillar3.center.y)
break
case 5:
GameView.sendSubview(toBack:Pillar5)
center = pillarPlacement(x: self.Pillar4.center.x, y: self.Pillar4.center.y)
break
case 6:
GameView.sendSubview(toBack:Pillar6)
center = pillarPlacement(x: self.Pillar5.center.x, y: self.Pillar5.center.y)
break
case 7:
GameView.sendSubview(toBack:Pillar7)
center = pillarPlacement(x: self.Pillar6.center.x, y: self.Pillar6.center.y)
break
case 8:
GameView.sendSubview(toBack:Pillar8)
center = pillarPlacement(x: self.Pillar7.center.x, y: self.Pillar7.center.y)
break
case 9:
GameView.sendSubview(toBack:Pillar9)
center = pillarPlacement(x: self.Pillar8.center.x, y: self.Pillar8.center.y)
break
case 10:
GameView.sendSubview(toBack:Pillar10)
center = pillarPlacement(x: self.Pillar9.center.x, y: self.Pillar9.center.y)
break
default:
break
}
}
return(center)
}
func checkPillarPosition(y: CGFloat) -> (Bool) {
return (y > 720)
}
func pillarPlacement(x: CGFloat, y: CGFloat) -> (CGPoint) {
var pillarNewX: CGFloat
var pillarNewY: CGFloat
let random: Int = Int(arc4random() % 2)
if (random == 1) {
pillarNewX = x + 39
pillarNewY = y - 29
if (pillarNewX >= 319) {
pillarNewX = x - 40
pillarNewY = y - 30
}
} else {
pillarNewX = x - 40
pillarNewY = y - 30
if (pillarNewX <= 17) {
pillarNewX = x + 39
pillarNewY = y - 29
}
}
let newPillarCenter = CGPoint(x: pillarNewX, y: pillarNewY)
return(newPillarCenter)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
var timer = Timer.scheduledTimer(timeInterval:0.045, target:self,
selector: #selector(self.movement), userInfo:nil, repeats:true)
并在 func movement() 前面添加 @objc 消除了错误!
我尝试按照 CraniumCode 在 YouTube 上的教程学习 Swift 的语法。教程是创建ZigZag游戏。
当我按下“播放”按钮时,我被发送到包含 10 个随机放置的柱子和球的游戏屏幕。然后游戏停止,我在顶部看到错误,还有
Could not load the "" image referenced from a nib in the bundle with identifier "user.App".
第一次提问,问了很久reader,还没找到可行的解决方案。
代码:
import UIKit
class ViewController: UIViewController {
@IBAction func Play(_ sender: Any) {
self.GameOver.isHidden = true
self.Play.isHidden = true
self.Retry.isHidden = true
self.Logo.isHidden = true
self.Ball.center.x = 178.0
self.Ball.center.y = 390.0
self.Pillar1.center = CGPoint (x: 175.0, y: 436.0)
self.Pillar2.center = CGPoint (x: 214.0, y: 407.0)
timer = Timer.scheduledTimer(timeInterval:0.045, target:self,
selector: Selector("movement"),
userInfo:nil, repeats:true)
self.Pillar3.center = pillarPlacement(x: Pillar2.center.x, y:
Pillar2.center.y)
self.Pillar4.center = pillarPlacement(x: Pillar3.center.x, y:
Pillar3.center.y)
self.Pillar5.center = pillarPlacement(x: Pillar4.center.x, y:
Pillar4.center.y)
self.Pillar6.center = pillarPlacement(x: Pillar5.center.x, y:
Pillar5.center.y)
self.Pillar7.center = pillarPlacement(x: Pillar6.center.x, y:
Pillar6.center.y)
self.Pillar8.center = pillarPlacement(x: Pillar7.center.x, y:
Pillar7.center.y)
self.Pillar9.center = pillarPlacement(x: Pillar8.center.x, y:
Pillar8.center.y)
self.Pillar10.center = pillarPlacement(x: Pillar9.center.x, y:
Pillar9.center.y)
self.Ball.isHidden = false
self.Pillar1.isHidden = false
self.Pillar2.isHidden = false
self.Pillar3.isHidden = false
self.Pillar4.isHidden = false
self.Pillar5.isHidden = false
self.Pillar6.isHidden = false
self.Pillar7.isHidden = false
self.Pillar8.isHidden = false
self.Pillar9.isHidden = false
self.Pillar10.isHidden = false
}
@IBAction func Retry(_ sender: Any) {
}
@IBOutlet var GameView: UIView!
@IBOutlet weak var Pillar1: UIImageView!
@IBOutlet weak var Pillar2: UIImageView!
@IBOutlet weak var Pillar3: UIImageView!
@IBOutlet weak var Pillar4: UIImageView!
@IBOutlet weak var Pillar5: UIImageView!
@IBOutlet weak var Pillar6: UIImageView!
@IBOutlet weak var Pillar7: UIImageView!
@IBOutlet weak var Pillar8: UIImageView!
@IBOutlet weak var Pillar9: UIImageView!
@IBOutlet weak var Pillar10: UIImageView!
@IBOutlet weak var Retry: UIButton!
@IBOutlet weak var Play: UIButton!
@IBOutlet weak var Logo: UIImageView!
@IBOutlet weak var GameOver: UIImageView!
@IBOutlet weak var Ball: UIImageView!
var timer = Timer()
var TapsValid:Bool?
var BallRight:Bool?
var BallChange:Bool?
override func viewDidLoad() {
super.viewDidLoad()
self.GameOver.isHidden = true
self.Play.isHidden = false
self.Retry.isHidden = true
self.Logo.isHidden = false
self.Ball.isHidden = true
self.Pillar1.isHidden = true
self.Pillar2.isHidden = true
self.Pillar3.isHidden = true
self.Pillar4.isHidden = true
self.Pillar5.isHidden = true
self.Pillar6.isHidden = true
self.Pillar7.isHidden = true
self.Pillar8.isHidden = true
self.Pillar9.isHidden = true
self.Pillar10.isHidden = true
}
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if (TapsValid == true) {
if (BallRight == true) {
BallChange = false
} else {
BallChange = true
}
}
}
func movement() {
if (BallChange == false) {
BallRight = false
} else {
BallRight = true
}
if (BallRight == true) {
Ball.center.x += 6.5
Ball.center.y -= 0.5
} else {
Ball.center.x -= 6.5
Ball.center.y -= 0.5
}
Ball.center.y += 0.5
Pillar1.center.y += 5
Pillar2.center.y += 5
Pillar3.center.y += 5
Pillar4.center.y += 5
Pillar5.center.y += 5
Pillar6.center.y += 5
Pillar7.center.y += 5
Pillar8.center.y += 5
Pillar9.center.y += 5
Pillar10.center.y += 5
Pillar1.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 1)
Pillar2.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 2)
Pillar3.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 3)
Pillar4.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 4)
Pillar5.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 5)
Pillar6.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 6)
Pillar7.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 7)
Pillar8.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 8)
Pillar9.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 9)
Pillar10.center = movePillarUp(floatx: Pillar1.center.x,
floaty: Pillar1.center.y,
pillarNumber: 10)
}
func movePillarUp(floatx: CGFloat, floaty: CGFloat, pillarNumber: Int )
-> (CGPoint) {
var center = CGPoint(x: floatx, y: floaty)
if checkPillarPosition(y: floaty) == true {
switch pillarNumber{
case 1:
GameView.sendSubview(toBack:Pillar1)
center = pillarPlacement(x: self.Pillar10.center.x, y: self.Pillar10.center.y)
break
case 2:
GameView.sendSubview(toBack:Pillar2)
center = pillarPlacement(x: self.Pillar1.center.x, y: self.Pillar1.center.y)
break
case 3:
GameView.sendSubview(toBack:Pillar3)
center = pillarPlacement(x: self.Pillar2.center.x, y: self.Pillar2.center.y)
break
case 4:
GameView.sendSubview(toBack:Pillar4)
center = pillarPlacement(x: self.Pillar3.center.x, y: self.Pillar3.center.y)
break
case 5:
GameView.sendSubview(toBack:Pillar5)
center = pillarPlacement(x: self.Pillar4.center.x, y: self.Pillar4.center.y)
break
case 6:
GameView.sendSubview(toBack:Pillar6)
center = pillarPlacement(x: self.Pillar5.center.x, y: self.Pillar5.center.y)
break
case 7:
GameView.sendSubview(toBack:Pillar7)
center = pillarPlacement(x: self.Pillar6.center.x, y: self.Pillar6.center.y)
break
case 8:
GameView.sendSubview(toBack:Pillar8)
center = pillarPlacement(x: self.Pillar7.center.x, y: self.Pillar7.center.y)
break
case 9:
GameView.sendSubview(toBack:Pillar9)
center = pillarPlacement(x: self.Pillar8.center.x, y: self.Pillar8.center.y)
break
case 10:
GameView.sendSubview(toBack:Pillar10)
center = pillarPlacement(x: self.Pillar9.center.x, y: self.Pillar9.center.y)
break
default:
break
}
}
return(center)
}
func checkPillarPosition(y: CGFloat) -> (Bool) {
return (y > 720)
}
func pillarPlacement(x: CGFloat, y: CGFloat) -> (CGPoint) {
var pillarNewX: CGFloat
var pillarNewY: CGFloat
let random: Int = Int(arc4random() % 2)
if (random == 1) {
pillarNewX = x + 39
pillarNewY = y - 29
if (pillarNewX >= 319) {
pillarNewX = x - 40
pillarNewY = y - 30
}
} else {
pillarNewX = x - 40
pillarNewY = y - 30
if (pillarNewX <= 17) {
pillarNewX = x + 39
pillarNewY = y - 29
}
}
let newPillarCenter = CGPoint(x: pillarNewX, y: pillarNewY)
return(newPillarCenter)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
var timer = Timer.scheduledTimer(timeInterval:0.045, target:self,
selector: #selector(self.movement), userInfo:nil, repeats:true)
并在 func movement() 前面添加 @objc 消除了错误!