FusionPointInc FusionPointInc - 11 months ago 52
Swift Question

Keep getting "(lldb)" and Why unwrap element after using .indexOf()

I am making a program in the Swift programming language. I am very new at the language as well :).
My command-line program is supposed to switch the lowest value in the array with the first element in the array.

For example,
Input = [1, 7, 100, -4, 800, 9999]; Output = [-4, 7, 100, 1, 800, 9999]

This is the unfinished code. I get a breakpoint and (lldb) pops up. I

import Foundation

var mainArray : Array<Int> = [1, 7, 100, -4, 800, 9999]

var lowestNumber = mainArray[0]

for element in mainArray{
if element < lowestNumber {
lowestNumber = element

let lowestIndex = mainArray.indexOf(lowestNumber)
let firstNumber = mainArray[0]
let firstIndex = mainArray.indexOf(firstNumber)

I get a breakpoint when I declare lowestIndex and it prints (lldb). Also, xCode wants me to put an "!" to unwrap the value, why do I have to if I know it is an integer?


(lldb) is the command prompt when you pause the program to debug. From there, you can type commands to examine the state of your program. For example, to print the content of mainArray:

(lldd) po mainArray

mainArray.indexOf(aNumber) returns an Optional<Int> because the number may not be found in the array, in which case it returns nil.