iOS Swift Development: Methods

iOS Swift Development: Methods

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

Modifying Value Types from Within Instance Methods?

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (9)

Section 1

(9 cards)

Modifying Value Types from Within Instance Methods?

Front

Use the mutate keyword struct Point { var x = 0.0, y = 0.0 mutating func moveBy(x deltaX: Double, y deltaY: Double) { x += deltaX y += deltaY } } var somePoint = Point(x: 1.0, y: 1.0) somePoint.moveBy(x: 2.0, y: 3.0) print("The point is now at (\(somePoint.x), \(somePoint.y))") // Prints "The point is now at (3.0, 4.0)" By using the mutate keyword, the variables x and y within the struct are permanently changed to 3 and 4 instead of kept at x=0.0, y=0.0. This is done by the function and using the keyword mutate in front of the word func.

Back

What is Self?

Front

Every instance of a type has an implicit property called self. which is exactly equivalent to the instance itself. You use the self property to refer to the current instance within its own instance methods. func increment() { self.count += 1 } calls the variable within the class instead of accidentally calling a variable called count outside the method. struct Point { var x = 0.0, y = 0.0 func isToTheRightOf(x: Double) -> Bool { return self.x > x } } let somePoint = Point(x: 4.0, y: 5.0) if somePoint.isToTheRightOf(x: 1.0) { print("This point is to the right of the line where x == 1.0") } self.x refers to the variable x inside the isToTheRight function within the struct, and not the variable x outside the function but within the struct.

Back

Give the example of mutating the following enumeration called TriStateSwitch

Front

enum TriStateSwitch { case off, low, high mutating func next() { switch self { case .off: self = .low case .low: self = .high case .high: self = .off } } } var ovenLight = TriStateSwitch.low ovenLight.next() // ovenLight is now equal to .high ovenLight.next() // ovenLight is now equal to .off This example defines an enumeration for a three-state switch. The switch cycles between three different power states (off, low and high) every time its next() method is called.

Back

What are type methods?

Front

Instance methods, as described above, are methods that are called on an instance of a particular type. You can also define methods that are called on the type itself. These kinds of methods are called type methods. You indicate type methods by writing the static keyword before the method's func keyword. Classes may also use the class keyword to allow subclasses to override the superclass's implementation of that method. class SomeClass { class func someTypeMethod() { // type method implementation goes here } } SomeClass.someTypeMethod()

Back

What variables are an exception to mutation within a method?

Front

A constant variable. Any variable that starts with the keyword "let"

Back

What are methods?

Front

Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. Classes, structures, and enumerations can also define type methods, which are associated with the type itself. Essentially a function within structs, classes, and enumerations, etc

Back

Give the example of an Instant Method?

Front

class Counter { var count = 0 func increment() { count += 1 } func increment(by amount: Int) { count += amount } func reset() { count = 0 } }

Back

What are Instance Method?

Front

Instance methods are functions that belong to instances of a particular class, structure, or enumeration. They support the functionality of those instances, either by providing ways to access and modify instance properties, or by providing functionality related to the instance's purpose. Instance methods have exactly the same syntax as functions, as described in Functions

Back

How do you call the individual instant methods of the class?

Front

let counter = Counter() Counter.increment() Counter.increment(by: Int) Counter.reset()

Back