我想从ViewController提取mp3文件名并将其传递给另一个名为TextViewVc的VC,目前我正在使用此方法,但无法通过此代码获取文件名]
@IBAction func Button1(_ sender: Any) {
opneAudioFile(path: "Chalisa")
}
@IBAction func Button2(_ sender: Any) {
opneAudioFile(path: "well")}
这是我获取文件名的代码
func opneAudioFile(path : String) {
if let audiopath = Bundle.main.path(forResource: path, ofType: "mp3") {
do {
let contents1 = try String(contentsOfFile: audiopath)
print(contents1)
// Now push second ViewController form here with contents.
if let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "TextViewVC") as? TextViewVC {
secondVC.content = contents1
self.navigationController?.pushViewController(secondVC, animated: true)
}
} catch {
// contents could not be loaded
}
} else {
// example.txt not found!
}
}
我在哪里做错了?还是有其他方法来获取mp3的文件名?
String(contentsOfFile:)
不返回文件名,但
let fileName = (audiopath as NSString).lastPathComponent
确实