ios – 致命错误:无法增加endIndex

ios – 致命错误:无法增加endIndex,第1张

概述出于某种原因,我得到了 fatal error: can not increment endIndex 在线myRange.为什么我收到此错误?我不明白我的代码有什么问题. import UIKitclass ViewController: UIViewController {@IBOutlet var drag: UIPanGestureRecognizer!@IBOutlet wea 出于某种原因,我得到了

Fatal error: can not increment endindex

在线myRange.为什么我收到此错误?我不明白我的代码有什么问题.

import UIKitclass VIEwController: UIVIEwController {@IBOutlet var drag: UIPanGestureRecognizer!@IBOutlet weak var currentLabel: UILabel!@IBOutlet weak var label1: UILabel!@IBOutlet weak var label2: UILabel!@IBOutlet weak var label3: UILabel!@IBOutlet weak var label4: UILabel!@IBOutlet weak var label5: UILabel!@IBOutlet weak var label6: UILabel!@IBOutlet weak var label7: UILabel!@IBOutlet weak var label8: UILabel!@IBOutlet weak var label9: UILabel!let letter = "OX"var currentLetter = Character!()var decIDe = 0var labelArray = [UILabel]()var moveCounter = 0overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    getCurrentLetter()    labelArray = [label1,label2,label3,label4,label5,label6,label7,label8,label9]}func getCurrentLetter(){    let myRange = Range<String.Index>(start: letter.startIndex.advancedBy(decIDe),end: letter.startIndex.advancedBy(decIDe+1))    currentLabel.text = letter.substringWithRange(myRange)    currentLetter = Character(letter.substringWithRange(myRange))}func reset() {    decIDe = 0}@IBAction func dragObject(sender: UIPanGestureRecognizer) {    let points = sender.locationInVIEw(vIEw)    let currentPoints = currentLabel.frame.origin    currentLabel.center = CGPointMake(points.x,points.y)    for item in labelArray {        if CGRectContainsPoint(item.frame,points) {            item.text = String(currentLetter)            decIDe++            drag.enabled = false            currentLabel.center = currentPoints        } else if drag.enabled == false {            decIDe++            drag.enabled = true            currentLabel.center = currentPoints        }        if decIDe == 2 {          reset()        }        getCurrentLetter()      }   }}
解决方法 你的问题是决定或决定1比字符串更长.值得庆幸的是,有一些内置的代码来解决这个问题.

尝试更换

let myRange = Range<String.Index>(start: letter.startIndex.advancedBy(decIDe),end: letter.startIndex.advancedBy(decIDe+1))currentLabel.text = letter.substringWithRange(myRange)currentLetter = Character(letter.substringWithRange(myRange))

let end = letter.endindex.advancedBy(-1,limit: letter.startIndex) //The -1 is actually required,as the 'limit:' bit does <=,not just <let pos = letter.startIndex.advancedBy(decIDe,limit: end)currentLetter = letter[pos]currentLabel.text = currentLetter
总结

以上是内存溢出为你收集整理的ios – 致命错误:无法增加endIndex全部内容,希望文章能够帮你解决ios – 致命错误:无法增加endIndex所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1072916.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存