0

I want to return the first item in a given array of custom objects and return an array of indices and the first custom object element so I can wrap a guard statement around it.

ie:

let firstOrder = existingOrders.enumerated().flatMap{$0,$1}.first

or attempt #1

let array = existingOrders.enumerated().map { (a, b) in return [$0.index : $1.element] }.first

or attempt #2

let array = existingOrders.enumerated().map { ($0.offset, $0.element) }.first
print (array)

This isn't returning the actual object; and it seems to return a tuple.

where

existingOrders = [ExistingOrder, EngineYard.ExistingOrder, EngineYard.ExistingOrder]

it returns the following;

[(0, EngineYard.ExistingOrder), (1, EngineYard.ExistingOrder), (2, EngineYard.ExistingOrder)]

attempt #3;

    let array = existingOrders.enumerated().map { ($0.offset, $0.element) }
    print (array)

    guard let firstOrder = array.first else {
        break
    }

    print (firstOrder) // should be a tuple of index and custom object

How do I grab the optional first item in an array and return index and element?

Many thanks


Edit. the reason I'm doing this is so that I can transfer the correct object to another class.

    // transfer all
    while (factory.existingOrders.count > 0) {

        let array = myOrderBook.existingOrders.enumerated().map { ($0.offset, $0.element) }

        guard let firstOrder = array.first else {
            break
        }

        let index = (firstOrder.0)

        factory.orderBook.transfer(index: index, destination: .completedOrder)
    }

Where the Orderbook is a class;

Factory {
  var orderBook:OrderBook = OrderBook()
}

OrderBook {
  var existingOrders: [ExistingOrder] = [ExistingOrder]()
  var completedOrders: [CompletedOrder] = [CompletedOrder]()
}

And the idea is that I want to transfer an object from existing orders to completed orders and vice versa

The function requires an index, but I guess I could refactor it so I can transfer an object instead.

zardon
  • 1,601
  • 4
  • 23
  • 46
  • 3
    Please give an example of what you are trying to achieve. – PGDev Jun 01 '17 at 09:56
  • I think attempt #3 is giving me the result I'm after; I've got an array of custom objects I need to get the first one and its index in a tuple and the command `existingOrders.enumerated().map { ($0.offset, $0.element) }` seems to do that (at least in swift playgrounds) – zardon Jun 01 '17 at 11:17
  • If you know it is the first element, then why do you want its index? – PGDev Jun 01 '17 at 11:21
  • Its because I need to use in a function where I transfer the object from the current object to another one and I need the index to do this. Although now that you mention it, I guess I could refactor my code so I just pass the object instead. – zardon Jun 01 '17 at 15:49
  • I have two objects; one for `existingOrder` and another for `completedOrder` and the idea was that I seek the existingOrder then transfer it to my `completedOrder` array. But I realize from your comment that its more better to actually pass the object I want to transfer rather than the index. – zardon Jun 01 '17 at 15:55
  • So issue resolved now? – PGDev Jun 01 '17 at 16:40
  • 1
    yes, I am refactoring my code. I will close the question. Thanks now. – zardon Jun 01 '17 at 17:10

1 Answers1

1

The answer I was looking for was;

let array = myOrderBook.existingOrders.enumerated().map { ($0.offset, $0.element) }

However, I found that my code needed to be refactored.

Thanks.

Issue closed.

zardon
  • 1,601
  • 4
  • 23
  • 46