1

First of all, I am new to Swift/iOS development and just started learning. I am trying to create a simple app that plays/pauses music. The interface has a play and pause buttons, and slider to control the music volume. I have searched SO and found some similar posts, but haven't found them helpful in my situation.

When I build and start the app in Xcode, I got the errors:

    2015-04-07 23:04:25.403 Music Player[8772:102170] *** Terminating app due 
to uncaught exception 'NSUnknownKeyException', reason: 

'[<Music_Player.ViewController 0x7f991ad3a160> setValue:forUndefinedKey:]: 
this class is not key value coding-compliant for the key play.'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x000000010a45bf35 __exceptionPreprocess + 165
        1   libobjc.A.dylib                     0x000000010bf9fbb7 objc_exception_throw + 45
        2   CoreFoundation                      0x000000010a45bb79 -[NSException raise] + 9
        3   Foundation                          0x000000010a8737b3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
        4   CoreFoundation                      0x000000010a3a5e80 -[NSArray makeObjectsPerformSelector:] + 224
        5   UIKit                               0x000000010afacc7d -[UINib instantiateWithOwner:options:] + 1506

.....

    22  UIKit                               0x000000010ace7420 UIApplicationMain + 1282
    23  Music Player                        0x0000000109f24afe top_level_code + 78
    24  Music Player                        0x0000000109f24b3a main + 42
    25  libdyld.dylib                       0x000000010c779145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

I use the AVAudioPlayer class to instantiate a player for playing and pausing music.

   var player: AVAudioPlayer = AVAudioPlayer()

    @IBAction func play(sender: AnyObject) {
        var audioPath = NSString(string: NSBundle.mainBundle().pathForResource("music", ofType: "mp3")!) //file: music.mp3

        var error: NSError? = nil

        //instantiate the player
        player = AVAudioPlayer(contentsOfURL: NSURL(string: audioPath), error: &error) //error pointer
        player.prepareToPlay()
        player.play()
    }


    @IBAction func pause(sender: AnyObject) {
        player.pause()
    }

I haven't done anything yet with the slider other than the below code:

@IBOutlet var slider: UISlider!
@IBAction func sliderChanged(sender: AnyObject) {
    }

the GUI is attached below: enter image description here

TonyW
  • 18,375
  • 42
  • 110
  • 183
  • Check each and every outlet in your storyboard. Any one from them is pointing to one, which does not exist. – itsji10dra Apr 08 '15 at 05:24
  • possible duplicate of [What does this mean? "'NSUnknownKeyException', reason: ... This class is not key value coding-compliant for the key X"](http://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key) – jtbandes Aug 03 '15 at 06:21

1 Answers1

1
if let resourceUrl = NSBundle.mainBundle().URLForResource("1", withExtension: "mp3") {
        if NSFileManager.defaultManager().fileExistsAtPath(resourceUrl.path!) {

            var error: NSError? = nil

            //instantiate the player
            player = AVAudioPlayer(contentsOfURL: resourceUrl, error: &error)
            player.prepareToPlay()
            player.play()
        }
    }

Update your code like this for avoid crashing if file is not there.

this class is not key value coding-compliant for the key play.

Means If you have a control in your nib (xib file) that is linked to a property (IBOutlet) or method (IBAction) in your view controller, and you have either deleted or renamed the property or method, the runtime can't find it because it has been renamed and therefore crashes.

HERE I created complete working project for you.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165