境界を越えたインデックスのエラーが発生しています。エラー:境界を越えたインデックス
私はZLSwipeableView Swiftバージョンを扱っています。私はcardViewの色を定義する際
コード
import UIKit
class SwipeableViewController: UIViewController {
var swipeAbleView : ZLSwipeableView!
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
swipeAbleView.nextView = {
return self.nextCardView()
}
}
// This is the colors array
var colors : NSArray = NSArray(array: [UIColor.orangeColor() , UIColor.blueColor() , UIColor.greenColor() , UIColor.blackColor() , UIColor.brownColor() , UIColor.magentaColor()])
var v : UIView!
var cardsCount = 6
var cardsIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = UIColor.whiteColor()
view.clipsToBounds = true
swipeAbleView = ZLSwipeableView()
swipeAbleView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
self.swipeAbleView.alpha = 0.0
UIView.animateWithDuration (0.25, delay: 0.35, options: UIViewAnimationOptions.CurveLinear ,animations: {
self.swipeAbleView.alpha = 1.0
}, completion:nil)
self.view.addSubview(swipeAbleView)
self.swipeAbleView.discardViews()
self.swipeAbleView.loadViews()
}
func nextCardView() -> UIView {
if cardsIndex < cardsCount {
cardsIndex += 1
}
let cardView = CardView(frame: self.swipeAbleView.bounds)
// The error occurs here and the app crashes
cardView.backgroundColor = colors[cardsIndex] as? UIColor
return cardView
}
}
エラーが発生します。
アプリケーションがクラッシュするエラーが発生します。
は、実際に、これを実行しないでくださいないクランプ整数の拡張子を書くことですこのようなことをしてから、新しいIntを返すようにしてください。自分自身を変更しないでください...これはより多くの、あるいはトロールの答えでした。XD –
実際にこれをやりたいと思ったら(そして 'clamp'関数は時には便利です)範囲 '。実際には 'Range 'の拡張として 'T'の値を受け取り、'(0 .. <20).clamp(30) 'を呼び出すことができます。 –
Sulthan
実際、Metal APIにはすでにクランプ機能が組み込まれています(これははるかに簡単で直観的にできません)。https://developer.apple.com/library/ios/documentation/Metal/Reference/MetalShadingLanguageGuide/ std-lib/std-lib.htmlを参照してください。 – TheCodingArt