1

I have an data model object with several properties. I have a dynamic array that will sometimes have some but not all of the object properties inside of it.

How do I safely check if the array as anything at it's index

DataModel

class Order{
     var item0:String?
     var item1:String?
     var item2:String?
}

Array:

var myArray = [String]()

The guard statements are where I'm having issues checking to see if there are elements inside the array at different indexes.

Btw the array will never have more then 3 elements inside of it.

let order = Order()
order.item0 = "hat"
order.item1 = "sneakers"

myArray.append(order.item0)
myArray.append(order.item1)
//sometimes there may or may not be item2

let placedOrder = Order

//These guard statements aren't working
guard case let placedOrder.item0 = myArray[0] else {return}

guard case let placedOrder.item1 = myArray[1] else {return}

//There isn't anything at myArray[2] but I need to safely check anyway
guard case let placedOrder.item2 = myArray[2] else {return}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • 1
    just use an `if myArray.count < 3` or whatever – Paulw11 Oct 30 '16 at 19:02
  • 1
    I wouldn't use an array for storing properties. I would create a **struct** with different properties. – mfaani Oct 30 '16 at 23:34
  • @Paulw11 I think that won't be a good mechanism. Because you may want to know which parameter is nil... – mfaani Oct 30 '16 at 23:43
  • I also thought of a dictionary, but it isn't really clear what the OP is trying to achieve; it seems that they just want to know how many items are in the array, but the use of 3 separate strings is a bad design anyway. – Paulw11 Oct 30 '16 at 23:46
  • @Paulw11 why is it bad design? I can explain what I'm doing in more detail but I was trying to keep things simple – Lance Samaria Oct 31 '16 at 01:37
  • The reason I say it is bad design is because using 3 separate variables is more complicated than using an array; you need to write repeated lines of code to access them rather than using a loop and if you needed to expand from 3 items then it just gets worse. Since your properties are optional, you can just check for `nil` anyway rather than having an associated array. – Paulw11 Oct 31 '16 at 01:40
  • @honey your exactly right, I also need to test for nil – Lance Samaria Oct 31 '16 at 01:47
  • Still seems to be more complicated than it needs to be, but anyway, can't you just check `myArray.count`? – Paulw11 Oct 31 '16 at 02:34
  • I can check and loop through the array but where I'm lost at is attaching whatever elements are inside the array to the datamodel property that would correspond ti that element – Lance Samaria Oct 31 '16 at 03:32

1 Answers1

0

1st The array should hold the data model type and not it's properties:

var orders = [Order]() 
//changed the array's name from myArray to orders instead

2nd Add the data model object to the array:

let orderOne = Order()

orders.append(orderOne)

3rd loop through the array, check that the element's properties aren't nil:

for order in orders{

    if order.item0 != nil{
       //do something with order.item0
    }

    if order.item1 != nil{
       //do something with order.item1
    }

    if order.item2 != nil{
       //do something with order.item2
    }
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256