Objects, Date and Methods

Howe Wu
3 min readMay 9, 2022

紀錄在 raywenderlich 上的課程。

Swift is an object-oriented projramming language,which means that most of the code you’ll write involves objects of some kind.

Remember how I said that an app is essentially made up of objects that can send messages to each other.

So just when the UIbutton sent a showAlert message to your View Controller.

Programmers like to group related functionality into objects. Each of which has a particular job.

For example, you might have an object whose job it is to parse a file, and another object whose job it is to authenticate a user, and a final one whose job it is to perform a difficult calculation.

The Hit Me button is also an object, as is the Alert pop-up, and the text value that you put on the button Hit Me is also an object.

An object can have both data and functionality.

For example, consider the Hit Me button in Your app. First it has some data such as the color of its label, its position on the screen, its width, its height, and so on.

Second, it has some functionality, such as the ability to recognize whenever you taps on it. highlight itself when tapped, and trigger an action and response.

‘’ The thing that provides functionality to an object is commonly called a method. ’’

Other programming languages may call this a procedure or a subroutine or a function.

You will also see the term function used in Swift.

A method is simply a function that belongs to an object.

Your showAlert action is an example of a method. You can tell it’s a method because the line says func, short for function and its name is followed by a parenthesis. ( 跟一個括號的意思是 🤔?)

inside the curly braces where you add your code, that performs the functionality. This is also known as the body of the method.

Each line of codes you add to the bodies as you did one line after the other.

The most important thing to remember from this lecture is that object contain two things.

First, methods, like the steps involved in buying ice cream.

And second, data, like the actual ice cream and the money to buy it with.

Objects can look at each others data. Well, to some extent at least. After all, Steve may not approve if you peek inside his wallet. ( 影片裡的舉例說明 XD)

You can also ask other object to perform thier methods like how you ask the alert controller to add action to itself that says awesome.

Asking objects to perform their methods is how you get your app to do things.

--

--

Howe Wu
0 Followers

一介調光師正努力轉生成 iOS 工程師