codestergit / SweetAlert-iOS

Live animated Alert View for iOS written in Swift
MIT License
2.05k stars 304 forks source link

Help with using with Objective-C project #5

Open harkmall opened 9 years ago

harkmall commented 9 years ago

How do I use this in my Objective-C project. It lets me do the :

        SweetAlert *newAlert=[[SweetAlert alloc]init];
        [newAlert showAlert:@"Error"];

but none of the customizing like adding a subtitle or changing the type of alert. Any help is appreciated. Thanks!

marvi14 commented 9 years ago

I'm facing exactly the same problem...i would love to use this great library in Objective-C

nmizoguchi commented 9 years ago

I was trying to do the same thing here. From Apple Documentation:

You’ll have access to anything within a class or protocol that’s marked with the @objc attribute as long as it’s compatible with Objective-C. This excludes Swift-only features such as those listed here:

  • Generics
  • Tuples
  • Enumerations defined in Swift
  • Structures defined in Swift
  • Top-level functions defined in Swift
  • Global variables defined in Swift
  • Typealiases defined in Swift
  • Swift-style variadics
  • Nested types
  • Curried functions

So the other overloaded methods are not available in the ObjC interface.

What I did was removing the AlertStyle enum from the methods, and used an Int type, redefining the enum elsewhere to match the AlertStyle enum.

susrisha commented 8 years ago

@nmizoguchi can you please explain how you did it? I am facing same issue.

nmizoguchi commented 8 years ago

@susrisha XCode won't generate the Obj-C interface unless there are no Swift Only input or output in the method, because it doesn't know how to do it (the reference to this is in the previous post).

If you look in the SweetAlert.swift, you'll see this enum: public enum AlertStyle { case Success,Error,Warning,None case CustomImag(imageFile:String) } This CustomImag property is not supported by Obj-C. Also, the methods to show an alert all use this enum, and that is why it isn't available in Objective-C environment.

One solution is to remove the CustomImag, and in the method: public func showAlert(title: String, subTitle: String?, style: AlertStyle,buttonTitle: String,buttonColor: UIColor,otherButtonTitle: String?, otherButtonColor: UIColor?,action: ((isOtherButton: Bool) -> Void)? = nil) edit the case where CustomImag is used. This will disable Custom Images, but you'll be able to use it in Objective-C code.

JosephShenton commented 7 years ago

Any fix yet?