How to access a Freestreamer Objective C class from Swift class? - ios

I am totally new to iOS development, and I decided to start directly with the new Swift programming language. But some Objective C knowledge is needed though, so this is a very basic question!
I am trying to use the FreeStreamer library, to play a shoutcast stream in my iOS app. I followed the basic steps (copied among others the needed file in my Xcode project) but how can I access to the FSAudioStream from my Swift class?
I tried this:
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let test = FSAudioStream
}
// ...
}
But the FSAudioStream class is not found, which doesn't surprise me. Should I add an extra import to my file? In that case, which one?

Ok, I found the solution from this Apple Developer page on mixing Swift and Objective C.
But there was something important: when setting the "Objective-C Bridging header" in the "Swift Compiler - Code Generation" section of the project, the path has to be set generally which in turns set value for both the "Debug" and "Release" keys.
So I set the "Swift Compiler - Code Generation" parameter to MyProject/MyProject-Header.h.
And in the MyProject/MyProject-Header.h file I added this:
#import "FSAudioStream.h"
And then there is nothing to import in the Swift file.
Problem solved!

Related

how to use the objective c framework class in swift project? [duplicate]

This question already has answers here:
How do I call Objective-C code from Swift?
(17 answers)
Closed 4 years ago.
As I developed the framework which is having both swift and objective c files.When I accessing the swift class in swift project it is working fine but when I tried to access the objective c class in swift i can't able to access and I also bridged the objective c file in swift project.can anyone help me to solve this above problem.
//swift project
import ObjCSwiftFramework
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let callingswiftfile = SwiftFile() //calling the swift file
let callingobjectivecfile = ObjectiveCFile()//**Use of unresolved identifier 'ObjectiveCFile'**
}
}
//Bridge file
#ifndef ObjCSwift_project_Bridging_Header_h
#define ObjCSwift_project_Bridging_Header_h
#import "ObjCSwiftFramework/ObjCSwiftFramework.h"
#endif
I also added the framework screen shot for your reference
for using objective-c files in swift you need to us Bridging header and now just simply Import the class name in that as below
Name of bridging file is like AppName-Bidging-Header.h
Create a Bridging Header
https://mycodetips.com/ios/manually-adding-swift-bridging-header-1290.html
File will look like after adding and importing obj-c class
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "CardIO.h"
you need to use bridging header. To know how to create a bridging header use below link
https://mycodetips.com/ios/manually-adding-swift-bridging-header-1290.html

{Module_name}-Swift.h file not working well only in Swift 4 projects unlike Swift 3.2

Anyone faced problems in {Module_name}-Swift.h file for Swift 4 projects? I've noticed -Swift.h autogenerated file not working well with Swift 4 syntax unlike Swift 3.2!.
For example, -Swift.h file doesn't contain all variables and methods which implemented in the custom Swift classes which inherited from NSObject class!
I've used #objc and #classkeywords but no way.
I don't get any errors! the problem is if I've created a class like this:
import Foundation
class Utils: NSObject {
let abc: String?
func xyz() {
print("")
}
}
and navigate to {Module_name}-Swift.h I see something like that:
SWIFT_CLASS("_TtC3{Module_name}5Utils")
#interface Utils : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
#end
Problem
Both let abc: String? and func xyz() have been never included in {Modue_name}-Swift.hfile!
I think in Swift 4 you have to mark a lot more things #objc (nothing implicit anymore) but other than that it should just be in there.
You can all check it to confirm class name in .h file like:
#class filename;
The generated file {Module}-Swift.h does not contain your variables and methods, the file is generated to give you access to the Module namespace.
The actual interface for the generated module lives in Module.swiftmodule/arm64.swiftmodule (depending on built architecture).
More information on its contents:
https://bugs.swift.org/browse/SR-2502
https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160111/000827.html
https://stackoverflow.com/a/24396175/1755720
however... the format is not documented anywhere and is subject to change. A good starting point would be to look in include/swift/Serialization/ModuleFormat.h
As to why it's not working - Swift 4 has a migration process, please ensure you have followed it: https://swift.org/migration-guide-swift4/
Xcode will pick up most things ... but it won't get everything!
And why do you need header files for Swift classes? You just can mark swift class as #objc and you will be able to reach all its properties.

Compile and runtime failures when importing interfaces with category extensions in XCode 7

I'm trying to get an example of running OpenEars with the RapidEars plugin running in Swift 2.2 (XCode 7.3.1). However, I suspect I'm having a larger issue with using Objective-C interfaces with extensions in a Swift project (or my understanding of how that works).
The OpenEars code is Obj-C. However I was able to get it running in my swift project through the standard Obj-C -> Swift translation techniques.
Abbreviated code follows. The full example is on a forked Github and updated to Swift-2.2: https://github.com/SuperTango/OpenEars-with-Swift-
This following example is working great. You can see the entire project by checkng out the "working-opears-swift2.2" tag.
OpenEarsTest-Bridging-Header.h:
#import <OpenEars/OELanguageModelGenerator.h>
#import <OpenEars/OEAcousticModel.h>
#import <OpenEars/OEPocketsphinxController.h>
#import <OpenEars/OEAcousticModel.h>
#import <OpenEars/OEEventsObserver.h>
ViewController.swift:
class ViewController: UIViewController, OEEventsObserverDelegate {
var openEarsEventsObserver = OEEventsObserver()
override func viewDidLoad() {
super.viewDidLoad()
loadOpenEars()
}
func loadOpenEars() {
self.openEarsEventsObserver = OEEventsObserver()
self.openEarsEventsObserver.delegate = self
var lmGenerator: OELanguageModelGenerator = OELanguageModelGenerator()
addWords()
var name = "LanguageModelFileStarSaver"
lmGenerator.generateLanguageModelFromArray(words, withFilesNamed: name, forAcousticModelAtPath: OEAcousticModel.pathToModel("AcousticModelEnglish"))
lmPath = lmGenerator.pathToSuccessfullyGeneratedLanguageModelWithRequestedName(name)
dicPath = lmGenerator.pathToSuccessfullyGeneratedDictionaryWithRequestedName(name)
}
func startListening() {
do {
try OEPocketsphinxController.sharedInstance().setActive(true)
OEPocketsphinxController.sharedInstance().startListeningWithLanguageModelAtPath(lmPath, dictionaryAtPath: dicPath, acousticModelAtPath: OEAcousticModel.pathToModel("AcousticModelEnglish"), languageModelIsJSGF: false)
} catch {
NSLog("Error!")
}
}
// A whole bunch more OEEventsObserverDelegate methods that are all working fine...
func pocketsphinxDidStartListening() {
print("Pocketsphinx is now listening.")
statusTextView.text = "Pocketsphinx is now listening."
}
Up until this point, everything is working great.
However, In order to use the "RapidEars" plugin, the documentation (http://www.politepix.com/rapidears/) says to:
Add the framework to the project and ensure it's being included properly.
import two new files (that are both "categories" to existing OpenEars classes):
#import <RapidEarsDemo/OEEventsObserver+RapidEars.h>
#import <RapidEarsDemo/OEPocketsphinxController+RapidEars.h>
Change methods that used: startListeningWithLanguageModelAtPath to use startRealtimeListeningWithLanguageModelAtPath
add two new OEEventsObservableDelegate methods.
func rapidEarsDidReceiveLiveSpeechHypothesis(hypothesis: String!, recognitionScore: String!)
func rapidEarsDidReceiveFinishedSpeechHypothesis(hypothesis: String!, recognitionScore: String!)
The new code can be found by checking out the rapidears-notworking-stackoverflow tag from the above github repo
Problem 1:
When doing completion in the XCode editor, the editor sees WILL perform autocompletion on the startRealtimeListeningWithLanguageModelAtPath method, however when the code is run, it always fails with the error:
[OEPocketsphinxController startRealtimeListeningWithLanguageModelAtPath:dictionaryAtPath:acousticModelAtPath:]: unrecognized selector sent to instance 0x7fa27a7310e0
Problem 2:
When doing auto completion in the XCode editor, it doesn't see the two new delegate methods defined in RapidEarsDemo/OEPocketsphinxController+RapidEars.h.
I have a feeling that these are related, and also related to the fact that they failing methods are defined as Categories to Objective-C classes. But that's only a guess at this point.
I've made sure that the RapidEars framework is imported and in the framework search path.
Can anyone tell me why this is happening? Or if there's some Swift magic incantation that I missed?
The problem could be the one described in the link below, where category methods in a static library produce selector not recognized runtime errors.
Technical Q&A QA1490: Building Objective-C static libraries with categories

I want to call a class function written in Swift from the AppDelegate (which is in Objective C). What am I doing wrong?

I have a swift file "SomeController.swift" it is like this:
import Foundation
func performSomeStuff() {
println("Performing stuff")
}
Now in the app delegate, I am trying to do this: (note that the swift bridging header is imported)
[SomeController performSomeStuff]
But its not working.
I have also tried this:
import Foundation
class SomeController:NSObject {
class func performSomeStuff() {
println("Performing stuff")
}
}
But it still fails.
What is the correct way?
Add:
#objc
before the class keyword in your swift code so it will be:
#objc class SomeStuff: NSObject {
}
Also add #obj in front of any function that you want to call.
Then in your app delegate make sure to use #import "projectName-Swift.h"
Are you able to access "SomeController" class in objective-c, if not then you firstly need to add "${ProjectName}-Swift.h file and add Swift compilation support in Build settings as:
And for accessing methods from Swift to Objective-C, add
import Foundation
class SomeStuff:NSObject {
#objc class func performSomeStuff() {
println("Performing stuff")
}
}
before functions name.
In some cases, you need finer grained control over how your Swift API is exposed to Objective-C. You can use the #objc attribute if your Swift class doesn’t inherit from an Objective-C class, or if you want to change the name of a symbol in your interface as it’s exposed to Objective-C code.
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html
Ok, So I figured it out. First, here's the correct way. Its like some of you said:
In the swift file, I have it like this:
class MyController:NSObject{
class func performTask {
// Here my task is running.
}
}
Then in the app delegate, I just import the swift header file. And do this:
[MyController performTask];
Now for the part I had wrong. It was an error on my part, but maybe it'll be useful to someone else out there.
When I first created the first swift file, I had placed it inside a folder within the my Source folder. But when I started having multiple swift files, I moved the bridging header outside that folder and into the main Source folder (just for organising).
The problem was, it did not give me a direct error to tell me what was the problem. I had to check the issue navigator to identify the problem.
Hope this helps someone out there.

How to import swift file into objective-c file in a swift project

The situation is a little complicated. I have a Swift project, and I have imported some Objective-C files into this project. Now, I want to use some Swift classes in these Objective-C files. How to do that?
It seems this reference covers everything:
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
Specifically:
Importing Swift into Objective-C
When you import Swift code into Objective-C, you rely on an
Xcode-generated header file to expose those files to Objective-C. This
automatically generated file is an Objective-C header that declares
the Swift interfaces in your target. It can be thought of as an
umbrella header for your Swift code. The name of this header is your
product module name followed by adding "-Swift.h". (You’ll learn more
about the product module name later, in Naming Your Product Module.)
Example:
Foo.swift in product named ExampleProduct
#objc public class Bar: NSObject {
var x: Int = 3
}
Blah.m in same product:
#import <Foundation/Foundation.h>
#import <ExampleProduct-Swift.h>
void doStuff() {
Bar *y = [[Bar alloc] init];
printf("%ld\n", (long)y.x);
}
It's even easy to call doStuff() from a Swift file if you create an Objective-C header file that defines the function prototype and then import that header file in the bridging header.
There's no limit to jumping back and forth.
Based on the comments it looks like you're having trouble importing the reverse bridging header.
Try this in terminal to ensure you're naming the file correctly:
% cd ~/Library/Developer/Xcode; find . -name '*-Swift.h'
I get (scroll all the way to the right):
./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProduct.build/DerivedSources/ExampleProduct-Swift.h
./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProduct.build/Objects-normal/x86_64/ExampleProduct-Swift.h
./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProductTests.build/DerivedSources/ExampleProductTests-Swift.h
./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProductTests.build/Objects-normal/x86_64/ExampleProductTests-Swift.h
Also, potentially a dup of:
How to call Objective-C code from Swift
With 326 upvotes that's worth studying!

Resources