added iOS source code
[wl-app.git] / iOS / Pods / RealmSwift / RealmSwift / Optional.swift
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2015 Realm Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 import Realm
20
21 /// A protocol describing types that can parameterize a `RealmOptional`.
22 public protocol RealmOptionalType {
23 }
24
25 public extension RealmOptionalType {
26     /// :nodoc:
27     public static func className() -> String {
28         return ""
29     }
30 }
31 extension Int: RealmOptionalType {}
32 extension Int8: RealmOptionalType {}
33 extension Int16: RealmOptionalType {}
34 extension Int32: RealmOptionalType {}
35 extension Int64: RealmOptionalType {}
36 extension Float: RealmOptionalType {}
37 extension Double: RealmOptionalType {}
38 extension Bool: RealmOptionalType {}
39
40 /**
41  A `RealmOptional` instance represents an optional value for types that can't be
42  directly declared as `@objc` in Swift, such as `Int`, `Float`, `Double`, and `Bool`.
43
44  To change the underlying value stored by a `RealmOptional` instance, mutate the instance's `value` property.
45  */
46 public final class RealmOptional<Value: RealmOptionalType>: RLMOptionalBase {
47     /// The value the optional represents.
48     public var value: Value? {
49         get {
50             return underlyingValue.map(dynamicBridgeCast)
51         }
52         set {
53             underlyingValue = newValue.map(dynamicBridgeCast)
54         }
55     }
56
57     /**
58      Creates a `RealmOptional` instance encapsulating the given default value.
59
60      - parameter value: The value to store in the optional, or `nil` if not specified.
61      */
62     public init(_ value: Value? = nil) {
63         super.init()
64         self.value = value
65     }
66 }