Getting error on clicking button in swift - ios

I am a newbie to iOS development and just started learning basic stuffs in swift. Now I am getting error while i try to run my basic coding that I am practising with Auto Layout. The below exceptions occurs while I try to open a dialog on clicking the button. I am also posting my sample code for your reference
The exception I am getting is as follows:
<NSLayoutConstraint:0x79e84ca0 V:[UILabel:0x79e82b40'sadadsfsadfsd']-(90)-[_UILayoutGuide:0x79e84610]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-10-01 00:04:18.833 FirstDemo[6922:355986] -[FirstDemo.ViewController showSomething]: unrecognized selector sent to instance 0x79e80210
2015-10-01 00:04:18.836 FirstDemo[6922:355986] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FirstDemo.ViewController showSomething]: unrecognized selector sent to instance 0x79e80210'
*** First throw call stack:
(
0 CoreFoundation 0x0025aa94 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01e5ce02 objc_exception_throw + 50
2 CoreFoundation 0x00263de3 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x001a1e3d ___forwarding___ + 1037
4 CoreFoundation 0x001a1a0e _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x01e710b5 -[NSObject performSelector:withObject:withObject:] + 84
6 UIKit 0x00a3dc40 -[UIApplication sendAction:to:from:forEvent:] + 118
7 UIKit 0x00a3dbbf -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
8 UIKit 0x00bd38fc -[UIControl sendAction:to:forEvent:] + 79
9 UIKit 0x00bd3c7c -[UIControl _sendActionsForEvents:withEvent:] + 408
10 UIKit 0x00bd2c87 -[UIControl touchesEnded:withEvent:] + 714
11 UIKit 0x00ab4eb8 -[UIWindow _sendTouchesForEvent:] + 1095
12 UIKit 0x00ab5da4 -[UIWindow sendEvent:] + 1118
13 UIKit 0x00a5e9b3 -[UIApplication sendEvent:] + 266
14 UIKit 0x00a35903 _UIApplicationHandleEventQueue + 7327
15 CoreFoundation 0x00174e7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
16 CoreFoundation 0x0016ab0b __CFRunLoopDoSources0 + 523
17 CoreFoundation 0x00169f28 __CFRunLoopRun + 1032
18 CoreFoundation 0x00169866 CFRunLoopRunSpecific + 470
19 CoreFoundation 0x0016967b CFRunLoopRunInMode + 123
20 GraphicsServices 0x045fa664 GSEventRunModal + 192
21 GraphicsServices 0x045fa4a1 GSEventRun + 104
22 UIKit 0x00a3bcc1 UIApplicationMain + 160
23 FirstDemo 0x000784fc main + 140
24 libdyld.dylib 0x028cda21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
ViewController.swift
import UIKit
class ViewController: UIViewController {
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 showMessage(){
let alertController = UIAlertController(title:"Success",message:"Awesome done",preferredStyle:UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
Please help me. Kindly text me if my question is not clear. Thanks in advance

So in interface builder you likely created an IBAction attached to your button. It looks like it was originally named 'showSomething' and you renamed it to 'showMessage'.
To fix the bug you either can change the name of your method back to 'showSomething', or you'll have to go into interface builder, select your button, and check the outlet connections (the button on the upper right that's a circle with an arrow in it).
You'll see your old outlet connection to showSomething, delete it and reconnect your button to your new function 'showMessage'.

Have you given Connection to your button? If not do like this
Create Your button IBOutlet and define your method otherwise create IBAction like this
#IBAction func showMessage(sender: AnyObject)
{
let alertController = UIAlertController(title:"Success",message:"Awesome done",preferredStyle:UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}

Related

Swift button error on iOS app but not in playground

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.

crash after accessing photo library using UIImagePickerController

I am having troubles with accessing Photo Library when the button is tapped. Just as usual I am asking if it is accessible. Below is my code
#IBAction func photoLibraryButtonTapped(sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.allowsEditing = true
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
And here is what Xcode says:
2016-08-08 23:05:11.209 Auyrma[1799:20237892] -[UIViewController photoLibraryButtonTapped:]: unrecognized selector sent to instance 0x7fd3e9d39ed0
2016-08-08 23:05:11.214 Auyrma[1799:20237892] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController photoLibraryButtonTapped:]: unrecognized selector sent to instance 0x7fd3e9d39ed0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000113fdcd85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000113a50deb objc_exception_throw + 48
2 CoreFoundation 0x0000000113fe5d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000113f2bcfa ___forwarding___ + 970
4 CoreFoundation 0x0000000113f2b8a8 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001124d6a8d -[UIApplication sendAction:to:from:forEvent:] + 92
6 UIKit 0x0000000112649e67 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x000000011264a143 -[UIControl _sendActionsForEvents:withEvent:] + 327
8 UIKit 0x0000000112649263 -[UIControl touchesEnded:withEvent:] + 601
9 UIKit 0x000000011254999f -[UIWindow _sendTouchesForEvent:] + 835
10 UIKit 0x000000011254a6d4 -[UIWindow sendEvent:] + 865
11 UIKit 0x00000001124f5dc6 -[UIApplication sendEvent:] + 263
12 UIKit 0x00000001124cf553 _UIApplicationHandleEventQueue + 6660
13 CoreFoundation 0x0000000113f02301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x0000000113ef822c __CFRunLoopDoSources0 + 556
15 CoreFoundation 0x0000000113ef76e3 __CFRunLoopRun + 867
16 CoreFoundation 0x0000000113ef70f8 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x00000001164bcad2 GSEventRunModal + 161
18 UIKit 0x00000001124d4f09 UIApplicationMain + 171
19 Auyrma 0x000000010e1c2d82 main + 114
20 libdyld.dylib 0x0000000114e8492d start + 1
21 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
1.Please check target of your view controller and storyboard files should be in same target.
2.select your xib and remove your class which is mapped to that storyboard and map it back and press enter once you paste your class your nib name keep same for testing.
3.If this button is loading form custom view to view controller need to check that custom views are added to right target and re-map touch up inside with ibaction again.
This is what I could figure it out by see your conversations.
I have solved my problem this way:
Open up the Utilites
Open the Identity Inspector
Provide the class name again (however it was there already)
It seems like it was a bug of Xcode...

Have multiple buttons have the same function?

I am creating an app where I need a button that generates more buttons. I have my code like this:
func showButtons(array:Array<UIButton>)
{
for(var i:Int = 0; i < array.count; i++)
{
array[i].setImage(UIImage(named: "StartButton.png"), forState: UIControlState.Normal)
array[i].frame = CGRectMake(CGFloat(arc4random_uniform(200)), CGFloat(arc4random_uniform(200)), 100, 100)
array[i].addTarget(self, action: "pressed", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(array[i])
}
}
The pressed button is this:
func pressed(sender:AnyObject?)
{
maybe()
}
But I know that is not the problem because another button, which was not create programmatically and has it's own action function, uses the same function and does not crash.
My crash report is here:
(104.0, 70.0)
2016-02-02 10:34:11.267 Test[5150:1087940] -[Test.ViewController pressed]: unrecognized selector sent to instance 0x7f8a8ac3be90
2016-02-02 10:34:11.283 Test[5150:1087940] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Test.ViewController pressed]: unrecognized selector sent to instance 0x7f8a8ac3be90'
* First throw call stack:
(
0 CoreFoundation 0x0000000101c81e65 exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001039c1deb objc_exception_throw + 48
2 CoreFoundation 0x0000000101c8a48d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000101bd790a ___forwarding_ + 970
4 CoreFoundation 0x0000000101bd74b8 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001024a0194 -[UIApplication sendAction:to:from:forEvent:] + 92
6 UIKit 0x000000010260f6fc -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x000000010260f9c8 -[UIControl _sendActionsForEvents:withEvent:] + 311
8 UIKit 0x000000010260eaf8 -[UIControl touchesEnded:withEvent:] + 601
9 UIKit 0x000000010250f49b -[UIWindow _sendTouchesForEvent:] + 835
10 UIKit 0x00000001025101d0 -[UIWindow sendEvent:] + 865
11 UIKit 0x00000001024beb66 -[UIApplication sendEvent:] + 263
12 UIKit 0x0000000102498d97 _UIApplicationHandleEventQueue + 6844
13 CoreFoundation 0x0000000101bada31 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
14 CoreFoundation 0x0000000101ba395c __CFRunLoopDoSources0 + 556
15 CoreFoundation 0x0000000101ba2e13 __CFRunLoopRun + 867
16 CoreFoundation 0x0000000101ba2828 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x0000000106297ad2 GSEventRunModal + 161
18 UIKit 0x000000010249e610 UIApplicationMain + 171
19 Test 0x0000000101aa134d main + 109
20 libdyld.dylib 0x00000001044ca92d start + 1
21 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I believe my problem is I have all of e buttons attached to the action "pressed" so it sends a crash. Does anyone know a way to fix this/replace this. I have looked at the posts showing how to programmatically make a UIButton in Swift.
Your code has not problem at all with the number of UIButton associated to the same function, you're missing the colon at the end of the selector for the UIBUtton as your function receive a parameter in his signature, so you need to change the following line:
array[i].addTarget(self, action: "pressed:", forControlEvents:.TouchUpInside)
I hope this help you.
thanks to God (or Latter... ) now on Xcode 8 / Swift 3 syntax is cleaner, so no need to add (:_); compiler will find the method.
so the following code works fine:
override func viewDidLoad() {
super.viewDidLoad()
btn.addTarget(self, action: #selector(buttonTapped), for: UIControlEvents.touchUpInside)
}
final func buttonTapped(sender: UIButton){
}
if You try to mess compiler, i.e:
final func buttonTapped(sender: UIButton. fake: int){..
You will be called (as signature is correct..) but the parameter "fake" will get garbage from stack.
Of course:
final func buttonTapped(){..
works and is called. (no param is taken from stack).
By the way I prefer old syntax that required multiple ":" for every param, as per previous code:
"pressed:"

Swift iOS - unwind segue crashing

I'm currently working on a section of my game where the user is taken to a second ViewController once they lose, this is my GameOverViewController.
I've successfully set up the second view controller with an interstitial advert that runs almost instantly once the GameOverViewController has loaded, the replay button is then only active once the interstitial advert has been closed.
My app crashes once the replay button has been pressed, it worked fine before I added the delay so I'm guessing it's something to do with my new code. The replay button is performing a unwind segue (or trying to), would anyone be able to help resolve?
class GameOverViewController: UIViewController {
#IBOutlet weak var button: UIButton!
}
override func viewDidLoad() {
super.viewDidLoad()
self.button.enabled = false
NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "enableButton", userInfo: nil, repeats: false)
//lots of code here to bring up google interstitial advert
}
func enableButton() {
self.button.enabled = true
}
The button is correctly greyed out for three seconds then turns blue, however once clicked the viewController hangs then crashes. The error brings up AppDelegate with the SIGABRT error. This is the extract from the output field...
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Ginger_Cat.GameOverViewController button:]: unrecognized selector sent to instance 0x7fe70739d120'
*** First throw call stack:
(
0 CoreFoundation 0x0000000111b4dc65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000113b96bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000111b550ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000111aab13c ___forwarding___ + 988
4 CoreFoundation 0x0000000111aaacd8 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001128cbd62 -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x00000001129dd50a -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x00000001129dc8d9 -[UIControl touchesEnded:withEvent:] + 522
8 UIKit 0x0000000112918958 -[UIWindow _sendTouchesForEvent:] + 735
9 UIKit 0x0000000112919282 -[UIWindow sendEvent:] + 682
10 UIKit 0x00000001128df541 -[UIApplication sendEvent:] + 246
11 UIKit 0x00000001128eccdc _UIApplicationHandleEventFromQueueEvent + 18265
12 UIKit 0x00000001128c759c _UIApplicationHandleEventQueue + 2066
13 CoreFoundation 0x0000000111a81431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x0000000111a772fd __CFRunLoopDoSources0 + 269
15 CoreFoundation 0x0000000111a76934 __CFRunLoopRun + 868
16 CoreFoundation 0x0000000111a76366 CFRunLoopRunSpecific + 470
17 GraphicsServices 0x00000001151d0a3e GSEventRunModal + 161
18 UIKit 0x00000001128ca8c0 UIApplicationMain + 1282
19 Ginger Cat 0x000000010f9caa37 main + 135
20 libdyld.dylib 0x00000001142f0145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Here is the code in my GameViewController, there is currently no other code relating to the unwind in this ViewController
#IBAction func replay(segue: UIStoryboardSegue) {
}
Any help would be great, thanks.
The stack trace from the exception is telling you that you are calling the method button(_:) on your GameOverViewController, and that method does not exist (known as an "unrecognized selector" from Objective-C parlance).
It is not immediately clear where this is happening in your code, but I'm guessing that since you say the crash happens when you tap the button, there is an unintentional action called button(_:) connected to a touch event on your button in your storyboard. Select your button in the storyboard and choose the connections inspector on the right. Look for an action called button: - that could be the cause of the problem.
As a guess as to how this happened - did your replay(_:) unwind segue used to be called button(_:), and then you renamed it? Renamed methods in code aren't updated automatically in the storyboard, and can be a common source of bugs due to bad connections between the storyboard and code.
I think there is issue with you IBAction. Code below might be helpful.
#IBAction func replay(sender: UIButton) {
self.performSegueWithIdentifier("yourunwindidentifername", sender: self)
}

Error when trying to play audio in iPhone app

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".

Resources