1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2015 Realm Inc.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ////////////////////////////////////////////////////////////////////////////
21 /// A protocol describing types that can parameterize a `RealmOptional`.
22 public protocol RealmOptionalType {
25 public extension RealmOptionalType {
27 public static func className() -> String {
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 {}
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`.
44 To change the underlying value stored by a `RealmOptional` instance, mutate the instance's `value` property.
46 public final class RealmOptional<Value: RealmOptionalType>: RLMOptionalBase {
47 /// The value the optional represents.
48 public var value: Value? {
50 return underlyingValue.map(dynamicBridgeCast)
53 underlyingValue = newValue.map(dynamicBridgeCast)
58 Creates a `RealmOptional` instance encapsulating the given default value.
60 - parameter value: The value to store in the optional, or `nil` if not specified.
62 public init(_ value: Value? = nil) {