iPhoneLife.com Swift 101 – Classes, Variables, Properties & Methods

iphonelife-logo

iPhoneLife.com Swift 101 – Classes, Variables, Properties & Methods

iPhoneLife.com Daily Tips once published Swift introductory tutorials. This excerpt is about Classes, Variables, Properties & Methods. I’ve seen and read about exactly the same thing for few times in the past six months, but now everything looks simple and easy:

Defining Classes in Swift

Fig02.001DeclareClass

In Swift, classes are defined in a single .swift source code file as compared to Objective-C, where classes are defined in two separate files, a header (.h) file and an implementation (.m) file.

Declaring Variables

Fig02.002DeclareVariableFull

 

In Objective-C you can declare instance variables at the class level and local variables within methods. In Swift, there are no class-level variables—only local variables that you declare within methods. However, you can declare properties at the class level (see the Declaring Properties section below.)

Declaring Simple Properties

Fig02.004PropertyDeclaration

In Objective-C, properties typically had backing instance variables in which the actual value of the property was stored. Swift simplifies properties by unifying the concepts of properties and instance variables. Properties no longer have a backing instance variable. Everything about a property is declared in a single location.

Declaring Computed Properties

Swift also has computed properties that have associated get and set methods.

Fig02.006CalculatedProperty

In this example, the Thermometer class has a simple temperatureFahrenheit property and a computedtemperateCelsius property.

Notice that the get and set methods of the Thermometer class do not reference an instance variable, because there is no such thing as an instance variable in Swift!

In this case, the get method retrieves the value from the temperatureFahrenheit property, converts it to Celsius and returns the value. The set method takes the value passed to it, converts it to Fahrenheit and then stores the result in the temperatureFahrenheit property.

The value passed to the property’s set method is stored in an implicitly named parameter called newValue. If you want to use a different parameter name, you can specify the name in parentheses after the set as shown in the following code:

Fig02.007NamedSetterParameter

Declaring Methods

Fig02.008MethodDeclaration

To declare a method, you first specify the func keyword, followed by the name of the method. If the method has parameters, you include them in the parentheses. For each parameter, you specify the name of the parameter followed by a colon, followed by its type. If the method has a return value, you then add a hyphen and greater than sign (->) followed by the type of the return value.

The following code contains an example of a method that accepts no parameters and returns nothing:

Fig02.009MethodNoParametersOrReturn

Initializer Methods

Initializer example:

 

 

Fig02.010Initializer

Multiple initializer example:

Fig02.011MultipleInitializers

In this example, both methods are named init, so to make the names unique, an external name is assigned to the parameters—fromFahrenheit and fromCelsius. This makes the full method names init(fromFahrenheit:) and init(fromCelsius:) respectively. It’s worth noting that in Swift, init methods are not inherited.

Creating an Instance of a Class

Fig02.012CreateInstance

Brian Moriguchi
Brian Moriguchi