【问题标题】:Why don't I have to import Foundation before using NSBundle in Swift?为什么我不必在 Swift 中使用 NSBundle 之前导入 Foundation?
【发布时间】: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


【解决方案1】:

UIKit 导入 Foundation 并且您继承了您公开导入的任何内容。

【讨论】:

    猜你喜欢
    • 2022-11-27
    • 1970-01-01
    • 2013-09-21
    • 2016-02-29
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    相关资源
    最近更新 更多