我在尝试弄清楚为什么会出现线程1致命错误:我的应用程序索引超出范围时遇到问题。我的应用程序显示8张图片,以开始图片幻灯片播放。由于某种原因,我收到此错误,无法解决。有没有办法消除此错误?谁能帮我吗?
var images = [UIImage]()
var counter = 2
var time = Timer()
@IBOutlet weak var menuButton: UIBarButtonItem!
@IBOutlet weak var ImageView: UIImageView!
@IBOutlet weak var Slider1: UISlider!
@IBAction func Slider(_ sender: UISlider) {
_ = 0
let value = Int(sender.value)
ImageView.image = images[value]
}
@IBAction func NextButton(_ sender: Any) {
Slider1.value += 1
ImageView.image = images[Int(Slider1.value)]
self.ImageView.animationImages = self.images
self.ImageView.animationDuration = 15.0
self.ImageView.animationRepeatCount = 0
self.ImageView.startAnimating()
UIView.transition(with: self.ImageView, duration: 5.0, options: .transitionCrossDissolve, animations: {self.ImageView.image = self.ImageView.image}, completion: nil)
}
@IBAction func PrevButton(_ sender: Any) {
Slider1.value -= 1
ImageView.image = images[Int(Slider1.value)]
self.ImageView.animationImages = self.images
self.ImageView.animationDuration = 15.0
self.ImageView.animationRepeatCount = 0
self.ImageView.startAnimating()
UIView.transition(with: self.ImageView, duration: 5.0, options: .transitionCrossDissolve, animations: {self.ImageView.image = self.ImageView.image}, completion: nil)
}
//Set Status Bar to light content (white)
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
//Set Navigation Bar color Example Home, Back button
self.navigationItem.backBarButtonItem?.tintColor = UIColor.white;
time = Timer.scheduledTimer(withTimeInterval: 8, repeats: true) { _ in
self.NextButton(self);
}
super.viewDidLoad()
setup()
images = [#imageLiteral(resourceName: "MainImage1.jpg"), #imageLiteral(resourceName: "MainImage2.jpg"), #imageLiteral(resourceName: "MainPage3.jpg"), #imageLiteral(resourceName: "MainImage4.jpg"), #imageLiteral(resourceName: "MainImage5.jpg"), #imageLiteral(resourceName: "MainImage6.jpg"), #imageLiteral(resourceName: "MainImage7.jpg"), #imageLiteral(resourceName: "MainImage8.jpg")]
sideMenus()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setup(){
self.navigationController?.navigationBar.tintColor = UIColor.white
}
override var prefersStatusBarHidden: Bool{
return false
}
var navigationBarAppearace = UINavigationBar.appearance()
override func viewDidAppear(_ animated: Bool){
}
func sideMenus() {
if revealViewController() != nil {
menuButton.target = revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
revealViewController().rearViewRevealWidth = 275
revealViewController().rightViewRevealWidth = 160
view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
}
答案 0 :(得分:0)
该错误告诉您正在访问images
数组范围之外的元素。在viewDidLoad
中,将images
数组设置为8个元素。这些元素的范围是0-7。如果您尝试访问该范围之外的元素,则会看到错误信息。
看看您的代码,问题是在NextButton
和PrevButton
中,您可以调整滑块值和索引,但是并没有检查以确保未超出数组界限。如果您在第一个图像上并单击“上一个”按钮,则超出范围。如果您在最后一张图像上,然后单击“下一步”按钮,则超出范围。
您需要做的是确保数组中没有负索引或索引大于或等于数组中的项目数。从用户界面的角度来看,最好的解决方案是在第一张图像上时禁用Prev按钮,在最后一张图像上时禁用Next按钮。
自从您询问了一个示例以来,下面的代码就在增加滑块值后放入nextButton
:
if slider1.value >= images.count {
return
}
减小滑块值后的prevButton
代码:
if slider1.value < 0 {
return
}
现在,如果数组索引超出范围,该函数将退出。
要检查的最后一件事是images
中正在填充viewDidLoad
数组。在viewDidLoad
中设置一个断点并逐步执行代码。如果加载MainImage.jpg文件时遇到问题,则该数组将为空,并且在访问该数组时会出现超出范围的错误。