-2

The solution help me avoid the empty array but can't let me extract values beside array[0].
I'd like to extract 2 values after appending all values to the array.
array[0] is printed out successfully but the error message appeared as "Index out of range" while I tried to print out array1.

below is the code:

    var followingList = [User]()

    func fetchfollowingList(callback: @escaping ([User]) -> Void) {

        API.User.fetchFollowingList { followingUser in

            self.followingList.append(followingUser)
            callback(self.followingList)
          print(self.followingList)
        }
          print(self.followingList[0].displayName)
          print(self.followingList[1].displayName)
    }

the "print(self.followingList)" result in console:

[APP01.User]  
[APP01.User, APP01.User]   
[APP01.User, APP01.User, APP01.User]

I inferred that the array1 is being extracted from the 1st array appended with only one value instead of the 3rd array appended with all values and not knowing how to fix it.

thanks

ting pan
  • 3
  • 2
  • 1
    Does this answer your question? [Why it print out an empty array instead of the array appended with values for the first time in viewDidLoad()?](https://stackoverflow.com/questions/63970418/why-it-print-out-an-empty-array-instead-of-the-array-appended-with-values-for-th). You have already asked this and gotten an answer to this question. – Joakim Danielson Sep 27 '20 at 07:33
  • [The answer](https://stackoverflow.com/questions/63970418/why-it-print-out-an-empty-array-instead-of-the-array-appended-with-values-for-th) help me avoid the empty array but can't let me extract the values besides the array[0]. – ting pan Sep 27 '20 at 12:25

1 Answers1

0

The function that fills the array is an async one, so when the compiler reaches the print state, it may not be filled yet. So you should check for that asynchronously too. Something like:

func fetchfollowingList(callback: @escaping ([User]) -> Void) {

    API.User.fetchFollowingList { followingUser in

        self.followingList.append(followingUser)
        callback(self.followingList)
        print(self.followingList)

        self.printIfNeeded() // <- Check everytime something appended
    }
}

func printIfNeeded() {
    guard followingList.count > 1 else { return } // Check the condition you need.

    print(self.followingList[0].displayName)
    print(self.followingList[1].displayName)
}
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • Really appreciated your answer and it does work. However, please let me explain further why it matters for me to access the array with all values appended: I need to call shuffled() method and extract 2 values from the array. Print(array[1]) is one of the ways to verify if there's multiple values in the array. " Futures & Promises" might be of help while I keep surveying this issue but still hard for me to understand. Any of your sample code to solve it is welcome. – ting pan Sep 29 '20 at 15:10
  • There are multiple solutions for that. But you may want to think a bit about it and then if you didn't find any solutions, ask another question with the **tried code** embedded. Then I can help you with that my friend. – Mojtaba Hosseini Sep 29 '20 at 15:15