Assuming you want to create an Order object with 1:N relation to the OrderItem objects here are the correct steps:
@objcMembers
class OrderItem: NSObject {
var objectId: String?
var name: String?
var quantity = 0
var price = 0.0
}
@objcMembers
class Order: NSObject {
var objectId: String?
var orderNumber = 0
var orderName: String?
var orderItems: [OrderItem]?
}
func testOrder() {
Types.tryblock({ () -> Void in
// create the orderItems DataStore
let orderItems = Backendless.sharedInstance().data.of(OrderItem.ofClass())
// create and save first orderItem
let orderItem1 = OrderItem()
orderItem1.name = "Printer"
orderItem1.quantity = 1
orderItem1.price = 99.0
let savedOrderItem1 = orderItems?.save(orderItem1) as! OrderItem
// create and save second orderItem
let orderItem2 = OrderItem()
orderItem2.name = "Paper"
orderItem2.quantity = 10
orderItem2.price = 19.0
let savedOrderItem2 = orderItems?.save(orderItem2) as! OrderItem
// create the orders DataStore
let orders = Backendless.sharedInstance().data.of(Order.ofClass())
// create and save order
let order = Order()
order.orderNumber = 1
order.orderName = "Office Supplies"
let savedOrder = orders?.save(order) as! Order
// add orderItems to the order
let relationsSet = orders?.setRelation("orderItems:OrderItem:n", parentObjectId: savedOrder.objectId, childObjects: [savedOrderItem1.objectId!, savedOrderItem2.objectId!])
print("Relations have been set")
}, catchblock: { (exception) -> Void in
print("Server reported an error: \(exception as? Fault)")
})
}