{"action":"closed","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/2354","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2354/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2354/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2354/events","html_url":"https://github.com/realm/realm-cocoa/issues/2354","id":99701383,"number":2354,"title":"`indexOf` is throwing \"Object type does not match RLMResults\" exception","user":{"login":"ahayman","id":1662738,"avatar_url":"https://avatars.githubusercontent.com/u/1662738?v=3","gravatar_id":"","url":"https://api.github.com/users/ahayman","html_url":"https://github.com/ahayman","followers_url":"https://api.github.com/users/ahayman/followers","following_url":"https://api.github.com/users/ahayman/following{/other_user}","gists_url":"https://api.github.com/users/ahayman/gists{/gist_id}","starred_url":"https://api.github.com/users/ahayman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahayman/subscriptions","organizations_url":"https://api.github.com/users/ahayman/orgs","repos_url":"https://api.github.com/users/ahayman/repos","events_url":"https://api.github.com/users/ahayman/events{/privacy}","received_events_url":"https://api.github.com/users/ahayman/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":{"login":"segiddins","id":1946610,"avatar_url":"https://avatars.githubusercontent.com/u/1946610?v=3","gravatar_id":"","url":"https://api.github.com/users/segiddins","html_url":"https://github.com/segiddins","followers_url":"https://api.github.com/users/segiddins/followers","following_url":"https://api.github.com/users/segiddins/following{/other_user}","gists_url":"https://api.github.com/users/segiddins/gists{/gist_id}","starred_url":"https://api.github.com/users/segiddins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/segiddins/subscriptions","organizations_url":"https://api.github.com/users/segiddins/orgs","repos_url":"https://api.github.com/users/segiddins/repos","events_url":"https://api.github.com/users/segiddins/events{/privacy}","received_events_url":"https://api.github.com/users/segiddins/received_events","type":"User","site_admin":false},"milestone":null,"comments":4,"created_at":"2015-08-07T18:15:53Z","updated_at":"2015-08-07T20:55:08Z","closed_at":"2015-08-07T20:55:08Z","body":"I've written a persistence layer around RealmSwift.  Everything works fine except I can't seem to use `indexOf`.  When I do, it throws: `Object type does not match RLMResults`.  However, even when I look in the debugger, it shows I'm passing in the correct object type to `indexOf`.  Further details:\r\n\r\nIn order to \"wrap\" Realm, I had to create a translation layer to translate between the Realm objects and the \"Model\" objects I use in the rest of the app.\r\n\r\nExample: \r\n\r\n    public protocol Translator {\r\n      func translate(from: Object) -> Model\r\n      func translate(from: Model) -> Object\r\n    }\r\n\r\nI created a wrapper around `Results`, I called `Dataset`.  The `Dataset` uses a `Translator` to translate between the objects returned by `Results` and the `Model` objects the rest of the app uses.\r\n\r\nBecause of the limitations of Swift, I could not directly wrap `Results<T>` (Swift generic types are invariant).  So instead, I _extended_ `Results` with a protocol to provide the functions I needed.  \r\n\r\n    protocol RealmResults {\r\n      func objectAt<T: Object>(index: Int) -> T\r\n      var objectCount: Int { get }\r\n      func indexOfObject<T: Object>(object: T) -> Int?\r\n      func filterOn(predicate: NSPredicate) -> RealmResults\r\n      func sortOn(sorts: [SortDescriptor]) -> RealmResults\r\n    }\r\n\r\n    extension Results : RealmResults {\r\n      func objectAt<T : Object>(index: Int) -> T {\r\n        return self[index] as! T\r\n      }\r\n      \r\n      var objectCount: Int {\r\n        return count\r\n      }\r\n      \r\n      func indexOfObject<U : Object>(object: U) -> Int? {\r\n        if let realmObject = object as? T {\r\n          return indexOf(realmObject)\r\n        }\r\n        return nil\r\n      }\r\n      \r\n      func filterOn(predicate: NSPredicate) -> RealmResults {\r\n        return filter(predicate)\r\n      }\r\n      \r\n      func sortOn(sorts: [SortDescriptor]) -> RealmResults {\r\n        return sorted(sorts)\r\n      }\r\n    }\r\n\r\nSo `Dataset` wraps `RealmResults` and accesses `Results<T>` using the protocol.  This all works fine, except for `indexOfObject`.  It throws `Object type does not match RLMResults` even though I'm passing in the exact object that `Results` is expecting.  \r\n![screen shot 2015-08-07 at 1 08 34 pm](https://cloud.githubusercontent.com/assets/1662738/9142504/ae35b1fa-3d05-11e5-83db-bfc20225343a.png)\r\n\r\nYou can see from the debugger that Results is: `Results<MessageObject>` and the type I'm passing in is `MessageObject`.  Any idea why this may be happening?  Is it an issue or am I missing something?"}}