Introduction
: Objective-C 와는 달리 Swift는 헤더파일과 구현파일을 나누지 않는다. 구현파일에다가 코드를 작성하면 Swift는 각 메서드, 멤버변수의 접근자에 따라 자동으로 헤더파일을 만들어 준다.

그리고 Swift의 Class와 Structure는 다른 언어에 비해 object로서의 역할 보다 functionality의 역할에 더 치우쳐져 있다.


Comparing Classed and Structures
1) 공통
 - property를 정의할 수 있다.
 - method를 정의할 수 있다.
 - subscript를 정의할 수 있다.
 - initializer를 구현할 수 있다.
 - extension을 구현할 수 있다.
 - protocol을 confirm할 수 있다.

2) 차이점
 - Class만 상속이 가능하다.
 - Type Casting이 가능하다.
 - Deinitializer를 구현할 수 있다.
 - Reference Counting이 가능하다. (Structure는 어떤 방식으로든 다른곳에 전달될 때, reference count를 증가시키는 것이 아니라 복사된다.)

Definition Syntax 

struct Resolution {
    var width = 0
    var height = 0
}

class VideoMode {
    var resolution = Resolution()
    var interlaced = false
    var frameRate = 0.0
    var name: String?
}

Class and Structure Instances
: class와 structure의 instance를 생성하는 문법이다.

let someResolution = Resolution()
let someVideoMode = VideoMode()

Accessing Properties
: property에 access할 때는 dot syntax를 사용한다.

print("The width of someResolution is \(someResolution.width)")
//Prints "The width of someResolution is 0"

print("The width of someVideoMode is \(someVideoMode.resolution.width)")
// Prints "The width of someVideoMode is 0"

someVideoMode.resolution.width = 1280
print("The width of someVideoMode is now \(someVideoMode.resolution.width)")
// Prints "The width of someVideoMode is now 1280"


Structures and Enumerations Are Value Types
: value type이 의미하는 것은 variable이나 constant에 할당되거나 다른 function으로 전달될 때 값이 복사되어 진다는 것이다.

모든 Structure, Enumeration은 value typed이다.

integers, floating-point numbers, Booleans, strings, arrays, dictionaries 모두 structure, 즉, value type이다.

let hd = Resolution(width: 1920, height: 1080)
var cinema = hd

cinema.width = 2048

print("cinema is now \(cinema.width) pixels wide")
// Prints "cinema is now 2048 pixels wide"

print("hd is still \(hd.width) pixels wide")
// Prints "hd is still 1920 pixels wide"

위의 예처럼 값이 복사되는 것을 알수 있다. 이는 Enumeration 에도 똑같이 적용된다.

enum CompassPoint {
    case north, south, east, west
}

var currentDirection = CompassPoint.west
let rememberDirection = currentDirection

currentDirection = .east

if rememberedDirection == .west {
    print("The remembered direction is still .west")
}
// Prints "The remembered direction is still .west"


Classes Are Reference Types
: reference type은 value type과 달리 copy 되는것이 아니라 같은 instance를 참조한다.

let tenEighty = VideoMode()

tenEighty.resolution = hd
tenEighty.interlaced = true
tenEighty.name = "1080i"
tenEighty.frameRate = 25.0

let alsoTenEighty = tenEighty

alsoTenEighty.frameRate = 30.0

print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
// Prints "The frameRate property of tenEighty is now 30.0"

Identity Operators
- Identical to(===), Not Identical to(!==)
- 두 contants 또는 variables가 같은 Class의 instance를 참조하고 있는지 판별한다.

* "=="는 두 instance가 의미상으로 같은지 체크한다. 이것은 클래스 디자인에 따라 equal 되는 조건이 틀려진다.


Choose Between Classes and Structures
: Class와 Structure 중에 어떤것을 써야할지 고민될 때는 애플의 가이드 라인을 참고하면 된다.

Structure를 사용하기 적합한 경우
 - Structure의 주요 목적이 간단한 데이터들을 encapsulation 하는 것일때.
 - instance가 다른곳으로 전달될 때 encapsulated value들이 reference 되는것 보다 copy 되는것이 합리적일때
 - 모든 property가 value type이고 reference 되는것 보다 copy 되기를 원할 때
 - 상속을 사용하지 않을 경우.


Assignment and Copy Behavior for Strings, Arrays. and Dictionaries
: value type은 다른곳으로 전달될 때 무조건 복사되는 것이 아니라, 필요할 때만 복사된다. 예를들어 value type이 다른곳으로 전달된 이후에 상태가 하나도 변경되지 않았다면 굳이 복사해서 메모리에 2개를 가지고 있을 필요가 없다. 이러한 경우 복사를 하지 않고 reference 하고 있다가 상태가 변경될때 복사해서 상태를 변경한다.






'IOS > Swift' 카테고리의 다른 글

[Swift 4.0] Methods  (0) 2017.04.01
[Swift 4.0] Properties  (0) 2017.04.01
[Swift 4.0] Enumerations  (0) 2017.04.01
[Swift 4.0] Closures  (0) 2017.04.01
[Swift 4.0] Functions  (0) 2017.04.01
Posted by 홍성곤
,