lucambd lucambd - 1 year ago 248
Swift Question

Expression resolves to an unused function

First of all let me say that I am very new to programming. What I'm trying to do is add a button that when pressed plays music, and when pressed again the music stops. Ideally when the button is pressed for a third time the music will have reset. Whilst trying to achieve this I'm getting the error message "Expression resolves to an unused function", as I am very new all the help I find online doesn't make any sense to me.

import UIKit
import AVFoundation

class ViewController: UIViewController {
@IBOutlet weak var janitor: UIImageView!
var pianoSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("C", ofType: "m4a")!)
var audioPlayer = AVAudioPlayer()

override func viewDidLoad() {

audioPlayer = AVAudioPlayer(contentsOfURL: pianoSound, error: nil)

override func didReceiveMemoryWarning() {

@IBAction func PianoC(sender: AnyObject) {

if audioPlayer.playing { audioPlayer.stop} else {}

Answer Source

Swooping in here after Martin R's comment ...

if audioPlayer.playing { audioPlayer.stop} else {}

On this line, you are not calling the stop and play functions, but simply accessing them. Resolving to an unused function is trying to tell you that you have an expression that is returning a function type, but you are never calling it (audioPlayer.stop and are the expressions in question here).

To rid yourself of this error, and probably produce the correct behavior, try calling the functions.

if audioPlayer.playing { 
} else {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download