Calling Methods

So, starting from my previous post on 'Optionals',  I'm focusing on one area/challenge of Swift at a time, with my immediate reactions and reflections on them written in this blog. As I've mentioned before, the purpose of this is to help me make sense of what I am learning, not revel in my lack of knowledge! Also, as before, the source material I am using to learn about Swift is from the same Ebook as before: 'Learn Code in Swift 4' by Kevin McNeish. This time I will also supplement with this from other courses I have used in the past, e.g. Treehouse to help me understand the information in context.

Calling Methods

So the purpose of this chapter in the Ebook is to do with appending. So, first of all, what is a method? A method groups together one or two more lines of code that are then executed as a unit. I've always thought of these as a function put into context. E.g. 'print' is a method, or is it a function? As with any element of Swift, there is a certain syntax that needs to be used.


Here's an example:

var myString = "Swift"


myString.appending(" is the best!")

myString - object; appending - method; (" is the best!") - argument


It's called 'dot syntax' as following the dot is the method; inside the method are the open and closes parentheses - that's where the argument goes. 

print(myString)


The example I thought of - no dot used for this one though. So this is probably separate to a calling method...

OK, so going through the example with the SwiftDemo, putting a dot after an object gives you possible methods: 'Code Completion'. It guesses what you need to complete a code statement. On the list are all the possibilities basically. So I've done the .appending example on the Xcode file, and the label on the simulator has "...is the best!" added to it - nice!

Arguments and Parameters

I've got these mixed up before; what exactly is the difference between them? So, as mentioned above, an argument is the data that you pass into the method. A parameter is 'part of the method declaration that dictates the argument(s) to be passed to the method'. So arguments appear in method calls, whereas parameters appear in the method declarations. OK, a bit more sense!

Types of Method

There are two types: instance and type.
Instance methods - an object from a class is created then the instance method is called. 
Type methods - these belong to the class or type, which means you cal the method without creating an object/instance of the class. 

When typing a dot after the object (myString again in Xcode), all of the options that come up have a 'M' symbol on the left hand side; this means that they are methods - 'V' would indicate properties. The next column on the right specifies the value type e.g. String, Character etc. Finally, the last column specifies the method name e.g. range, remove etc. 

So I've figured that these would all be instance methods as they all stem from a particular object (myString). 


Another example with calling methods:

var fileName = "Robot_00001.png"

let newFileName = fileName.replacingOccurrences(of: "_0001", with: "")



So now "Robot1.png" returns in the sidebar. Again, this is all instance methods, as an object was created first. Makes sense!

For type methods, you create a instance/object when applying the method... Example:


let direction = Locale.characterDirection(forLanguage: "en")


direction.rawValue


The second part is using the .rawValue to reveal a return of '1' in the sidebar: left to right; Hebrew or Arabic would show '2'!. When I typed the .rawValue, the 'V' symbol showed that this is a property rather than a method being used. 

Ok, so that has clarified a few things for me. One of the terms I never quite new about before was an 'argument' this seems to be data passed into a method. Parameters, on the other hand, are part of the method declaration, specifying the number and type of arguments needed. In the first example up on this page, the string that was entered within the brackets is an argument, whereas what is already set in the brackets of the examples for newFileName (of/with) are parameters. 

Before I finish this, I'm going to refer to other material that I have looked at about Methods, so that I can get a deeper understanding of it. According to the official Apple Ebook, Methods are defined as: "...Functions that are associated with a particular type... methods are just functions that are associated with a type." It goes on to say about Instance Methods, which are "...Functions that belong to instances of a particular class, structure of enumeration." Finally, it mentions Type Methods as being called on the type itself (rather than on an instance of a particular type). 

Essentially, this confirms what I thought about Methods - that they are functions! The clarification from the Ebook makes it clear that they are associated with a particular type. 

There's nothing from my Treehouse course that related to Methods specifically, just functions. So that's it for now! I'm sure that Methods will be revisited soon. 


In fact, the next chapter in 'Learn to Code in Swift 4' is 'Creating Objects from Classes', so we'll see how much Calling Methods ties in. 

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)