How to Resolve Argument labels (contetsOF, fileTypeHint) do not match any available overloads

Asked 2 years ago, Updated 2 years ago, 33 views

I'm a beginner at Swift.
I'm trying to make an app that makes sound, but I'm stuck in the error below, so I can't proceed.
Argument labels (contentsOF, fileTypeHint) do not match any available overloads

I would appreciate it if you could tell me how to solve it.
The code is as follows:

//
//  ViewController.swift
//  MyMusic
//
//  Created by name on November 5, 2016.
//  Copyright © 2016 mycompany. All rights reserved.
//

import UIKit
import AVFoundation

classViewController:UIViewController {

  override func viewDidLoad(){
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from anib.
  }

  override funcdidReceiveMemoryWarning(){
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  // cymbal sound source file
  var cymbalPath=Bundle.main.bundleURL.appendingPathComponent("cymbal.mp3")

  // Create a player instance for cymbals
  varcymbalPlayer=AVAudioPlayer()

  @ IBAction function(_sender:Any){
    do{
    // Specify the name of the sound source file for cymbals
    cymbalPlayer=try AVAudioPlayer(contetsOF:cymbalPath, fileTypeHint:nil)
    cymbalPlayer.play()
    } catch{
        print("A cymbal encountered an error.")
    }

  }

}

swift

2022-09-30 17:57

1 Answers

Basically, it's a simple spelling mistake.Make sure you know what you really want to do, and then check with official references.

AVAudioPlayer

 init(contentsOf:URL, fileTypeHint:String?)

Initializes and returns an audio player using the specified URL and
file type hint.

contentsOf:, not contentsOf:.(Note case differences.)

If possible, why don't you take this opportunity to change the writing style you don't recommend?

class ViewController:UIViewController {

    //...

    // cymbal sound source file
    // Use `url(forResource: withExtension:)` instead of creating a path from bundleURL when accessing resource files in Bundle
    // It is often better not to use the "Crash My App" operator (`!`), but here I intentionally use it to notify you of a crash if the app is not configured properly.
    var cymbalPath=Bundle.main.url (forResource: "cymbal", withExtension: "mp3")!

    // Declare player properties for cymbals
    //`= The declaration AVAudioPlayer()` has simultaneously generated an unused instance.
    varcymbalPlayer —AVAudioPlayer?

    @ IBAction function(_sender:Any){
        do{
            // Create a player instance for cymbals
            // ContentsOf instead of contentsOF
            // If you don't want to give a hint, you should use `init(contentsOf:)`
            cymbalPlayer=try AVAudioPlayer(contentsOf:cymbalPath)
            cymbalPlayer ?.play()
        } catch{
            print("A cymbal encountered an error.")
        }
    }
}


2022-09-30 17:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.