Swift global functions [closed]

0

Well, I've been reading and learning a bit about swift but I still do not understand how I can call a function, without having an object or class that has it.

An example:

print() - It's a function that I can use anywhere in the code. abs() - The same thing happens.

And there are many more that I do not see where they come from ... Also, just looking for google, I found this . A page where I explain the functions but I still do not understand where the functions come from ... if I do not have any object or class created, whoever calls that function.

Second question, do you know any course, video where explain how to properly use the Swift API?

Third and last question, the operator% co_of% that compares exactly?

    
asked by MatiEzelQ 22.03.2016 в 23:28
source

1 answer

1

First of all you must distinguish between functions and methods. The first are just pieces of code with a specific objective and / or return. The seconds are no more than functions associated with a particular type of class. Or seen in another way, a function is a piece of code and a method is that same piece inserted into a class. In addition, you must distinguish between class and instance methods. In the case of print() and abs() for example, they are functions defined in the standard library (as you have put in your link) that are globally defined so that it can be accessed from anywhere in the code.

About the operator == very simple

  • = assignment operator
  • == equality operator
  • === operator identity

To give you an idea, with = you assign a value, with == comparas values, for example a == b and with the identity operator what you compare are the memory references of those objects to know if they are the same instance.

In the case of the equality operator, values such as 3 == 5 or abc == bcd are compared but if the class implements the comparison protocol, it can be used to compare what is needed. A good explanation can be found at: link

    
answered by 23.03.2016 / 09:15
source