realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.32k stars 2.15k forks source link

Getting compile errors when running project with Xcode 8.2.1 / Realm 2.4.1 / Cocoapods 1.2.0 #4600

Closed thomasgravina closed 7 years ago

thomasgravina commented 7 years ago

Goals

I want to be able to use Realm in a iOS Swift 3.0.2 project.

Expected Results

For the project to compile and run.

Actual Results

Getting error messages at compile time:

Genre.swift:23:1: 'required' initializer 'init()' must be provided by subclass of 'Object'

Genre.swift:23:1: 'required' initializer 'init(realm:schema:)' must be provided by subclass of 'Object'

Genre.swift:23:1: 'required' initializer 'init(value:schema:)' must be provided by subclass of 'Object'

Steps to Reproduce

Code Sample

Podfile

platform :ios, '8.0'
use_frameworks!

pod 'Fabric'
pod 'Crashlytics'
pod 'RealmSwift'

target 'MyProject' do
source 'https://github.com/CocoaPods/Specs.git'
pod 'XCGLogger', '~> 4.0.0'
end

target 'MyProject Tests' do

end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = '3.0.2'
    end
  end
end

Genre.swift

import Foundation
import RealmSwift

class Genre: Object {

    // MARK: Properties
    dynamic var id: Int
    dynamic var name: String

    init?(id: Int, name: String) {
        self.id = id
        self.name = name
        super.init()
    }

    override class func primaryKey() -> String? {
        return "id"
    }

    override class func indexedProperties() -> [String] {
        return ["name"]
    }
}

Version of Realm and Tooling

ProductName:    Mac OS X
ProductVersion: 10.12.2
BuildVersion:   16C67

/Applications/Xcode.app/Contents/Developer
Xcode 8.2.1
Build version 8C1002

/Users/thomas/.rbenv/shims/pod
1.2.0
Realm (2.4.1)
RealmSwift (2.4.1)

/bin/bash
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)

carthage not found
(not in use here)

/usr/bin/git
git version 2.10.1 (Apple Git-78)
pigeon-archive commented 7 years ago

Hi @thomasgravina! Thanks for reaching out. I'll have someone review what you've provided and respond with a solution or follow-up questions soon. 😸

austinzheng commented 7 years ago

Hi! Thanks for reaching out to us, and sorry you're seeing this error.

To fix this error, you can set initial values on your properties, and then make your initializer a convenience initializer:

class Genre: Object {
    dynamic var id: Int = 0
    dynamic var name: String = ""

    convenience init(id: Int, name: String) {
        self.init()
        self.id = id
        self.name = name
    }
    // ...
}

Hope that helps, let us know if you have any other questions! Thanks for using Realm!

pigeon-archive commented 7 years ago

Closing due to inactivity. @thomasgravina: Please follow-up if you need additional help and we can re-open the issue.