반응형
Comparing structure and class in swift
In common, both can
-
Define properties to store values
-
Define subscripts to provide access to their values using subscript syntax
-
Define initializers to set up their initial state
-
Be extended to expand their functionality beyond a default implementation
-
Conform to protocols to provide standard functionality of a certain kind
Only Class
-
inherit another classes characteristics.
-
Define deinitializers to free up assigned resources
Main difference
1 . Class is reference type otherwise structure is value type
- what is reference type?
from swift official document
Unlike value types, reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used.
- what is value type?
from swift official document
A value type is a type whose value is copied when it’s assigned to a variable or constant, or when it’s passed to a function.
2. Only structure can use codable protocol
- what is codable?
Codable is a type alias for the Encodable and Decodable protocols. When you use Codable as a type or a generic constraint, it matches any type that conforms to both protocols.
Happy Coding :)
반응형
그리드형
'개발 > swift' 카테고리의 다른 글
Alamofire 5 Network Interceptor (0) | 2020.10.12 |
---|---|
Multiple link UILabel in swift (0) | 2020.10.11 |
Swift navigation controller freeze on swipe gesture (0) | 2020.10.07 |
Swift IBInspectable and IBDesignable (0) | 2020.10.06 |
Swift validation (0) | 2020.10.05 |