AppDelegate unrecognized selector sent to instance - objective-c

i am working on a project and i am implementing this SDK https://docs.drivequant.com/trip-analysis/ios/get-started and I am implementing it in appdelegeate.m
here how I import the module
#import DriveKitCoreModule;
#import DriveKitTripAnalysisModule;
and here how I initialise it
[[DriveKit shared] initialize];
[[DriveKit shared] setApiKeyWithKey: #""];
[[DriveKit shared] setUserIdWithUserId: #""];
[[DriveKit shared] enableLoggingWithShowInConsole:YES];
[[DriveKitTripAnalysis shared] initializeWithTripListener: self appLaunchOptions:launchOptions];
but I always get this warning with initializeWithTripListener
and after running I got this error
does anyone know why I am getting this error? and how to fix it?

See this link for a description of how you declare that an Objective-C class conforms to a protocol. (Specifically the section titled "Conforming to Protocols".)
Looking at the readme for the framework you are using, it says:
Make the AppDelegate class implement TripListener protocol. This protocol contains 6 methods to implement:
func tripPoint(tripPoint: TripPoint) {
}
func tripStarted(startMode: StartMode) {
}
func tripCancelled(cancelTrip: CancelTrip) {
}
func tripFinished(post: PostGeneric, response: PostGenericResponse) {
}
func tripSavedForRepost(){
}
func beaconDetected(){
}
func significantLocationChangeDetected(location: CLLocation){
}
func sdkStateChanged(state: State){
}
So you will need to add implementations for those 6 methods to your app delegate. (Those method declarations are in Swift. If you're writing your app delegate in Objective-C you'll need to find the Objective-C function prototypes.)

Related

Unable to call some methods from an Objective-C class in a Swift extension of that class

I have an objective-c class something like:
#interface MyClass: NSObject { }
- (MyObject *)coolMethod;
#end
#implementation MyClass
- (MyObject *)coolMethod {
return [self doCoolStuff];
}
#end
...and a Swift extension something like:
extension MyClass {
#objc func addedMethodInSwift() {
let coolObj = coolMethod() // <<<< compiler error - method not found
}
}
The code will not compile, because the compiler cannot find the implementation of coolMethod(), however certain other methods from the Objective-C class can be called from the Swift extension.
I have checked and the Objective-C header file is included in the project's bridging header, so it should be visible to Swift. The method in question is definitely visible in the header file.
Why can't coolMethod() be called from the Swift extension?
Check that both your objective-C class and the return type of the function (in your case MyObject.h) are included in the Bridging header. Methods with return types not included in the bridging header are not available from Swift.

Swift functions are not included into autogenerated -Swift.h umbrella file

I'm trying to access Swift class methods from Objective-C, following this guide
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
This is my Swift class:
import Foundation
#objc public class MySwiftClass: NSObject {
// modifiers public, open do not help
func Hello()->String {
return "Swift says hello!";
}
}
And here is an excerpt from the autogenerated "umbrella" file MyProductModuleName-Swift.h
SWIFT_CLASS("_TtC25_MyProductModuleName12MySwiftClass")
#interface MySwiftClass : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
#end
It seems like XCode completely ignores methods of MySwiftClass, and as a result I can't access them from Objective-C. Xcode version is 9.2.
Is it a bug or I missed anything?
You need to add #objc before each function you want to access from Objective-C:
#objc public class MySwiftClass: NSObject {
// modifiers public, open do not help
#objc func Hello() -> String {
return "Swift says hello!";
}
}

Can not find Swift Protocol declaration in Obj-C class

I have created on Class in Swift and that class and its protocol I am using in Obj-C enabled project but I am getting below error while compiling my project.
cannot find protocol declaration for 'SpeechRecognizerDelegate'; did
you mean 'SFSpeechRecognizerDelegate'?
Can anyone guide me on this how can I use Swift class protocol in my Obj-C class.
Here is my Swift code:
protocol SpeechRecognizerDelegate : class {
func speechRecognitionFinished(_ transcription:String)
func speechRecognitionError(_ error:Error)
}
class SpeechRecognizer: NSObject, SFSpeechRecognizerDelegate {
open weak var delegate: SpeechRecognizerDelegate?
}
Protocol use in Obj-C:
#import "ARBot-Swift.h"
#interface ChatScreenViewController : JSQMessagesViewController <SpeechRecognizerDelegate>
Let me know if required more info.
Thanks in Advance.
in Swift:
#objc public protocol YOURSwiftDelegate {
func viewReceiptPhoto()
func amountPicked(selected: Int)
}
class YourClass: NSObject {
weak var delegat: YOURSwiftDelegate?
}
in Objective-C headerFile.h
#protocol YOURSwiftDelegate;
#interface YOURController : UIViewController < YOURSwiftDelegate >
in Objective-C Implementation.m
SwiftObject * swiftObject = [SwiftObject alloc] init];
swiftObject.delegate = self
Define your Swift protocol like this inside your Swift file.
#objc protocol SpeechRecognizerDelegate: class{
func speechRecognitionFinished(_ transcription:String)
func speechRecognitionError(_ error:Error)
}
Create a Swift Module inside your project setting then use it. You can find here complete blog for the mix language coding.
Then use Protocol inside Objective C class,
We required to add protocol inside Objective C file -
#import "ARBot-Swift.h"
#interface ChatScreenViewController : JSQMessagesViewController <SpeechRecognizerDelegate>
Then you need to conform to the protocol methods -
- (void)viewDidLoad {
[super viewDidLoad];
SpeechRecognizer * speechRecognizer = [[SpeechRecognizer alloc] init];
speechRecognizer.delegate = self;
}
#pragma mark - Delegate Methods
-(void)speechRecognitionFinished:(NSString *) transcription{
//Do something here
}
-(void)speechRecognitionError:(NSError *) error{
//Do something here
}
I had a similar issue after following (import header + Objc annotation on protocol). I got the warning when using Swift code from Objective C headers. Solved by only importing into the implementation .m files.
Add #objc attribute to your protocol:
#objc protocol SpeechRecognizerDelegate : class {
//...
}
Include Swift Classes in Objective-C Headers Using Forward Declarations
//MySwiftClass.swift
#objc protocol MySwiftProtocol {}
#objcMembers class MySwiftClass {}
// MyObjcClass.h
#class MySwiftClass;
#protocol MySwiftProtocol;
#interface MyObjcClass : NSObject
- (MySwiftClass *)returnSwiftClassInstance;
- (id <MySwiftProtocol>)returnInstanceAdoptingSwiftProtocol;
// ...
#end

Crash when calling Obj-C method with nullability annotations from Swift

We came across this issue after we upgraded to AFNetworking 2.6.0, which added nullability annotations, and UIWebView's extension -loadRequest:progress:success:failure: started crashing. Below is minimal example causing the crash.
Obj-C class:
NS_ASSUME_NONNULL_BEGIN
#interface Foo : NSObject
- (void) fooWithSuccess:(NSString* (^)(NSString* string))success;
#end
NS_ASSUME_NONNULL_END
#implementation Foo
- (void)fooWithSuccess:(NSString* (^)(NSString*))success
{
// EXC_BAD_ACCESS on the following line
NSLog(#"%#", success(#"Foo"));
}
#end
Calling the code above from swift (e.g. in viewDidAppear()):
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
foo.fooWithSuccess { string in
return string
}
}
However, it works without problems if Obj-c code is either not annotated or return parameter of success block is nullable:
- (void) fooWithSuccess:(NSString* __nullable (^)(NSString* string))success;
It seems to us that this behaviour is bug in Obj-c and Swift2 interoperability, and we are wondering if anyone has any insight / we are missing something.
Update
Issue is only with Swift2. When running in Swift 1.2 program behaves as expected.
Radar
Radar has been filed: rdar://22911182

How to extend an Objective-C class with Swift in a framework?

I have an (OS X) Objective-C framework to which I want to add some Swift extensions and I'm using Xcode 7ß6 to work with this. There is class in the framework (let's call it "Sample") implemented in the files "Sample.h" and "Sample.m" .. "Sample.h" contains:
#import <Foundation/Foundation.h>
#interface Sample : NSObject
#property int x;
#end
.. and "Sample.m" contains:
#import "Sample.h"
#implementation Sample
- (instancetype) init {
if ((self = [super init]) == nil) return nil;
self.x = 99;
return self;
}
#end
I have added "Sample.swift" to the framework containing:
import Foundation
extension Sample {
func PrettyPrint () {
print("\(x)")
}
}
This is clearly a trivial version of what I want to do in a larger context, here I want to use the Swift file to extend "Sample" by adding a "PrettyPrint" function.
.. the framework builds without error, but the framework function "PrettyPrint" is not visible to a calling app. App code calling into the framework like:
import Foundation
import TestKit
let sample = Sample()
sample.PrettyPrint()
fails on "sample.PrettyPrint()" with: Value of type 'Sample' has no member 'PrettyPrint'
Why does this fail? and Can it be made to work?
Extra info: If I remove the file "Sample.swift" from the framework and place it the app which is calling into the framework, the "Sample" class is successfully extended and "sample.PrettyPrint()" works as expected (printing "99").
Have you tried making the extension and function public?
public extension Sample {
public func PrettyPrint () {
print("\(x)")
}
}
In a similar case, Tie's answer really helped me but it didn't work for me, I had to add #objc for the method:
public extension SearchBarWidget {
#objc public func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.endEditing(true)
}
}
The SearchBarWidget class was #objc public signatured.