title: Cannot convert value of type Appdata to expected argument type Object.type Realm Swift
body: <p>How can I retrieve Realm Object data in my IOS App?</p>
  
  <p>The Object class would be:</p>
  
  <pre class="lang-js prettyprint-override"><code>import Foundation
  import RealmSwift
  
  
  class AppData: Object {
  
    dynamic var id = 0
    dynamic var tokeType = ""
    dynamic var token = ""
    dynamic var refreshToken = ""
    dynamic var accessToken = ""
  
    override static func primaryKey() -&gt; String? {
      return "id"
    }
  
  }
  </code></pre>
  
  <p>The next function I'm using to populate/update the AppData Realm Object:</p>
  
  <pre class="lang-js prettyprint-override"><code>func register(_ instanceParameters : [String : AnyObject], completionHandler: @escaping ([Instance]?, NSError?) -&gt; ()) {
  
      Alamofire.request(Router.register(instanceParameters)).responseJSON{ response in
  
        var instance = [Instance]()
  
        let AppDataObj = AppData()
        let config = Realm.Configuration(
          schemaVersion: 1,
          migrationBlock: { migration, oldSchemaVersion in
            if (oldSchemaVersion &lt; 1) {
            }
        })
        Realm.Configuration.defaultConfiguration = config
        let realm = try! Realm()
  
  
        switch response.result {
        case .success(let json):
          let responseObject = json as? [String: AnyObject]
          instance.append(Instance(dictionary: responseObject!))
  
          AppDataObj.token = (instance.first?.token)!
          AppDataObj.tokeType = "Instance_Token"
          AppDataObj.id = 1
  
          try! realm.write {
            realm.add(AppDataObj, update: true)
          }   
          completionHandler(instance, nil)
        case .failure(let error):
          completionHandler(nil, error as NSError)
        }
      }
    }
  </code></pre>
  
  <p>and for the moment everything its OK and you can see below I can manage AppData Objects, using Realm Browser:</p>
  
  <p><a href="https://i.stack.imgur.com/lVz5a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lVz5a.png" alt="enter image description here"></a></p>
  
  <p>The problem I've found is when I need to use the Realm Object Data for Persistence trying to retrieve the AppData token:</p>
  
  <pre class="lang-js prettyprint-override"><code>import RealmSwift
  
  var AppDataObj = AppData()
  
  let data = realm.object(ofType: AppDataObj, forPrimaryKey: "id")
  loginParameters["token"] = AppDataObj.token as AnyObject? --&gt; this line throws the error defined in the title of this question
  </code></pre>