Reputation: 1886
let's say the file name is MessageBubbleCell.swift
class MessageBubbleCell: UITableViewCell {
...
}
func bubbleImageMake() ->
(incoming: UIImage, incomingHighlighed: UIImage, outgoing: UIImage, outgoingHighlighed: UIImage) {...}
the context of the func is not an issue. I don't understand why some func are outside the boundaries of class {}. There are a few func within the class {}. So then whats the difference?
Upvotes: 0
Views: 75
Reputation: 115041
A method that is defined within the class opening and closing braces is an instance method or a type method -
You write an instance method within the opening and closing braces of the type it belongs to. An instance method has implicit access to all other instance methods and properties of that type. An instance method can be called only on a specific instance of the type it belongs to. It cannot be called in isolation without an existing instance.
Excerpt From: Apple Inc. “The Swift Programming Language.”
You can also define type methods - these are called on the class itself, rather than on an instance. Type methods are defined with the class
prefix (or static
in the case of a struct) and accessed against the class name itself MyClass.createDefaultInstance()
as an example.
What you have in your question is a global method - This can be called from anywhere and without providing a context. Personally I would have created it as type method as it provides functionality that is logically associated with the MessageBubbleCell
class.
Upvotes: 2