【发布时间】:2015-03-31 15:21:56
【问题描述】:
在下面的代码中,我使用的是 NSBundle。根据文档(https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/#//apple_ref/occ/clm/NSBundle/bundleWithURL:),需要导入 Foundation 才能使用 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 的文档。多亏了丹的帖子,我才知道这一点。根据 AVFoundation 文档 (developer.apple.com/library/ios/documentation/AVFoundation/…),NSObject 是一个继承自 AVFoundation 的类(除了如 Dan 提到的那样被 UIKit 继承)。在我的原始帖子中,我查看了 NSObject 文档,但没有查看包含 NSObject 的框架。
标签: ios iphone xcode swift avfoundation