ios


Fatal Error when connectting Objective-C framework to Swift iOS project (SpeechSDK framework)


I converted an Objective-C Speech Recognition API Swift as is.
class ViewController: UIViewController, SpeechRecognitionProtocol {
var micClient: MicrophoneRecognitionClient
var isMicrophoneReco: Bool
/* Action for pressing the "Start" button */
#IBAction func startButtonTapped(sender: AnyObject) {
if isMicrophoneReco {
var status: OSStatus = micClient.startMicAndRecognition()
}
}
I got following errors:
Type 'ViewController' does not conform to 'SpeechRecognitionProtocol'
Class 'ViewController' has no initializers
I fixed as following:
class ViewController: UIViewController {
var micClient: MicrophoneRecognitionClient?
var isMicrophoneReco: Bool = true
/* Action for pressing the "Start" button */
#IBAction func startButtonTapped(sender: AnyObject) {
if isMicrophoneReco {
var status: OSStatus = micClient!.startMicAndRecognition()
}
}
I am getting
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) at
micClient!.startMicAndRecognition()
line inside startButtonTapped function. Apparently, when converting Objective-C based APIs, it is common for values to be declared as an optional although they are always expected to return a value. But Swift requires to initialize variables.
I am working on this for days now. How can I eliminate this error?
The first error:
Type 'ViewController' does not conform to 'SpeechRecognitionProtocol' Class
is saying that your class declares that it conforms to the SpeechRecognitionProtocol but does not implement the required method(s).
I can only guess that SpeechRecognitionProtocol is from here, and you are not implementing all the required methods.
Simply removing it fixes the compile-time error, but you lose the SpeechRecognitionProtocol functionality, and that depends on whether you want it or not.
The second error:
'ViewController' has no initializers
is because all instance variables in swift must either be initialised when they are declared, or initialised in their class init method, or declare optional (or as implicitly-unwrapped optionals).
Your fix for this second error was to declare micClient as optional. However in startButtonTapped you force-unwrap it, and since you don't initialise micClient anywhere, it is nil, and you get your run-time exception.
To fix this, you need to initialise micClient at some point.
var status: OSStatus = micClient!.startMicAndRecognition()
! Means you are force unwrapping this object and if it is not there it will break so you could wrap it in a guard statement or an if let So before that line do something like this
guard let client = micClient as? MicrophoneRecognitionClient else {
NSAssertFailure("error No MicrophoneClient")
}
client.startMicAndRecognition()

Related Links

Hiding left / rightBarButtonItem based on how view controller is presented
NSTimer invalidate not working on stop button click
Passing value from Firebase from one tableView to another
Program not working anymore if I rotate device due to gesture recognizer
How to make a tableview snap to cells centers ? (with gif animation illustration)
Change Polyline Color Conditionally
How can I pass data back to ViewController from delegated class method in swift?
Stuck! can only pass data for presentviewcontroller, not pushviewcontroller
Place UIViewController inside UIView
UIView is shown after IBAction method returns
How to conform to a protocol based on OS version ?
ITunes connect not showing app after 24 hours
Debugging (owned) Framework when using Carthage
No tab bar value showing when returned from a non-tabbed view controller
Issue with implementing “Login with Github” in iOS app
How to run hybrid applications ionic/nativescript in the simulator ios?

Categories

HOME
amazon-s3
document
lsf
tags
rtmp
isis
sql-server-2012
word-vba
fonts
google-cloud-dataproc
cql3
feed
redhat
visual-studio-2012
apple-mail
regression
tcplistener
sql-server-express
pouchdb
sonata-admin
chart.js2
union
scale
pyfftw
deserialization
accumulo
nsmutableattributedstring
libpcap
jscodeshift
cayley
centroid
csom
urlrewriter.net
core-location
selenium-edgedriver
shape
hawq
mathdotnet
meanjs
expression-trees
buffer-overflow
json-c
django-users
complex-networks
querydsl
macvim
angular-xeditable
atlassian-bitbucketserver
protobuf-3
message-hub
pac
tabview
arm7
vega-lite
wrk
masm32
ifc
tfs2008
logstash-jdbc
cglib
facebook-audience-network
webpagetest
activity-diagram
kernighan-and-ritchie
webmail
android-popupwindow
linq2db
session-replication
slick2d
ratingbar
antisamy
disabled-input
blueprint
mips64
rte
xcache
sugar.js
chomsky-hierarchy
rcu
ccombobox
mms-gateway
haar-wavelet
mixing
timestamp-with-timezone
datakey
google-website-optimizer
jsr223
vim-fugitive
android-screen
junitperf
codi
hunchentoot
clipboard-pictures

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App