Answer:
- Function / Method is a block of statements which performs some specific task.
- Function is global whereas Method is associated with a
Class
andStructure
.
// Global Scope
// Function Example
func function() {
print("I'm a Function")
}
function()
// Method
class Car {
func method() {
print("I'm a Method beloing to `Car`")
}
}
let car: Car = Car(
car.method()
- The input arguments of a method by default are
let
constants. To change the value of input arguments the input keyword helps. - To pass the references of the arguments to a method we use inout keyword.
// Without `inout`
class Car {
func refuel(input: Double) {
input += 10 // Error, because `input` here is `Constant`
}
}
// With `inout`
class Car {
func refuel(input: inout Double) {
input += 10 // It works!
}
}
// parameter with `&`
let car: Car = Car()
let baseValue: Double = 20.0
car.refuel(input: &baseValue)
- Methods are two types: Instance methods and Type Methods.
- Instance methods are tied up with the instance of a class. So access instance methods using object.
- Type methods are tied up with classes. Use ClassName. TypeMethod.
- Use static / class to declare Type Methods
class SomeClass {
class func typeMethod() {
print("This is TypeMethod")
}
static func staticMethod() {
print("Another type of TypeMethod")
}
func method() {
print("This is method")
}
}
let someClass: SomeClass = SomeClass()
// call method
someClass.method()
// call type method
someClass.typeMethod()
someClass.staticMethod()
Answer:
- class function — can be overridden, but static func cannot be overridden
class MyClass {
// Instance Method
func instanceMethod() {}
// Class Type Method
class func classTypeMethod() {}
// Static Type Method
static func staticTypeMthod() {}
}
// Common
MyClass.classTypeMethod()
MyClass.staticTypeMethod()
// Difference
class SubMyClass: MyClass {
// Good!
override class func classTypeMethod() {}
// Compile Error
override static func staticTypeMthod() {}
}
#Tips
- if you use
class type method
withfinal
keyword, there is no difference usingstatic type method
.
Answer:
- A method which accepts dynamic number of input arguments is known as variadic params method.
class MyClass {
func variadicMethod(numbers: Int...) {
print(numbers)
}
}
let myClass: MyClass = MyClass()
myClass.variadicMethod(numbers: 1)
myClass.variadicMethod(numbers: 1, 2)
myClass.variadicMethod(numbers: 1, 2, 3)
Answer:
- func myName(firstName: String, lastName=”Shin”)
class MyClass {
func defaultValueMethod(a: Int, b: Int = 10) {
print("a: \(a)")
print("b: \(b)")
}
}
let myClass: MyClass = MyClass()
myClass.defaultValueMethod(a: 10) // a: 10, b: 10
myClass.defaultValueMethod(a: 5, b: 3) // a: 5, b: 3
Answer:
- It’s Reference Type
Section 3, Conditional Statement
Section 5, functions
Section 10, static type vs dynamic type
Section 15, higher order function