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

Init method not visible to ObjC #11

Closed
heyarny opened this issue Jun 13, 2017 · 11 comments
Closed

Init method not visible to ObjC #11

heyarny opened this issue Jun 13, 2017 · 11 comments

Comments

@heyarny
Copy link

heyarny commented Jun 13, 2017

You seem to be using optional parameters within the constructors which can't be bridged to ObjC.
Thus this library is not usable in ObjC projects.

See: https://stackoverflow.com/questions/26470771/swift-init-not-visible-in-objecitve-c

@HarshilShah
Copy link
Owner

Thanks for pointing this out.

As you can guess from this bug, I have no experience with Objective-C, so I’m gonna need a little help on this one. How would you go around fixing this? I’m not sure if/how closures are represented in Obj-C, or how to represent them in types that can be bridged. If you could possibly file a PR, that would be great.

@hamedh
Copy link

hamedh commented Jun 14, 2017

You can add a init constructor like this (if you don't care about the animation blocks) and it will be bridged to objective-c

public init(presentDuration: TimeInterval, dismissDuration: TimeInterval) {
        self.presentDuration = presentDuration
        self.presentAnimation = nil
        self.presentCompletion = nil
        self.dismissDuration = dismissDuration
        self.dismissAnimation = nil
        self.dismissCompletion = nil
    }

@HarshilShah
Copy link
Owner

Thanks! Is there any way to represent the various closures in Obj-C though? Would hate for Obj-C users to not be able to access the full capabilities.

@HarshilShah
Copy link
Owner

Could you folks take a look at the ObjCTest branch? I don't know a lick of Objective-C, so just going by some random stuff I'm coming across

@gcox
Copy link
Contributor

gcox commented Aug 2, 2017

Works fine for me, tested that all the callbacks work.

You might consider adding an additional objc init that only has args for the various blocks/closures so we don't have to guess at durations to provide if we just want it to use the defaults.

@HarshilShah
Copy link
Owner

Nice, good to know. Just put this out there to see if the closures work

@HarshilShah
Copy link
Owner

Anyone know how to mark a method as visible in Objective-C but not in Swift?

@gcox
Copy link
Contributor

gcox commented Aug 2, 2017

I don't think you can do that.

If you're wanting to just have one ctor, you could switch those TimeInterval arguments to NSNumber, which would be compatible with obj-c. Then, you can just unbox the doubleValue inside your init.

public init(presentDuration: NSNumber? = nil,
    presentAnimation: (() -> ())? = nil,
    presentCompletion: ((Bool) -> ())? = nil,
    dismissDuration: NSNumber? = nil,
    dismissAnimation: (() -> ())? = nil,
    dismissCompletion: ((Bool) -> ())? = nil) {
  self.presentDuration = presentDuration?.doubleValue;
  self.presentAnimation = presentAnimation
  self.presentCompletion = presentCompletion
  self.dismissDuration = dismissDuration?.doubleValue;
  self.dismissAnimation = dismissAnimation
  self.dismissCompletion = dismissCompletion
}

Of course, that forces Swift callers to pass in NSNumber instances if they choose to specify durations, so you may not be happy with that.

@gcox
Copy link
Contributor

gcox commented Aug 2, 2017

Actually, I just tried that and it doesn't affect swift callers, so this still works fine:

let transition = DeckTransitioningDelegate(presentDuration: 2.5)

@HarshilShah
Copy link
Owner

Hmm, good find. One tiny issue is it breaks existing code if you've set it up with types variables and not just numbers the type for which can be inferred to be whatever is needed. Not sure about this

@HarshilShah
Copy link
Owner

NSNumber it is. Don't think there's gonna be many issues, and none that won't be easily fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants