I am trying to learn swift to make free educational app for my country.
I know only the basics of programming and swift, but I am trying to understand iOS development.
I think I am having a problem with scope, but not sure what to do.
With didMoveToView being (the required) according to books I read, I can't compile and execute the code. So, when I run it in Xcode iOS App Project, my App crashes (even though it works in playground).
The code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
class fooo {
static var bar = "hello world!"
#IBAction func hello_button(sender: AnyObject) {
print(fooo.bar)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Below is the Crash log (Occurs on button touch):
2016-08-26 07:22:00.512 k[2074:37791] -[k.ViewController hello_button:]: unrecognized selector sent to instance 0x7aa2e670
2016-08-26 07:22:00.521 k[2074:37791] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[k.ViewController hello_button:]: unrecognized selector sent to instance 0x7aa2e670'
*** First throw call stack:
(
0 CoreFoundation 0x00200494 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01f17e02 objc_exception_throw + 50
2 CoreFoundation 0x0020a253 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0013f89d ___forwarding___ + 1037
4 CoreFoundation 0x0013f46e _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x01f2c0b5 -[NSObject performSelector:withObject:withObject:] + 84
6 UIKit 0x009f2e38 -[UIApplication sendAction:to:from:forEvent:] + 118
7 UIKit 0x009f2db7 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
8 UIKit 0x00b96f3b -[UIControl sendAction:to:forEvent:] + 79
9 UIKit 0x00b972d4 -[UIControl _sendActionsForEvents:withEvent:] + 433
10 UIKit 0x00b962c1 -[UIControl touchesEnded:withEvent:] + 714
11 UIKit 0x00a7352e -[UIWindow _sendTouchesForEvent:] + 1095
12 UIKit 0x00a745cc -[UIWindow sendEvent:] + 1159
13 UIKit 0x00a15be8 -[UIApplication sendEvent:] + 266
14 UIKit 0x009ea769 _UIApplicationHandleEventQueue + 7795
15 CoreFoundation 0x00112e5f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
16 CoreFoundation 0x00108aeb __CFRunLoopDoSources0 + 523
17 CoreFoundation 0x00107f08 __CFRunLoopRun + 1032
18 CoreFoundation 0x00107846 CFRunLoopRunSpecific + 470
19 CoreFoundation 0x0010765b CFRunLoopRunInMode + 123
20 GraphicsServices 0x04717664 GSEventRunModal + 192
21 GraphicsServices 0x047174a1 GSEventRun + 104
22 UIKit 0x009f0eb9 UIApplicationMain + 160
23 k 0x00012581 main + 145
24 libdyld.dylib 0x02935a25 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
You're right, the issue is scope (assuming you connected your button properly as others mentioned):
import UIKit
class ViewController: UIViewController {
class fooo {
var bar = "hello world!"
func mSayBar() { print(self.bar)}
}
var foo = fooo()
#IBAction func hello_button(sender: AnyObject) {
foo.mSayBar()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
viewDidLoad is it's own local scope--it doesn't extend any further than the end brace " } "
After the first (millisecond?) everything declared in VDL is no longer alive; thus, it is best for initializing or calling, not declaring.
To declare global, just put everything in a new swift file, or put it outside ALL class scopes. Swift makes global scope easy (but be careful)
You said viewDidLoad is required, which is true, but only for setting up the program. Most of your logic / functions go outside of viewDidLoad
unrecognized selector sent to instance 0x7aa2e670 means that the button has been linked to an IBAction outlet that isn't defined in your code. Go to your ViewController's Storyboard mode, and right click the button in the subview list. Check if there is an outlet for the Touch Up Inside event and if it has the same name as the function in your code that you want to call (in your case it's hello_button.
Here's a picture if you didn't understand the above paragraph.
Related
Why do I get a Thread:1 Signal SIGAGBRT within this code? I don't know what to change to fix the error. The application starts and as soon as I press the button the app cancels out and gives me an error.
import UIKit
class ViewController: UIViewController {
#IBOutlet var textFieldInput: UITextField!
#IBOutlet var laCelsius: UILabel!
#IBOutlet var laFahrenheit: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func btn(sender: UIButton) {
var c_out = 0.0
var f_out = 0.0
var inputValue = 0.0
let textInput = NSString(string: textFieldInput.text!)
inputValue = textInput.doubleValue
c_out = (inputValue-32)*5/9
f_out = inputValue * 1.8 + 32
self.laCelsius.text = NSString(format: "%3.2f" ,c_out) as String
self.laFahrenheit.text = NSString(format: "%3.2f" ,f_out) as String
}
}
this is the error code:
2016-09-22 14:15:51.669 DegreeCL[19045:1774964] -[DegreeCL.ViewController btnPressed:]: unrecognized selector sent to instance 0x7fa7bb643d40
2016-09-22 14:15:51.674 DegreeCL[19045:1774964] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DegreeCL.ViewController btnPressed:]: unrecognized selector sent to instance 0x7fa7bb643d40'
*** First throw call stack:
(
0 CoreFoundation 0x000000010de1dd85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010fbc1deb objc_exception_throw + 48
2 CoreFoundation 0x000000010de26d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010dd6ccfa ___forwarding___ + 970
4 CoreFoundation 0x000000010dd6c8a8 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010e647a8d -[UIApplication sendAction:to:from:forEvent:] + 92
6 UIKit 0x000000010e7bae67 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x000000010e7bb143 -[UIControl _sendActionsForEvents:withEvent:] + 327
8 UIKit 0x000000010e7ba263 -[UIControl touchesEnded:withEvent:] + 601
9 UIKit 0x000000010e6ba99f -[UIWindow _sendTouchesForEvent:] + 835
10 UIKit 0x000000010e6bb6d4 -[UIWindow sendEvent:] + 865
11 UIKit 0x000000010e666dc6 -[UIApplication sendEvent:] + 263
12 UIKit 0x000000010e640553 _UIApplicationHandleEventQueue + 6660
13 CoreFoundation 0x000000010dd43301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x000000010dd3922c __CFRunLoopDoSources0 + 556
15 CoreFoundation 0x000000010dd386e3 __CFRunLoopRun + 867
16 CoreFoundation 0x000000010dd380f8 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x00000001124b3ad2 GSEventRunModal + 161
18 UIKit 0x000000010e645f09 UIApplicationMain + 171
19 DegreeCL 0x000000010dc38412 main + 114
20 libdyld.dylib 0x000000011068592d start + 1
21 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
The crucial information is
[DegreeCL.ViewController btnPressed:]: unrecognized selector sent to instance ...'
That means in Interface Builder there is somewhere a dead connection to an action btnPressed. Remove it.
You can search for btnPressed with ⇧⌘F.
SIGABRIT - signal is sent due to many reasons but in this case i think you have problem with Memory.
You should turn on All Exeptions with option of po $arg1 it will identify the error.
Select your button from interface builder, then select connection inspector from utilities and check that if you have extra connected action method should be there, remove it by clicking x and your issue will be solved!
It's should be btnPressed as per your crash log!
I'm creating an app and trying to update a NSUserDefault from a a different view controller, although i don't think its the separate view controller thats doing it because i can't set a new NSUserDefault on this controller either. its crashing whenever i push the button, heres the code from the view controller:
// Upgrades.swift
// Unit3Final
//
// Created by fgstu on 4/14/16.
// Copyright © 2016 AllenH. All rights reserved.
//
import UIKit
class Upgrades: UIViewController {
var tapModifier = (NSUserDefaults.standardUserDefaults().integerForKey("tapModifier"))
func storeTheScore() {
NSUserDefaults.standardUserDefaults().setInteger(tapModifier, forKey: "tapModifier")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func BuyAHut(sender: UIButton) {
tapModifier += 5
storeTheScore()
}
}
it is set in my main view controller in my viewDidLoad with
var tapModifier: Int = 1
NSUserDefaults.standardUserDefaults().setInteger(tapModifier, forKey: "tapModifier")
and the error message is
2016-04-14 12:13:45.264 Unit3Final[40985:5120078] -[Unit3Final.Upgrades hutUpgrade:]: unrecognized selector sent to instance 0x7fadab4b2e80
2016-04-14 12:13:45.270 Unit3Final[40985:5120078] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Unit3Final.Upgrades hutUpgrade:]: unrecognized selector sent to instance 0x7fadab4b2e80'
*** First throw call stack:
(
0 CoreFoundation 0x000000010258de65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001042cddeb objc_exception_throw + 48
2 CoreFoundation 0x000000010259648d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001024e390a ___forwarding___ + 970
4 CoreFoundation 0x00000001024e34b8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000102dac194 -[UIApplication sendAction:to:from:forEvent:] + 92
6 UIKit 0x0000000102f1b6fc -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x0000000102f1b9c8 -[UIControl _sendActionsForEvents:withEvent:] + 311
8 UIKit 0x0000000102f1aaf8 -[UIControl touchesEnded:withEvent:] + 601
9 UIKit 0x0000000102e1b49b -[UIWindow _sendTouchesForEvent:] + 835
10 UIKit 0x0000000102e1c1d0 -[UIWindow sendEvent:] + 865
11 UIKit 0x0000000102dcab66 -[UIApplication sendEvent:] + 263
12 UIKit 0x0000000102da4d97 _UIApplicationHandleEventQueue + 6844
13 CoreFoundation 0x00000001024b9a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x00000001024af95c __CFRunLoopDoSources0 + 556
15 CoreFoundation 0x00000001024aee13 __CFRunLoopRun + 867
16 CoreFoundation 0x00000001024ae828 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x0000000106bcdad2 GSEventRunModal + 161
18 UIKit 0x0000000102daa610 UIApplicationMain + 171
19 Unit3Final 0x000000010201db7d main + 109
20 libdyld.dylib 0x0000000104ddf92d start + 1
21 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
It doesn't have a value, that's why it's crashing, try setting it to 0 initially.
You're adding 5 to nothing, instead of this,
tapModifier = NSUserDefaults etc...
try...
tapModifier = 0
I'm able to run the app initially (no visible errors), but when I input a number in the ageBox area and press the getAge button, I get several errors.
Here is the code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var ageBox: UITextField!
#IBAction func getAge(sender: AnyObject) {
println(ageBox.text)
}
#IBOutlet var answerLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is the error I get:
2015-04-21 10:36:24.183 Cat Years[5284:2484424] Can't find keyplane
that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad;
using 2705787216_PortraitChoco_iPhone-Simple-Pad_Default
2015-04-21 10:36:25.826 Cat Years[5284:2484424] Can't find keyplane
that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad;
using 2705787216_PortraitChoco_iPhone-Simple-Pad_Default
5
2015-04-21 10:36:27.067 Cat Years[5284:2484424]
-[Cat_Years.ViewController getAgeButton:]: unrecognized selector sent to instance 0x7fc881e287f0
2015-04-21 10:36:27.078 Cat Years[5284:2484424] *** Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'-[Cat_Years.ViewController getAgeButton:]: unrecognized selector sent
to instance 0x7fc881e287f0'
*** First throw call stack:
(
0 CoreFoundation 0x00000001044c5c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000106030bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001044cd0ad -[NSObject(NSObject)
doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010442313c forwarding + 988
4 CoreFoundation 0x0000000104422cd8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000104d65da2 -[UIApplication
sendAction:to:from:forEvent:] + 75
6 UIKit 0x0000000104e7754a -[UIControl
_sendActionsForEvents:withEvent:] + 467
7 UIKit 0x0000000104e76919 -[UIControl touchesEnded:withEvent:] + 522
8 UIKit 0x0000000104db2998 -[UIWindow _sendTouchesForEvent:] + 735
9 UIKit 0x0000000104db32c2 -[UIWindow sendEvent:] + 682
10 UIKit 0x0000000104d79581 -[UIApplication sendEvent:] + 246
11 UIKit 0x0000000104d86d1c _UIApplicationHandleEventFromQueueEvent +
18265
12 UIKit 0x0000000104d615dc _UIApplicationHandleEventQueue + 2066
13 CoreFoundation 0x00000001043f9431
CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
14 CoreFoundation 0x00000001043ef2fd __CFRunLoopDoSources0 + 269
15 CoreFoundation 0x00000001043ee934 __CFRunLoopRun + 868
16 CoreFoundation 0x00000001043ee366 CFRunLoopRunSpecific + 470
17 GraphicsServices 0x00000001084aca3e GSEventRunModal + 161
18 UIKit 0x0000000104d64900 UIApplicationMain + 1282
19 Cat Years 0x00000001042c8007 main + 135
20 libdyld.dylib 0x0000000106788145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type
NSException
Any ideas?
Copied your code to a new project. Added a text box, switch, and label. Did the connections. Added constraints using "Add Missing Constraints". And it all worked (with no modification to your view controller code). Here is the link:
Project
The possibilities I can think of (based on errors I made when I started): (1) in changing connections while editing the storyboard I found that deleted controls are not automatically deleted in the xml file. Suggest you look in the connections editor to see if there are controls that you deleted. (2) do a "clean" to make sure.
Good luck
For the keyplane error,try this:
For reference:see this
iOS Simulator -> Hardware -> Keyboard -> Connect Hardware Keyboard
so that the entry is UNchecked then the software keyboard will be displayed once again.
For the NSInvalidArgumentException',try this:
#IBAction func getAge(sender: UIButton) {
println(ageBox.text)
}
I am following udacity's course to try to learn how to make an iPhone app using xcode and swift. I am up to the part where I am playing an audio file when clicking a button in the app. Every time I click it, I get this error message:
in recordAudio
2015-04-05 14:42:18.124 Pitch Perfect[18544:1141273] -[Pitch_Perfect.PlaySoundsViewController slowAudio:]: unrecognized selector sent to instance 0x7f8174001760
2015-04-05 14:42:18.135 Pitch Perfect[18544:1141273] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Pitch_Perfect.PlaySoundsViewController slowAudio:]: unrecognized selector sent to instance 0x7f8174001760'
*** First throw call stack:
(
0 CoreFoundation 0x000000010ab57a75 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010c6afbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010ab5ed1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010aab69dc ___forwarding___ + 988
4 CoreFoundation 0x000000010aab6578 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010b3eba22 -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x000000010b4f2e50 -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x000000010b4f221f -[UIControl touchesEnded:withEvent:] + 522
8 UIKit 0x000000010b431b68 -[UIWindow _sendTouchesForEvent:] + 735
9 UIKit 0x000000010b432493 -[UIWindow sendEvent:] + 683
10 UIKit 0x000000010b3fefb1 -[UIApplication sendEvent:] + 246
11 UIKit 0x000000010b40c227 _UIApplicationHandleEventFromQueueEvent + 17700
12 UIKit 0x000000010b3e723c _UIApplicationHandleEventQueue + 2066
13 CoreFoundation 0x000000010aa8cc91 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x000000010aa82b5d __CFRunLoopDoSources0 + 269
15 CoreFoundation 0x000000010aa82194 __CFRunLoopRun + 868
16 CoreFoundation 0x000000010aa81bc6 CFRunLoopRunSpecific + 470
17 GraphicsServices 0x000000010eb2da58 GSEventRunModal + 161
18 UIKit 0x000000010b3ea580 UIApplicationMain + 1282
19 Pitch Perfect 0x000000010a61d42e top_level_code + 78
20 Pitch Perfect 0x000000010a61d46a main + 42
21 libdyld.dylib 0x000000010ce8b145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
here is the code I have to play the audio. I can't seem to figure out what is wrong. I have been following all of the steps exactly.
// PlaySoundsViewController.swift
// Pitch Perfect
import UIKit
import AVFoundation
class PlaySoundsViewController: UIViewController {
var audioPlayer:AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if var filePath = NSBundle.mainBundle().pathForResource("movie_quote",ofType: "mp3"){
var filePathUrl = NSURL.fileURLWithPath(filePath)
audioPlayer = AVAudioPlayer(contentsOfURL: filePathUrl, error: nil)
} else{
println("the filepath is empty")
}
}
#IBAction func playSlowAudio(sender: UIButton) {
// Play audio slowly
audioPlayer.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Can someone please help me find the issue? I am very new to app development.
It seems you have a method called slowAudio: somewhere, but haven't implemented that method in your controller; maybe connected to a button in IB? Right-click on the button in the storyboard (or xib) to bring up the black window to see if there's a connection there to a method called slowAudio:. If there is, delete the connection by clicking on the little "x".
Similar to many, many other questions on StackOverflow, I get a unrecognized selector sent to instance fault, when running my app. What makes my case different (I think) is that it is written in Swift and that the common reason is not the reason here.
In this question the solution is to set the "Custom Class" in the Identity Inspector for that view to the responding class. I checked this and this is the case (note that I did rename the class at some point, but it now is definitely set to "ViewController").
I include the stack trace below as well as the code for ViewController.swift. You can also find all of the code here. I managed to avoid this problem by starting fresh, which enabled me to solve my earlier question. This new trial can be found in this branch. But, I want to know what is wrong and solve this.
Some thought
One question that I couldn't answer successfully is whether CLLocationManagerDelegate needs some required methods? But because I got it to work in the other branch, I suspect it's not necessary.
2014-07-03 21:50:26.056 RowingTracker2[11416:60b] -[CLLocationManager requestWhenInUseAuthorization]: unrecognized selector sent to instance 0xa10c4a0
2014-07-03 21:50:26.059 RowingTracker2[11416:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CLLocationManager requestWhenInUseAuthorization]: unrecognized selector sent to instance 0xa10c4a0'
*** First throw call stack:
(
0 CoreFoundation 0x008831e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01e2a8e5 objc_exception_throw + 44
2 CoreFoundation 0x00920243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0087350b ___forwarding___ + 1019
4 CoreFoundation 0x008730ee _CF_forwarding_prep_0 + 14
5 RowingTracker2 0x00003352 _TFC14RowingTracker214ViewController11viewDidLoadfS0_FT_T_ + 738
6 RowingTracker2 0x000033a2 _TToFC14RowingTracker214ViewController11viewDidLoadfS0_FT_T_ + 34
7 UIKit 0x0102f33d -[UIViewController loadViewIfRequired] + 696
8 UIKit 0x0102f5d9 -[UIViewController view] + 35
9 UIKit 0x00f4f267 -[UIWindow addRootViewControllerViewIfPossible] + 66
10 UIKit 0x00f4f5ef -[UIWindow _setHidden:forced:] + 312
11 UIKit 0x00f4f86b -[UIWindow _orderFrontWithoutMakingKey] + 49
12 UIKit 0x00f5a3c8 -[UIWindow makeKeyAndVisible] + 65
13 UIKit 0x00f0abc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
14 UIKit 0x00f0f667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
15 UIKit 0x00f23f92 -[UIApplication handleEvent:withNewEvent:] + 3517
16 UIKit 0x00f24555 -[UIApplication sendEvent:] + 85
17 UIKit 0x00f11250 _UIApplicationHandleEvent + 683
18 GraphicsServices 0x031fff02 _PurpleEventCallback + 776
19 GraphicsServices 0x031ffa0d PurpleEventCallback + 46
20 CoreFoundation 0x007feca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
21 CoreFoundation 0x007fe9db __CFRunLoopDoSource1 + 523
22 CoreFoundation 0x0082968c __CFRunLoopRun + 2156
23 CoreFoundation 0x008289d3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x008287eb CFRunLoopRunInMode + 123
25 UIKit 0x00f0ed9c -[UIApplication _run] + 840
26 UIKit 0x00f10f9b UIApplicationMain + 1225
27 RowingTracker2 0x00008021 top_level_code + 97
28 RowingTracker2 0x0000805b main + 43
29 libdyld.dylib 0x024ff701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
ViewController.swift
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet var mapview: MKMapView = nil
var locationmgr : CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationmgr = CLLocationManager()
locationmgr.delegate = self
locationmgr.requestWhenInUseAuthorization() ///< Offending line.
// mapview.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
As requestWhenInUseAuthorization method is only available on iOS 8 (according to Apple Documentation) you have to wrap it:
Swift code:
if (locationmgr.respondsToSelector(Selector("requestWhenInUseAuthorization"))) {
locationmgr.requestWhenInUseAuthorization()
}
And the same for Objective-C:
if ([locationmgr respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[locationmgr requestWhenInUseAuthorization];
}
This code won't run on iOS 7 and below - according the docs, requestWhenInUseAuthorization is only available on iOS 8:
Availability:
Available in iOS 8.0 and later.
Here is what I'm using to support the new core location method in iOS 7 with no errors:
SEL requestSelector = NSSelectorFromString(#"requestWhenInUseAuthorization");
if ([locationManager respondsToSelector:requestSelector]) {
[locationManager performSelector:requestSelector withObject:NULL];
}
Similarly, if you're seeing this error in relation to CLLocationManager, check that you are not attempting to use the authorizationStatus property on an iOS version prior to iOS14.
https://developer.apple.com/documentation/corelocation/cllocationmanager/3563952-authorizationstatus