Surabhya Aryal Surabhya Aryal - 14 days ago 4x
Swift Question

Cannot override prepareForSegue function

import AVFoundation
import UIKit

class RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate{

@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var recordingLabel: UILabel!
@IBOutlet weak var stopRecordingButton: UIButton!

var audioRecorder: AVAudioRecorder!

override func viewDidLoad() {
stopRecordingButton.isEnabled = false

override func didReceiveMemoryWarning() {

@IBAction func recordAudio(_ sender: AnyObject) {
print("The record button was pressed!")
recordingLabel.text = "Recording in Progress"
stopRecordingButton.isEnabled = true
recordButton.isEnabled = false

let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String
let recordingName = "recordedVoice.wav"
let pathArray = [dirPath, recordingName]
let filePath = NSURL.fileURL(withPathComponents: pathArray)

let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord)

try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true

@IBAction func stopRecording(_ sender: AnyObject) {
print("Recording was stopped")
recordingLabel.text = "Tap to Record"
recordButton.isEnabled = true;
stopRecordingButton.isEnabled = false
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setActive(false)

override func viewWillAppear(_ animated: Bool) {

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "stopRecording") {
let playSoundsVC = segue.destination as! PlaySoundsViewController
let recordedAudioURL = sender as! NSURL
playSoundsVC.recordedAudioURL = recordedAudioURL

func audioRecorderDidFinishRecording(recorder: AVAudioRecorder, successfully flag: Bool) {
print("\n Av audio recorder has finished \n")
if flag{
self.performSegue(withIdentifier: "stopRecording", sender: audioRecorder.url)
print("Saving the audio file was unsucessfull")

When I try to override prepareForSegue function it throws an error mentioning the superclass does not have overridden method.


In Swift 3 the signature of the method is

override func prepare(for segue: UIStoryboardSegue, sender: Any?)

A way to figure out yourself is to comment out the entire method and retype the first few characters (prep). Code completion will help you.