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

How to use this framework with objective-c? #28

Closed
afiq90 opened this issue Jan 1, 2016 · 10 comments
Closed

How to use this framework with objective-c? #28

afiq90 opened this issue Jan 1, 2016 · 10 comments

Comments

@afiq90
Copy link

afiq90 commented Jan 1, 2016

How to use this framework with objective-c?

@RamonGilabert
Copy link
Contributor

Hey @afiq90! Well this framework wasn't thought for Objective C to be honest, and it's been a while since I do a project with it, so I don't know how that stuff work anymore to do a Swift - Objective C podfile. Sorry about that.

@bjones2015
Copy link

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

The "Importing Swift into Objective-C" section gives instructions on how to do this.

@RamonGilabert
Copy link
Contributor

Thank's @bjones2015.

@brianinator
Copy link

+1 I really like the concept and would like to use in an obj-c project.

@bcbroom
Copy link

bcbroom commented Jan 5, 2016

As is, it will not run from Obj-C. Obj-C cannot use Swift structs or functions outside of a class declaration. To use this as is, you need to add a "bridge" layer by adding a swift class, reachable from Obj-C, that passes the call on to the swift version. I did something like this

// WhisperBridge.swift
import Foundation
import Whisper

@objc public class WhisperBridge: NSObject {

  static public func whisper(text: String, backgroundColor: UIColor, toNavigationController: UINavigationController, silenceAfter: NSTimeInterval) {
    let message = Message(title: text, color: backgroundColor)
    Whisper(message, to: toNavigationController)

    if silenceAfter > 0.1 {
      Silent(toNavigationController, after: silenceAfter)
    }
  }
}

Then in ViewController.m

#import "ViewController.h"
#import "WhisperTest-swift.h"

@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];

  [WhisperBridge whisper:@"Message to Send!" backgroundColor:[UIColor orangeColor] toNavigationController:self.navigationController silenceAfter:3.0];
}
@end

@brianinator
Copy link

@bcbroom What is #import "WhisperTest-swift.h" from? Can you give more info on this class? I am trying get the WhisperBridge swift file to be recognized in obj-c class. I get file not found when I use #import "WhisperBridge-Swift.h" in obj-c class.

@bcbroom
Copy link

bcbroom commented Jan 6, 2016

Its a generated header file - the pattern is "TargetName"-Swift.h. My project (and default target name) was "WhisperTest". There is only one for the whole target, not one for each Swift file.

@brianinator
Copy link

@bcbroom Oh I see. Thanks and I changed it and it worked. 👍

@zenangst
Copy link
Contributor

zenangst commented Jan 7, 2016

@bcbroom that is amazing, thanks for answering this issue and finding a feasible solution for Objective-C support; mind adding it to the README? =)

@RamonGilabert
Copy link
Contributor

So 🔝 @bcbroom! :)

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

6 participants