realm / realm-swift

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

cannot find the interface declaration for RealmSwiftObject #3073

Closed hitendradeveloper closed 5 years ago

hitendradeveloper commented 8 years ago

I have just started working on watch app, I want to share my Realm model classes to watch app also,

To make data available for watch app using NSKeyedArchiver and NSKeyedUnarchiver using WatchConnectivity, I have make CocoaTouch Framework which includes model classes, and added "pod 'RealmSwift'" to framework target.

When I compile the Project, it is giving error ''

screen shot 2016-01-07 at 1 00 57 pm

I have changed SWIFT_INSTALL_OBJC_HEADE to NO

screen shot 2016-01-07 at 1 02 11 pm
bigfish24 commented 8 years ago

Is RealmSwiftObject a custom class of yours?

hitendradeveloper commented 8 years ago

No its not custom class, its class of Realm framwork

jpsim commented 8 years ago

No its not custom class, its class of Realm framwork

RealmSwiftObject is now the fully qualified Objective-C name of RealmSwift.Object as of #2979, so you shouldn't name your models by that name. I'll update the docs to reflect this.

jpsim commented 8 years ago

Actually, since the compiler should prevent you from declaring classes named RealmSwiftObject, there's not much more that documentation about it would do to help.

@hitendradeveloper could you please your entire Podfile here? This error could happen if you're using mismatched versions of the 'Realm' and 'RealmSwift' pods.

hitendradeveloper commented 8 years ago

I have used pods as below in iOS app target.

REALM SWIFT : Replacement of coreData

use_frameworks! pod 'RealmSwift'

also added same pod to watchOS2 extension and my ModelKit framework.

jpsim commented 8 years ago

@hitendradeveloper have you made any progress on this since you last posted here? If not, I could spend some time trying to reproduce this next week.

hitendradeveloper commented 8 years ago

@jpsim No, there is not any progress, still getting the same error.

danielhour commented 8 years ago

@jpsim I'm not sure if this is the same issue but I'm getting the same error message...

I have a predominantly Swift project so am using RealmSwift. However, I need to do a little Objective-C (that doesn't use Realm). However, when i #import "ProductModuleName-Swift.h" as Apple's Docs suggest for a mix-and-match project... the "cannot find the interface interface declaration for RealmSwiftObject" error prevents me from compiling.

Has your team encountered this at all? Thanks in advance for any help/advice you have!

jpsim commented 8 years ago

I have yet to prepare a sample project that reproduces the issue, so no we haven't encountered this ourselves. If you could share one, that could save some time!

danielhour commented 8 years ago

@jpsim absolutely! here you go: https://github.com/danielhour/RealmSwiftSample

thanks again for taking a look at this!

jpsim commented 8 years ago

Thanks for the project, @danielhour! I could easily trigger the compilation failure.

To be clear, using Realm Swift from Objective-C is unsupported. This is stated in the first line of our Realm Swift docs:

If you’re looking to use Realm from Objective-C, or from mixed Objective-C & Swift apps please see Realm Objective-C instead.

Realm Objective-C is designed to be used from Objective-C and/or Swift and we support its usage in mixed projects. So I encourage you to use Realm Objective-C if you need to use Realm from Objective-C.

@danielhour in your case, you're not actually trying to use Realm Swift from Objective-C, but rather you need to access some other declarations from the Swift part of your app in the Objective-C part of your app. So you're "playing within the rules" so to speak. In that case, you can hide your Realm model classes from your -Swift.h header by marking them private:

private class RObject: Object

By doing that your project compiles for me.

danielhour commented 8 years ago

@jpsim so i actually tried that and while that removes that error.. then my other swift files can no longer see that class. is there another way to hide it? thanks.

jpsim commented 8 years ago

Manually editing the -Swift.h file to avoid redeclaring your Realm models? You'd have to stop Xcode from auto-generating the file and pass in your own.

hitendradeveloper commented 8 years ago

@jpsim I am using mix and match environment,

I was just trying to create modelKit, so I can share the pure swift classes between watch and iOS app, all the classes are subClass of Object, Object class is part of Realm.

I have not used and Objective-C code, compiler automatic generates the -Swift.h header file, that's why I am getting the error.

About your solution to make private, in this case how we can share classes with other target, they will not visible anymore by making them private.

jpsim commented 8 years ago

Thanks for sharing, I'll reopen this and take another look for potential workarounds later.

hitendradeveloper commented 8 years ago

@jpsim Is there any progress about the issue ?

jpsim commented 8 years ago

I'm working on a fix in Swift's PrintAsObjC.cpp.

jpsim commented 8 years ago

I've just filed SR-805 to track this on Swift's bug tracker.

Until this is fixed, I'd encourage you to work around this by only including the -Swift.h file from an Objective-C file, re-exposing the methods you want to use, and importing that from your Objective-C++ file.

paandahl commented 8 years ago

Hacky workaround:

There is no way to exclude classes from the *-Swift.h without making them private, but what you can do is open the Swift.h file, remove all classes with errors (or just keep the class/es you need to access), copy what's left into a new file (e.g. SwiftBridge.h) in your project, and import that one instead.

This will of course need to be redone to add new classes / methods. For me that's not really an issue though, as I only need to call one Swift-class from ObjC, that doesn't change often.

The class reference looks like this:

SWIFT_CLASS("_TtC10tripfinger21TripfingerAppDelegate")

That's the module name and the class name, prefixed by the length of their names. So if they change names, you also have to go through the process again.

gereons commented 8 years ago

I'm in the same boat, with a Swift and Obj-C++ mix.

Since none of the Realm-related classes are ever used on the ObjC-side, I can work around this problem by having #define RealmSwiftObject NSObject just before the #import "xyz-Swift.h"

hitendradeveloper commented 8 years ago

Looking forward for the progress on this issue. Any updates ?

jpsim commented 8 years ago

No updates on our side. SR-805 is still up for grabs. @hitendradeveloper is this something you're interested in fixing in the Swift compiler?

vincent-peng commented 7 years ago

@gereons Thanks for the workarounds, we are currently encounter the same issue on our end and lucky saw this workaround. And btw, #define RealmObjectSwift NSObject should be -> #define RealmSwiftObject NSObject

gereons commented 7 years ago

@vincent-peng Thanks, I've edited my comment to reduce further confusion.

jkmathew commented 7 years ago

Any good news with swift 4.0? I checked @jpsim 's sample code PrintAsObjcBug with Xcode 9 beta 1. But getting same error. This will remain impossible in future too?

devolaw commented 5 years ago

I'm in the same boat, with a Swift and Obj-C++ mix.

Since none of the Realm-related classes are ever used on the ObjC-side, I can work around this problem by having #define RealmSwiftObject NSObject just before the #import "xyz-Swift.h"

This is still working for me to fix the same error on Xcode10.2 with Swift5, realm-cocoa v3.14.0

3a4oT commented 5 years ago

Cannot find interface declaration for 'RealmSwiftObject', superclass of MyObject. Mix Swift 5 and Obj-C.

innoreq commented 5 years ago

dito, Swift-only (except obj-c in imported frameworks).

DanielAsher commented 5 years ago

❗️Swift Compiler Error: Cannot find interface declaration for 'RealmSwiftObject', superclass of 'MyRealmObject'. Pure Swift 5.

3a4oT commented 5 years ago

In my case it was related to Carthage bug.

Tapestes commented 5 years ago

@3a4oT, how did you resolve the Carthage bug? Thanks.

3a4oT commented 5 years ago

depends on https://github.com/Carthage/Carthage/pull/2748 and https://github.com/Carthage/Carthage/pull/2723. Carthage should release later this week. For now I have postponed Xcode 10.2 migration.

DanielAsher commented 5 years ago

depends on Carthage/Carthage#2748 and Carthage/Carthage#2723. Carthage should release later this week. For now I have postponed Xcode 10.2 migration.

The build should work for Generic iOS Device, but fails for builds to simulators.

krystiank907 commented 5 years ago

I have the same problem: Xcode 10.2 Swift 5.0 Carthage 0.33.0 I writing only in Swift, don't have Objective-C in my project

Zrzut ekranu 2019-04-4 o 15 34 27

Update Carthage does't resolve this issues

ivitoria commented 5 years ago

I have updated to last realm-cocoa version (3.14.1) where it is supposed to be fixed, but still failing. Have anyone find a solution?

jweg29 commented 5 years ago

For me it was an issue with Carthage. Updating to Carthage 0.33.0 resolved all the issues.

allanweir commented 5 years ago

This seems to be working for me now - with Realm 3.14.1 and the pre-built binary. Carthage 0.33 but since it's pre-built maybe that doesn't matter? I also deleted the cached version in ~/Library/Caches/org.carthage.kit and I deleted the binaries from my Carthage/Build folder too

devolaw commented 5 years ago

This seems to be working for me now - with Realm 3.14.1 and the pre-built binary. Carthage 0.33 but since it's pre-built maybe that doesn't matter? I also deleted the cached version in ~/Library/Caches/org.carthage.kit and I deleted the binaries from my Carthage/Build folder too

Clear all the caches you mentioned did the trick.

MrMatten commented 4 years ago

I still see this issue Obj-C + Swift 5