致命错误:in循环swift 3的索引超出范围 [英] Fatal error: Index out of range for in loops swift 3

查看:139
本文介绍了致命错误:in循环swift 3的索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Github上使用simplePDF框架创建一个保存到pdf方法。我有几个数组,并尝试使用for循环。这是我的第一次,但我不明白为什么我会致命的错误:索引超出范围。这是我的代码。

Im trying to create a save to pdf method using the simplePDF framework on Github. I have several arrays and have tried to use a for in loop. Its my first go at this, but I do not understand why I am getting fatal error: Index out of range. Here is my code.

let A4paperSize = CGSize(width: 595, height: 842)
    let pdf = SimplePDF(pageSize: A4paperSize)
    pdf.setContentAlignment(.center)

    let count = Globals.datesArray.count
    var sum = 0

    for index in 0...count {
        pdf.addText(Globals.datesArray[index])
        pdf.addText(Globals.titleArray[index])
        pdf.addText(Globals.descriptionArray[index])
        sum += index
    }

    let pdfData = pdf.generatePDFdata()

非常感谢任何帮助。谢谢!

Any help is much appreciated. Thanks!

推荐答案

两个答案都是正确的,但两者都不理想。

Both answers are correct, but neither are ideal.

这样可行:

for index in 0..<count {

然而,使用起来要好得多:

However, it's much better to use:

for index in Globals.datesArray.indicies

这样就为你创建了范围,并删除了错字的可能性( ... 而非 ..<

This way the range is created for you, and removes the potential for a typo (... instead of ..<)

我担心的是你使用3个数组来存储相关数据。维基百科有一节关于并行数组的优缺点。他们在现代高级编程中确实没有地位。他们工作繁琐,而且非常脆弱。例如,如果在
datesArray titleArray 的中间添加元素,但忘记添加 descriptionArray 中的描述,突然之间,您的日期/标题和描述之间存在不匹配。

What concerns me is your use of 3 arrays to store related data. Wikipedia has a section on the pros and cons of parallel arrays. They really have no place in modern, high-level programming. They're cumbersome to work with, and very fragile. For example, if you add an element to the middle of datesArray and titleArray, but forget to add a description in descriptionArray, all of a sudden, you have mismatching between your dates/titles and descriptions.

您应尝试使用 class struct 。例如,您可能需要这样的结构声明:

You should try using a class or struct. For example, you might want a struct declaration like this:

struct Thing { //TODO: give me a name!
    let date: Date
    let title: String
    let description: String
}

这可以让你改变这样凌乱的并行结构:

This lets you change a messy parallel structure like this:

let datesArray = [date0, date1, date2]
let titlesArray = ["title0", "title1", "title2"]
let descriptionsArray = ["Description 0", "Description 1", "Description 2"]

就像这样:

let things = [
    Thing(
        date: date0,
        title: "title0"
        description: "Description 0"
    ),
    Thing(
        date: date1,
        title: "title1"
        description: "Description 1"
    ),
    Thing(
        date: date2,
        title: "title2"
        description: "Description 2"
    ),
]

通过第二次声明,存储了与单个 Thing 相关的所有信息ohesively。它可以让您更轻松地进行添加/编辑。没有更多的计数元素来确保它们排成一行!

With this second declaration, all the information pertaining to a single Thing is stored cohesively. It lets you much more easily make additions/edits. No more counting elements to make sure they're lined up!

有了这样的结构,你的代码就可以这样编写:

With such a struct in place, your code can be written like this:

let A4paperSize = CGSize(width: 595, height: 842)
let pdf = SimplePDF(pageSize: A4paperSize)
pdf.setContentAlignment(.center)

var sum = 0

for (index, thing) in things.enumerated() {
    pdf.addText(thing.date)
    pdf.addText(thing.title)
    pdf.addText(thing.description)
    sum += index
}

let pdfData = pdf.generatePDFdata()

该代码段使用 enumerated() ,它允许您迭代元素及其索引。这样,我们就不必在循环体中下标我们的数组了。

That snippet uses enumerated(), which lets you iterate over elements and their index. This way, we don't have to subscript our array in the loop body.

然而,总和 in这种情况总是 0,1,...,计数 。这等于(count *(count + 1))/ 2 ,因此我们可以进一步简化代码:

However, the sum in this case will always be the sum of 0, 1, ... , count. This is equal to just (count * (count + 1)) / 2, so we can simplify the code further:

let A4paperSize = CGSize(width: 595, height: 842)
let pdf = SimplePDF(pageSize: A4paperSize)
pdf.setContentAlignment(.center)

for (index, thing) in things.enumerated() {
    pdf.addText(thing.date)
    pdf.addText(thing.title)
    pdf.addText(thing.description)
}

let pdfData = pdf.generatePDFdata()

let sum = (count * (count + 1)) / 2

现在因为我们没有使用 index for 循环体,我们可以使用常规迭代,没有 enumerated()

Now since we're not using index in the for loop body anymore, we can use regular iteration, without enumerated():

let A4paperSize = CGSize(width: 595, height: 842)
let pdf = SimplePDF(pageSize: A4paperSize)
pdf.setContentAlignment(.center)

for index in things {
    pdf.addText(thing.date)
    pdf.addText(thing.title)
    pdf.addText(thing.description)
}

let pdfData = pdf.generatePDFdata()

let sum = (count * (count + 1)) / 2

这篇关于致命错误:in循环swift 3的索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆