I recently started learning Swift with Xcode12.
I got a red and yellow error like the one in the picture below, and I looked it up on the Internet, but I couldn't find a solution.How do I resolve this error?
Red Error:
Cannot use instance member 'bottunPath' with in property initializer; property initializers run before 'self' is available
yellow warning:
'catch' block is unreachable because no errors are brown in 'do' block
source code:
import UIKit
import AVFoundation
classViewController:UIViewController {
override func viewDidLoad(){
super.viewDidLoad()
// Do any additional setup after loading the view.
}
let botunPath=
Bundle.main.bundleURL.appendingPathComponent("BOTTUN.mp3")
var bottunPlayer=AVAudioPlayer(contentsOf:bottunPath, fileTypeHint:nil)
@ IBAction func BOTTUN(_sender:Any){
do{
bottunPlayer=try AVAudioPlayer(contentsOf:bottunPath, fileTypeHint:nil)
bottunPlayer.play()
} catch{
print("Error in BOTTUN")
}
}
}
I looked it up online, but I couldn't find a solution.
In such cases, it would be better to look it up online and write down what information you got and what you tried.Even if you search the error message Cannot use instance member 'bottunPath' with in property initializer; property initializers run before 'self' is available
, you have found many articles that apply to you.Of course, there are many in Japanese.
So, don't worry too much about the latter half of the error message because it's an excuse from the Swift compiler creator who said why you're making such an error.
Cannot use instance member 'bottunPath' with in property initializer
Your code declares two instance properties Another instance member (instance member --instance property or instance method) cannot be used in the initialization expression when declaring instance properties This is Swift's rule as a programming language, so you have to follow it. Specifically, for your code, This is what it looks like if you correct any other incorrect code writing. Rewriting the name of an action method that you've connected to (most introductions don't say much about how to fix mistakes), but you'd better take the plunge as it's something you'll eventually have to learn. Instead of copying it completely, try it as a reference. By the way, the second warning appears to be caused by the first error that some of the sentences are treated as missing (when there is no sentence in the do-catch that uses try).Try to clear up the error first.bottunPath
and bottunPlayer
, of which bottunPlayer
is the initializer (property initializer), AVAudioPlayer (contentOf:bottunPath, fileTypePhint:code), , in the
instance.
bottunPlayer
does not have a meaningful value until the button is pressed, so it should be optional.import UIKit
import AVFoundation
classViewController:UIViewController {
override func viewDidLoad(){
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//Usually use `url(forResource:withExtension:)` to retrieve URLs to in-app resources
// Intentionally use forced unwrap (`!`) to indicate that if you make a mistake, it will crash and let you know.
let buttonUrl = Bundle.main.url (forResource: "BUTTON", withExtension: "mp3")!
// If the appropriate initial value cannot be set, leave it as an optional type, and the initial value automatically nil
var buttonPlayer —AVAudioPlayer?
// Swift does not use names that start with capital letters other than type names
@ IBAction func buttonTapped(_sender:Any){
do{
buttonPlayer=try AVAudioPlayer(contentsOf:buttonUrl,fileTypeHint:nil)
// Since `buttonPlayer` is an optional type, use Optional Chaining
buttonPlayer?play()
} catch{
// If an error is caught, it is difficult to debug without providing information about the error.
print("Error in BUTTON:\(error)")
}
}
}
© 2024 OneMinuteCode. All rights reserved.