1

I read swift handbook and was trying to do some exercises. But I run into a problem and I do not know if I do something wrong or if xCode 6 beta is just buggy.

// Playground - noun: a place where people can play

import Cocoa


let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25],
]
var largest = 0
var lastLargest = Integer[]()
var index = 0

for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            //lastLargest[index] = number

            index++

            largest = number
        }
    }
}
index
lastLargest
largest

As soon as I uncomment lastLargest[index] = number I do not get any results on right side in playground. Nor I get any infos about index, lastLargest or largest.

Following example does not work either:

var index2 = 0
var lastLargest2 = Integer[]()
lastLargest2[index2] = 1
index2++
lastLargest2[index2] = 2

2 Answers 2

2

You are appending using an out of bound array-index. Don't do that. Instead, use append:

lastLargest.append(number)

From Apple's documentation:

You can’t use subscript syntax to append a new item to the end of an array. If you try to use subscript syntax to retrieve or set a value for an index that is outside of an array’s existing bounds, you will trigger a runtime error.

Sign up to request clarification or add additional context in comments.

5 Comments

He's not appending - he's setting/replacing. That's the root issue.
I'm looking at the first code, and he is appending. lastLargest starts out empty and he assigns the first element to it using lastlargest[index] where index==0.
Right, but you can't append using subscript notation - which is highlighted in the documentation you quoted. The OP wants to append, but that's not what his code is doing, hence the exception.
I would like to accept both answers. Indeed - I want just append it but did not know how to do it in Swift. But when working with index, Craig's answer helps too.
Well - I think my main problem was that xCode did not show up with any exception. I just saw no result in playground on right side and thought that there might be something wrong.
1

When you're using explicit indexes (subscript notation) to set values in a mutable array, some value must already exist in that array at that index. When you use subscript notation, you're essentially using a 'set', rather than a 'set and add if necessary'.

As a result, you should be using:

lastLargest.insert(number, atIndex: index)

If you want to insert a new item. This will let you insert an item at the specified index, assuming your collection's size is already greater than or equal to the index you're trying to replace.

1 Comment

Better use append in this case. lastLargest.append(number)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.