create(_:value:update:)

public func create<T: Object>(_ type: T.Type, value: Any = [:], update: Bool = false) -> T

Creates or updates a Realm object with a given value, adding it to the Realm and returning it.

You may only pass true to update if the object has a primary key. If no object exists in the Realm with the same primary key value, the object is inserted. Otherwise, the existing object is updated with any changed values.

The value argument can be a Realm object, a key-value coding compliant object, an array or dictionary returned from the methods in NSJSONSerialization, or an Array containing one element for each managed property. Do not pass in a LinkingObjects instance, either by itself or as a member of a collection.

If the object is being created, all required properties that were not defined with default values must be given initial values through the value argument. Otherwise, an Objective-C exception will be thrown.

If the object is being updated, all properties defined in its schema will be set by copying from value using key-value coding. If the value argument does not respond to value(forKey:) for a given property name (or getter name, if defined), that value will remain untouched. Nullable properties on the object can be set to nil by using NSNull as the updated value, or (if you are passing in an instance of an Object subclass) setting the corresponding property on value to nil.

If the value argument is an array, all properties must be present, valid and in the same order as the properties defined in the model.

Warning

This method may only be called during a write transaction.

  • T

    Declaration

    Swift

    public func create<T: Object>(_ type: T.Type, value: Any = [:], update: Bool = false) -> T