Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RealmSwift] Errors in Swift->ObjC Generated Header for Realm Objects with List<> Properties #1925

Closed
leegoodrich opened this Issue May 12, 2015 · 12 comments

Comments

Projects
None yet
4 participants
@leegoodrich
Copy link

leegoodrich commented May 12, 2015

RealmSwift v0.92.2
XCode 6.3.1

In a project with mixed Swift / Objective-C code, target fails to compile due to missing type information for List<> properties in the generated Swift->ObjC header. Here is a snippet of the generated ObjC interface for one of my affected classes:

@interface RealmPlaylist : Object
@property (nonatomic, copy) NSString * __nonnull name;
@property (nonatomic) NSInteger id;
@property (nonatomic, readonly) /* List<RealmTrack> */ tracks;
@end

The compiler complains that tracks "requires a specifier or qualifier", which definitely seems to be the case.

The docs say to use the Realm-ObjectiveC framework for projects with mixed codebases, so I'll migrate over to that one and have to unfortunately give up all the nice Swift-ness. I initially assumed I could get away using RealmSwift as my Objective-C code does not use any of the Realm objects, but they do use other Swift objects defined in the generated header.

Generally, XCode simply forgoes generating interfaces exposing Swift features that are incapable of providing an Objective-C equivalent, such as generics and Swift-style enums. Is that not possible here?

@segiddins

This comment has been minimized.

Copy link
Contributor

segiddins commented May 12, 2015

Thanks @leegoodrich -- we've seen a couple of reports of this earlier, and it appears to be a swift bug. We recommend using Realm Objective-C if you will need to use Realm from both Swift and Objective-C in the same project.

@jpsim

This comment has been minimized.

Copy link
Contributor

jpsim commented May 12, 2015

What does your RealmPlaylist class definition look like in Swift? I think this happens when attempting to expose List properties to Objective-C as dynamic, but then Objective-C can't represent the generic.

So what happens if you define your property as public let tracks = List<RealmTrack>() rather than public dynamic var tracks = List<RealmTrack>()? (assuming that's what you're doing)

@leegoodrich

This comment has been minimized.

Copy link
Author

leegoodrich commented May 12, 2015

I have it defined like so:

class RealmPlaylist: Object {
    dynamic var name : String = ""
    dynamic var id : Int = -1

    let tracks = List<RealmTrack>()

Adding the 'public' keyword to tracks makes no difference in the generated header

@jpsim

This comment has been minimized.

Copy link
Contributor

jpsim commented May 12, 2015

I was speaking to @nkirby earlier, who had hit the same issue, but managed to work around it by making his List property internal, and exposing the equivalent of public func tracksInPlaylist(playlist: RealmPlaylist) -> Results<RealmTrack> {…}. @nkirby can you comment on that?

@leegoodrich

This comment has been minimized.

Copy link
Author

leegoodrich commented May 12, 2015

So, I noticed that my Results<> properties are not being included in the generated header, and whipped up this quick hack to test as a workaround:

public class RealmPlaylist: Object {
    dynamic var name : String = ""
    dynamic var id : Int = -1

    private let _tracks : List<RealmTrack> = List<RealmTrack>()
    public var tracks : Results<RealmTrack> {
        get { return _tracks.filter(NSPredicate(value: true)) }
    }

EDIT: Updated code to use correct predicate for passing all values

Any reason you think this plus adding some custom setters to wrap append() and extend() wouldn't work? Probably more of a PITA than just switching over to the Objective-C framework, but might work for limited uses while waiting for a fix.

@segiddins

This comment has been minimized.

Copy link
Contributor

segiddins commented May 12, 2015

This is probably due to Results being a native swift class, whereas List inherits from RLMListBase, which is an NSObject subclass declared in Objective-C.

@leegoodrich

This comment has been minimized.

Copy link
Author

leegoodrich commented May 12, 2015

Ah, @jpsim, you updated right when I was finishing my comment! Yeah, so it sounds like that works as a work-around. Well, I figure you guys will figure out a solution here (or bug Apple on Radar enough to get them to fix it in the next Swift update, let me know if you need a dupe). I might go ahead with this for now.

@segiddins

This comment has been minimized.

Copy link
Contributor

segiddins commented May 12, 2015

@jpsim

This comment has been minimized.

Copy link
Contributor

jpsim commented May 12, 2015

I'm closing this issue since it's out of our hands, but I've opened #1926 to make sure we document this as a limitation and provide work-arounds. Thanks for reporting @leegoodrich.

@jpsim jpsim closed this May 12, 2015

@shahankit

This comment has been minimized.

Copy link

shahankit commented Aug 15, 2015

@jpsim Can implementing a getter function for private List objects instead of creating two variables as mentioned by @leegoodrich work good?.

@segiddins

This comment has been minimized.

Copy link
Contributor

segiddins commented Aug 15, 2015

@shahankit yup!

By the way, I believe this has been fixed in the Xcode 7 betas.

@shahankit

This comment has been minimized.

Copy link

shahankit commented Aug 15, 2015

Ok so, List no longer are required to be declared as private in Xcode 7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.