[Swift] Struct와 Class를 비교해보자.

Kang Hee Young·2022년 1월 10일
0

SwiftUI를 근본부터 다지기 위해 새로운 강의를 들으며 학습하고 있다.
강의의 내용중 기록해놓으면 도움이 될 것 같은 자료들을 정리해보자.

Struct & Class

SwiftUI에서 Struct와 Class가 공통으로 가질 수 있는 요소

- stored vars
 var isFaceUp: Bool
 
- computed vars
 var body: some View {
	 return Text("Hello World")
 }
 
- constant lets
 let defaultColor = Color.orange
 
- functions
 func multiply(_ operand: Int, by: Int)->Int {
 	return operand * by
 }
 multyply(5, by: 6)
 
- initializers
 struct RoundedRectangle {
    init(cornerRadius: CGFloat) {
    	// initialize this rectangle withd that corenerRadius
    }
    init(cornerSize: CGSize) {
    	// initiralize this rectangle with that cornerSize
    }
 }

Struct와 Class 차이점

StructClass
value typeReference type
Copied when passed or assignedPass around via pointers
Copy on writeAutomatically reference counted
Functional programmingObject-oriented programming
No inheritanceInheritance
Mutability must be explicitly statedAlways mutable

강의의 내용을 간단하게 정리해보았다. Struct와 Class 중 무엇을 사용해야 할지 모를때가 있었는데 도움이 된 것 같다.
profile
hekang in 42Seoul.

0개의 댓글