Swift Object Features

Swift Object Features


In “Everything is an Object,” I discuss Swift’s approach to the concept of objects. If you’re unfamiliar with Swift objects, I recommend you start by reading that first.

This article expands on the points from my previous post but focuses on the different features of objects.


Properties

A property is a variable or constant declared at the top level of an object such as a class, struct or enum.

Properties, by default, are instance properties. They have an instance-scope, meaning they’re accessed through a particular instance of the type created. For this reason, a property can have varying values for every instance created.

There’s also a static or class property. Such properties belong to the type itself rather than to instances of the type. This means you can access a static property directly from the class, struct, or enum, without creating an instance.

Static properties are great for storing shared data or settings that affect the whole type instead of individual instances.


Methods

Functions declared at the top level of objects such as classes, structures or enums, are known as methods.

Methods, by default, are instance methods and as such they define behaviours or actions that can be performed by instances of those types. Methods can manipulate instance properties, perform calculations, and return values.

It’s important to note that inside an instance method, the keyword self represents the instance itself.

Just like properties, methods can belong to the object itself and not the instance.

For such situations, the keyword static is used to declare methods that belong to structures or enums; and the keyword class is used for class methods. You call it by sending a message to the type.

When you’re referring to a static or class method from inside the type, the keyword self is used.


Initializers

An object type is simply an idea of an object. To turn the idea into an “actual” object you need to instantiate the object and to do this, you use a special method known as an initializer or constructor.

It’s the job of the initializer to set up an object in a known state when it’s created. Initializers allow you to assign initial values to an object’s properties and perform any other setup required before the object is ready for use.

In Swift, initializers are defined using the init keyword inside a class, struct, or enum and there are multiple ways to initialize objects.


Subscripts

Classes, structs, and enums can have subscripts, which are handy shortcuts for getting elements from collections, lists, or sequences. With subscripts, you can easily set and get values by index without needing separate methods.


Local Scope

Declaring an object inside of a function’s body makes it inaccessible outside the function. In other words, the object only exists within the curly braces of the function, and cannot be seen by any code outside that containing function.


Nested Type

When you declare an object inside another object, the hierarchy is known as a nested type.


Top Level

Any object type declared at the top level of a file is, by default, accessible to all files within the same module.

要查看或添加评论,请登录

Ijeoma Nelson的更多文章

社区洞察

其他会员也浏览了