SCObject


init Object(collection, id?)

SCObject represents the application data object and includes methods for handling this data. The constructor creates a minimal basic "wrap" for user data.

Parameter Type Properties Description Value example
collection String Обязательное Имя коллекции в которую добавляется объект "testcoll"
id String _id объекта "huNr3L7QDh"

Example

// Создадим новый экземпляр объекта коллекции items.
let obj1 = SCObject(collection: "items", id: "huNr3L7QDh")
let obj2 = SCObject(collection: "items")

.set(dic)

Method for setting data to object

Parameter Type Properties Description Value example
dic [String: SCValue] Dictionary with data to be transferred to document ["fieldString": SCString("NewValue")]

Example

let newItem = SCObject(collection: "items")
newItem.set([
    "fieldString": SCString("Some test string"),
    "readACL": SCArray([SCString("*"), SCString("0123456789")])
    ])
newItem.save() {
    success, error, result in
    if success {
        // ... 
    } else {
        if let error = error {
            // ...
        }
    }
}

.save(callback)

The method saves the object in the data warehouse or updates an object that already exists there

Parameter Type Properties Description Value example
callback (Bool, SCError?, [String: AnyObject]?) Callback for the request being executed.

Example

let newItem = SCObject(collection: "items")
newItem.set(["fieldName": SCString("Value")])
newItem.save() {
    success, error, result in
    if success {
        // ... 
    } else {
        if let error = error {
            // ...
        }
    }
}

.getById(id, collection, callback)

Method for retrieving a collection object from DB by its _id.

Example

Parameter Type Properties Description Value example
id String Обязательный Document identifier "huNr3L7QDh"
collection String Обязательный Collection name "items"
callback (Bool, SCError?, [String: AnyObject]?) -> Void Callback for the request being executed.

Example

SCObject.getById("p3OtsLXw8p", collection: "items") {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.get(name)

Method for retrieving data from a specified object field.

Parameter Type Properties Description Value example
name String Mandatory Field name "price"

Example

let dataItem = SCObject(collection: "items", id: "huNr3L7QDh")
dataItem.get("price")

.upload(field, filename, data, callback)

File upload method

Parameter Type Properties Description Value example
field String Mandatory Field name "attachments"
filename String Mandatory File name "docname.pdf"
data NSData Mandatory File content
callback (Bool, SCError?) Callback for the request being executed.

Example

let newItem = SCObject(collection: "items")
let image = UIImage(named:"forupload")
newItem.set([
    "description": SCString("Example upload")
    ])
newItem.save() {
    success, error, result in
    if success {
        newItem.upload("attachment", "image.jpg", data: image) {
            success, error, result in
            if success {
                print("Success")
            } else {
            if let error = error {
                    print("Error")
                }
            }
        }   
    } else {
        if let error = error {
             print("Error")
        }
    }
}

.getFile(field, filename, callback)

Method for retrieving file contents

Parameter Type Properties Description Value example
field String Mandatory Field name "attachments"
filename String Mandatory File name "docname.pdf"
callback (Bool, SCError?) Callback for the request being executed.

Example

let item = SCObject(collection: "items", id: "huNr3L7QDh")
item.getFileLink("attachment")
    if success {
        print("Success")
    } else {
    if let error = error {
            print("Error")
        }
    }
}

.deleteFile(field, filename, callback)

Method for file deletion

Parameter Type Properties Description Value example
field String Mandatory Field name "attachments"
filename String Mandatory File name "docname.pdf"
callback (Bool, SCError?) Callback for the request being executed.

Example

let item = SCObject(collection: "items", id: "huNr3L7QDh")
item.delete("attachment", "swiftDocs.pdf")
    if success {
        print("Success")
    } else {
    if let error = error {
            print("Error")
        }
    }
}

.remove(callback)

Method for removing the selected document

Parameter Type Properties Description Value example
callback (Bool, SCError?) Callback for the request being executed.

Example

let item = SCObject(collection: "items", id: "huNr3L7QDh")
obj.remove() {
    success, error, result in
 if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

Exception

"Id is not specified" - String


.push(name, _ value)

Method for adding an element to an array

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "tags"
_ value SCValue Обязательный Value of the new array element 42

Example

let editItem = SCObject(collection: "items")
editItem.push("location", SCString("Sierra Army Depot"))

.pushEach(name, _ value)

Method for adding several elements to an array.

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "tags"
_ value SCValue Обязательный Values of new array elements 42, [43,43], 44

Example

let editItem = SCObject(collection: collection)
editItem.pushEach("location", SCArray([SCString("Sierra Army Depot"), SCString("Navarro")]))

.pull(name, _ value)

Method for removing all array elements whose values are the same as the specified one.

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "tags"
_ value SCPullable Обязательный Value to be removed 42

.pullAll(name, _ value)

Method for removing all array elements whose values are the same as one of the specified values.

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "tags"
_ value SCValue Обязательный Array of values to be removed [42, 44]

.addToSet(name, _ value)

Method for adding an element to an array only if there are no elements with the same name in the array.

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "tags"
_ value SCValue Обязательный Value of the new array element 42

Example

let editItem = SCObject(collection: "items")
editItem.addToSet("location", SCString("A"))

.addToSetEach(name, _ value)

Method for adding elements to an array only if there are no elements with the same name in the array.

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "tags"
_ value SCValue Обязательный Array of new array element values [42, 43]

Example

let editItem = SCObject(collection: "items")
editItem.addToSetEach("location", SCArray([SCString("Sierra Army Depot"), SCString("Navarro")]))

.pop(name, _ value)

Method for removing the first or the last array element

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "tags"
_ value Int Обязательный Position of the element to be removed in the array: -1 for the first element and 1 for the last -1

Example

let editItem = SCObject(collection: "items")
editItem.pop("location", 1)

.inc(name, _ value)

The method increments the numeric field value by a defined number

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "price"
_ value SCValue Обязательный Increment step 5

Example

let editItem = SCObject(collection: "items")
editItem.inc("amount", SCInt(-14))

.currentDate(name, typeSpec)

Sets the current time as the field's value

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "price"
typeSpec SCValue Обязательный Date type. Accepts the following values: true, "date" or "timestamp" "timestamp"

Example:

let editItem = SCObject(collection: "items")
editItem.currentDate("someDate", typeSpec: "date")

.mul(name, _ value)

The method multiplies the numeric field value by a defined number

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "price"
_ value SCValue Обязательный Multiplier 2.5

Example

let editItem = SCObject(collection: "items")
editItem.min("price", SCDouble(42.42))

.min(name, _ value)

The method updates the numeric field value only if the new value is less than the current field value

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "price"
_ value SCValue Обязательный New value 42

Example

let editItem = SCObject(collection: "items")
editItem.min("price", SCInt(42))

.max(name, _ value)

The method updates the numeric field value only if the new value is greater than the current field value

Parameter Type Properties Description Value example
name String Обязательный Name of the field whose value should be updated "price"
_ value SCValue Обязательный New value 43

Example

let editItem = SCObject(collection: "items")