iOS Bundle的简单使用

class ViewController1: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //获得整个App的Bundle对象
        let mainBundle = Bundle.main
        
        //通过Bundle对象获得这个App的bundle标识: com.linyufeng.segueTest
        print(mainBundle.bundleIdentifier ?? "无法显示bundleIdentifier")
        
        //通过Bundle对象获得这个App的相对路径: /Users/linyufeng/Library/Developer/CoreSimulator/Devices/114FBDEE-A305-46FD-B324-A5C88845BB5D/data/Containers/Bundle/Application/3F857297-396F-4975-B73A-0DE463EA89AB/segueTest.app
        print(mainBundle.bundlePath)
        
        //通过Bundle对象获得这个App的绝对路径: file:///Users/linyufeng/Library/Developer/CoreSimulator/Devices/114FBDEE-A305-46FD-B324-A5C88845BB5D/data/Containers/Bundle/Application/3F857297-396F-4975-B73A-0DE463EA89AB/segueTest.app/
        print(mainBundle.bundleURL)
        
        //通过Bundle对象获得这个App的本地化信息(我在Info.plist设置的是China): zh_CN
        print(mainBundle.developmentLocalization ?? "无法显示developmentLocalization")
        
        //通过Bundle对象获得这个App的可执行文件的相对地址: /Users/linyufeng/Library/Developer/CoreSimulator/Devices/114FBDEE-A305-46FD-B324-A5C88845BB5D/data/Containers/Bundle/Application/3F857297-396F-4975-B73A-0DE463EA89AB/segueTest.app/segueTest
        print(mainBundle.executablePath ?? "无法显示executablePath")
        
        //通过Bundle对象获得Info.plist文件的消息(Info.plist是字典形式存储的,虽然我们在设置Info.plist时是将 Bundle Display Name 设为 网易云,但真正的键名是:CFBundleDisplayName),这里控制台的输出是:网易云
        print(mainBundle.infoDictionary!["CFBundleDisplayName"] ?? "无法显示Info.plist文件信息")
        
        
        //Bundle初始化
        //这里的输出是true,说明使用Bundle.main和使用Bundle的其他初始化函数都能获得Bundle对象,从而获得获得资源的接口
        let bundle1 = Bundle(for: type(of:self))
        let bundle2 = Bundle(identifier: "com.linyufeng.segueTest")
        let bundle3 = Bundle(path: mainBundle.bundlePath)
        let bundle4 = Bundle(url: mainBundle.bundleURL)
        print(mainBundle ==  bundle1 && mainBundle ==  bundle2 && mainBundle ==  bundle3 && mainBundle ==  bundle4)
        
        
    }

 

转载于:https://www.cnblogs.com/xiaotian331/p/6658680.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值