0
在我的代碼下面,我使用NSBundle。根據文檔(https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/#//apple_ref/occ/clm/NSBundle/bundleWithURL :),您需要導入Foundation以使用NSBundle。爲什麼在Swift中使用NSBundle之前不需要導入Foundation?
但是,在我的代碼下面,我沒有導入基金會,但我能夠使用NSBundle。爲什麼是這樣?
我認爲這可能是因爲我導入AVFoundation和AVFoundation從NSObject繼承;但是,當我閱讀文檔時,AVFoundation未被列爲從NSObject繼承。
import UIKit
import AVFoundation
class PlaySoundsViewController: UIViewController {
var audioPlayer:AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if var filePath = NSBundle.mainBundle().pathForResource("movie_quote", ofType: "mp3") {
var filePathUrl = NSURL.fileURLWithPath(filePath)
audioPlayer = AVAudioPlayer(contentsOfURL: filePathUrl, error: nil)
}else {
println("the file path is empty")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playSlowAudio(sender: UIButton) {
audioPlayer.play()
}
}
我相信我最初誤解了AVFoundation的文檔。感謝Dan的帖子,我明白了這一點。根據AVFoundation文檔(https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVFoundationFramework/index.html#other),NSObject是從AVFoundation繼承的類(除了被UIKit繼承正如Dan所說)。在我原來的文章中,我在查看NSObject文檔,但沒有看到包含NSObject的框架。 – Michael 2015-03-31 16:15:10