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

How to Allow Prospective Developer to Review/Test Live iOS App
how to make a tableview take off where it was after switching back from another view? [duplicate]
Custom CPTPlotDataSource not being called when self was
Editing an existing tableview row to enter new text
Accessing Geolocation detail from images - ios 8.3
Calling objective c function from SWIFT through bridging header
xCode7 how to access “energy gauge”?
How do I add a sign up page into my Parse app with Swift?
Facebook Login Swift don't works On Device (But do on IOS Simulator)
UITableView under UINavigationBar with a black top background in IOS 9
View stack or Drawing
How to know if my iOS app has the correct Bundle ID?
Is it possible to spin another process from within an iOS application?
How to drag the cell on right in UITableView when tap, like in Snapchat?
Issues with map view camera heading
How to create UIProgressViewStyleBar on UIAlertview iOS 7?

Categories

HOME
common-lisp
coffeescript
watson
react-navigation
passwords
windows-runtime
stack
xquery
addeventlistener
intl
tcplistener
wav
google-maps-sdk-ios
superfish
pymc3
google-weather-api
zend-expressive
union
vscode-settings
curve-fitting
jscodeshift
beanstalkd
haml
deadbolt
azure-ad-graph-api
missingmethodexception
cadvisor
shape
vnc
angular-services
android-security
buffer-overflow
magento2.0.2
doctrine-odm
gradient-descent
angularjs-routing
wp-api
rtems
php-deployer
azure-cdn
spring-form
ios-app-group
kony
maven-versions-plugin
unityscript
javascript-security
easyhook
glumpy
netbeans-7
waf
mysql-error-1050
callkit
ipmi
vistadb
cglib
insert-update
menustrip
geneos
groups
groupbox
sliding
vimeo-ios
ctakes
pakyow
parsoid
clientid
kinto
pypdf
coldfusion-10
sample-data
phone-number
android-listfragment
viennacl
equinox
scalariform
abort
wintersmith
generalization
cordova-cli
gitattributes
newsql
imake
slick2d
datacontractserializer
keyboard-navigation
youtrack-api
lightstreamer
mod-python
hresult
nextpeer
arr
rgba
ekeventkit
nsinvocation
consistent-hashing
android-searchmanager
indextank
cassandra-0.7
contracts
68hc11
large-teams

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