---
# System prepended metadata

title: 4/7 (四) 基本資料類別

---

# 4/7 (四) 基本資料類別



|Swift|Kotlin|
|---|---|
|struct| data class |
|class| class|
| x | abstract class |
|protocol| interface = abstrat class|
|enum| enum class = sealed class |
|optional| x |
|!| lateinit
| | !! (nonNull)|


補充：
https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html



struct <---- value type
class <---- referenece type


### 常數變數

```swift=
let pi = 3.14
var num = 123
pi = 345 // 不行 
num = 456 // 可以
```
- 變數可以分為 get & set / get only
- get only 有個名字，叫 computed variable

### 基本的 class 這樣宣告

```swift=
class KKSAVPlayer : NSObject {
    var state: KKSAVPlayerState
    var player: AVPlayer!
    var playerLayer: AVPlayerLayer!
    var masterPlaylist: ManifestMasterPlaylist?
    var duration: TimeInterval
    var currentTime: TimeInterval
}
```

KKSAVPlayer 繼承 NSObject

### protocol

* protocol 等於 Java 的 Interface
* Swift 的 protocol 可以繼承 protocol 
* extension protocol 可以有預設的實作

```swift=
protocol AppEnvProtocol {
    var castApplicationId: String { get }
    var envName: String { get }
}
struct DevelopEnv: AppEnvProtocol {
    var castApplicationId: String { return "F26E7D83" }
    var envName: String { return EnvName.develop.rawValue }
}
struct ReleaseEnv: AppEnvProtocol {
    var castApplicationId: String { return "DC01DFFB" }
    var envName: String { return EnvName.release.rawValue }
}
struct MasterEnv: AppEnvProtocol {
    var castApplicationId: String { return "365A05A2" }
    var envName: String { return EnvName.master.rawValue }
}
```

### enum

```swift=
enum APIError {
    case none
    case specifyArgumentNotFound
    case internalServerError
    case unauthorizedToPlay
    case reachTheDeviceLimit
}

let error: APIError = .reachTheDeviceLimit
```

