Swift cannot convert value of type to expected argument in Realm

user2201063

I'm trying to wrap my head around the Realm API in Swift which looks extremely promising. I'm trying some demo code in their documentation and I keep getting the same error. I have a Dog.swift file with the following:

import Foundation
class Dog {
    dynamic var name = ""
    dynamic var age = 0
}

In my main ViewController.swift, I have the following to create an instance of Dog and try to save it. The issue is that the realm.add line is not compiling because it "Cannot convert value of type 'Dog' to expected argument type 'Object'"

import UIKit
import RealmSwift

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let myDog = Dog()
    myDog.name = "Rex"
    myDog.age = 10

    // Get the default Realm
    let realm = try! Realm()
    // You only need to do this once (per thread)

    // Add to the Realm inside a transaction
    realm.write {
        realm.add(myDog)
    }
  }
}

Any help would be greatly appreciated. Thanks!

user2201063

Import the RealmSwift framework in the Dog class instead of the Realm framework.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

swift cannot convert value of type 'Plugin.Type' to expected argument type 'MyClass?'

Cannot convert value of type Substring to expected argument type String - Swift 4

Swift Error: Cannot convert value of type 'ArraySlice' to expected argument type

Swift error: cannot convert value of type 'Int32' to expected argument type 'Int32'

Cannot convert value of type '()' to expected argument type '() -> ()'

Swift closure: Cannot convert value of type '(_) -> Bool' to expected argument type

Realm iOS: Cannot Convert value of type 'Dogs.Type' to expected argument type 'T.Type'

Swift 3: Cannot convert value of type 'NSMutableDictionary' to expected argument type '[AnyHashable : Any]!'

Xcode 8.1 Swift 3 Error: cannot convert value of type 'String' to expected argument type 'UnsafeMutableRawPointer'

Swift: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'

Swift generics error: Cannot convert value of type 'Type<T>' to expected argument type 'Type<_>'

Swift Generics: Cannot convert value of type to expected argument type

Swift 4: Cannot convert value of type '(_) -> ()' to expected argument type '() -> ()' or Argument passed to call that takes no arguments

Swift text to image issue. Cannot convert value of type '[String : Any]' to expected argument type '[NSAttributedStringKey : Any]?'

Swift generic func cannot convert value of type to expected argument type

Swift category: "Cannot convert value of type to expected argument type 'AnyObject!'

Swift: Cannot convert value of type 'NSDate' to expected argument type 'NSDateComponents'

SWIFT error "cannot convert a value of type "UIImageView" to expected argument type "UIImage"

Cannot convert value of type '[String]?' to expected argument type 'String' Swift

Swift 2 to Swift 3: Cannot convert value of type '(Data?, NSError?) -> Void' to to expected argument type 'GTMSessionFetcherCompletionHandler?'

Swift 3.0 cannot convert value of type (_, _)->() to Expected argument type 'ObjectsOrErrorBlock'

Cannot convert value of type [()] to expected argument type ()

firebase swift 3 conversion : Cannot convert value of type '(_) -> ()' to expected argument type '((Error) -> Void)?

Cannot convert value of type () to expected argument type bool (Swift)?

in swift assert(0) Cannot convert value of type Int to expected argument type Bool

swift cannot convert value of type (_, _) -> _ to expected argument type '((_, CGFloat)) -> _

Cannot convert value of type 'ViewController' to expected argument type 'UIButton' in Swift 4

Swift - Cannot convert value of type 'Any?' to expected argument type 'String'

Swift Cannot convert value of type '()' to expected argument type '() -> Void'