SLN SLN - 4 months ago 6
Swift Question

What is API? I have an example code with 2 lines here, let's use this code to discuss

I've searched a while of the meaning of the API. Yes, I kind of know it from the general overview, it is the Application Programming Interface. It is an Interface for the Application and only used by programmers, users don't need it. App can change, but API keeps, .......... I can describe it by so many metaphors. But what is it? I still don't know.

There is the last part of the section Collection Types from the official document

If you need to use a dictionary’s keys or values with an API that takes an Array instance, initialize a new array with the keys or values property:

let airportCodes = [String](airports.keys)
// airportCodes is ["YYZ", "LHR"]

let airportNames = [String](airports.values)
// airportNames is ["Toronto Pearson", "London Heathrow"]

Could someone explain the API to me by this example? What is the API here? How it works?

Thanks a lot


API is something which can be used without you writing a code for the same thing over and over again. let airportNames = [String](airports.values) says you can append this array with an array of airportNames which is a string. If you wanted to do this by yourself, you had to first initialise the array, then add it. The .values will have it's own function and implementation behind this. It eases the way you write your code.