-1

Okay so I'm trying to make a conway's game of life in swift. I am trying to create a UI that allows the user to input coordinates that start off alive. However when they are done, I am trying to get it so they click enter with nothing there and it moves. I put a "yes!" to represent where I'm trying to get the code, but I just can't get it to "yes!", when i enter it in as nothing, I get an index out of bounds error.

func splitStrPar(_ expression: String) -> [String] {
    return expression.split{$0 == " "}.map{ String($0) }
}

func getCommand() -> [String] {
    return splitStrPar(readLine()!)
}

func setup() {
    print("Enter x y coordinates of living cells, blank line when done")
    print("Coor" , terminator : ":")
    var command = getCommand()
    print(command[0])
    while(command[0] != "") {
        print("Coor" , terminator : ":")
        command = getCommand()
    }
    print("yes!")
}
Kentaro Okuda
  • 1,509
  • 2
  • 11
  • 16

3 Answers3

0

It's simple to check if the empty or not. Sample code:

// Initialize the Array

var myArray = [1,2,3]

// Check if it is empty

if myArray.isEmpty {
  print("My Array, it's empty")
}
else {
  print("No, My Array not empty")
}
Harish
  • 2,398
  • 4
  • 22
  • 47
0

you can use this condition for check empty array

if "your array name" == []{}
if "your array name".isEmpty {}

also for check empty string you can use this condition

if "your string" == "" {}
Masoud Roosta
  • 435
  • 8
  • 19
0

Simply use this code:

let fullList = [1,2,3]
let emptylist = [Int]()
if fullList.isEmpty{
    print("This will not be printed as it is false.")
}
if emptylist.isEmpty{
    print("This will be printed as it is true.")
}

The funtion array.isEmpty will return true if the array is empty. Note that the emptylist array is empty.