w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Swift: Key Value Observing Collections

This works for me:

class Document: NSDocument {

    var rooms = [Room]()

    func insertObject(room: Room, inRoomsAtIndex index: Int)
    {
        // Do something interesting 
        NSLog("Adding (room) to (rooms)")

        // Perform the actual insert
        rooms.insert(room, atIndex: index)
    }

    func removeObjectFromRoomsAtIndex(index: Int)
    {
        let room = rooms[index]
        NSLog("Removing room (room)")


        // Perform the actual remove
       rooms.removeAtIndex(index)
    }

    // ...
}    

I got to this by translating the expected Obj-C KV signatures to Swift for the methods I was interested in. You should be able to get the others (for unordered to-many) working the same way.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.