zoukankan      html  css  js  c++  java
  • Bundle extension

    import UIKit

    /// 定义一个结构体类型的错误类型

    struct MyBundleError: Error {

        var errCode: Int = 0

        /// 实现Error协议的localizedDescription只读实例属性

        var localizedDescription: String {

            return "TbundleName和podName不能同时为空 (errCode)"

        }

    }

    extension Bundle {

        class func bundleWithClass(for ctype:AnyClass,forResourceName resouceName: String, suffixType typeName:String) -> Bundle? {

            return Bundle(path: Bundle(for: ctype).path(forResource: resouceName, ofType: typeName) ?? "")

        }

        class func bundle(withBundleName bundleName: String?, podName: String?) throws -> Bundle? {

            var bpName: String?

            if bundleName == nil && podName == nil {

                throw MyBundleError(errCode: -1)

            } else if bundleName == nil {

                bpName = podName

            } else if podName == nil {

                bpName = bundleName

            }

            var bName: String?

            if bpName?.contains(".bundle") == true {

                bName = bpName?.components(separatedBy: ".bundle").first

            }

            //没使用framwork的情况下

            var associateBundleURL = Bundle.main.url(forResource: bName, withExtension: "bundle")

            //使用framework形式

            if associateBundleURL == nil {

                associateBundleURL = Bundle.main.url(forResource: "Frameworks", withExtension: nil)

                associateBundleURL = associateBundleURL?.appendingPathComponent(bpName ?? "")

                associateBundleURL = associateBundleURL?.appendingPathExtension("framework")

                if let tassociateBundleURL = associateBundleURL, let associateBunle = Bundle(url: tassociateBundleURL){

                    associateBundleURL = associateBunle.url(forResource: bundleName, withExtension: "bundle")

                }

            }

            

    //        assert((associateBundleURL != nil), "取不到关联bundle")

            //生产环境直接返回空

            

    //        return (associateBundleURL != nil) ? Bundle(url: associateBundleURL) : nil

            guard let tlassociateBundleURL = associateBundleURL else {

                return nil

            }

            return  Bundle(url: tlassociateBundleURL)

        }

        

    }

    @implementation NSBundle (AssociatedBundle)
    /**
     获取文件所在name,默认情况下podName和bundlename相同,传一个即可
     
     @param bundleName bundle名字,就是在resource_bundles里面的名字
     @param podName pod的名字
     @return bundle
     */
    + (NSBundle *)bundleWithBundleName:(NSString *)bundleName podName:(NSString *)podName{
        if (bundleName == nil && podName == nil) {
            @throw @"bundleName和podName不能同时为空";
        }else if (bundleName == nil ) {
            bundleName = podName;
        }else if (podName == nil) {
            podName = bundleName;
        }
        
        
        if ([bundleName containsString:@".bundle"]) {
            bundleName = [bundleName componentsSeparatedByString:@".bundle"].firstObject;
        }
        //没使用framwork的情况下
        NSURL *associateBundleURL = [[NSBundle mainBundle] URLForResource:bundleName withExtension:@"bundle"];
        //使用framework形式
        if (!associateBundleURL) {
            associateBundleURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
            associateBundleURL = [associateBundleURL URLByAppendingPathComponent:podName];
            associateBundleURL = [associateBundleURL URLByAppendingPathExtension:@"framework"];
            NSBundle *associateBunle = [NSBundle bundleWithURL:associateBundleURL];
            associateBundleURL = [associateBunle URLForResource:bundleName withExtension:@"bundle"];
        }
        
        NSAssert(associateBundleURL, @"取不到关联bundle");
        //生产环境直接返回空
        return associateBundleURL?[NSBundle bundleWithURL:associateBundleURL]:nil;
    }
    @end



    // MARK: 新的获取方式

    #import "NSBundle+TLAssociateBundle.h"

    @implementation NSBundle (TLAssociateBundle)

    + (NSBundle *)bundleWithPathBundleName:(NSString *)bundleName  ResourceName:(NSString *)bundleResourceName{

        // MARK: 新的获取方式

        return [NSBundle bundleWithPath:[[NSBundle bundleForClass:NSClassFromString(bundleName)] pathForResource:bundleResourceName ofType:@"bundle"]];

    }

    + (NSBundle *)bundleWithURLBundleName:(NSString *)bundleName  ResourceName:(NSString *)bundleResourceName {

        NSBundle *bundle = [NSBundle bundleForClass:NSClassFromString(bundleName)];

        NSURL *bundleURL = [bundle URLForResource:bundleResourceName withExtension:@"bundle"];

        return  [self bundleWithURL:bundleURL];

    }

    @end

     

    
    
    NSBundle *bundle = [NSBundle bundleForClass:[MYSomeClass class]];
    NSURL *bundleURL = [bundle URLForResource:bundleNamewithExtension:@"bundle"];
    NSBundle *resourceBundle = [NSBundle bundleWithURL: bundleURL];








  • 相关阅读:
    hdu-2841 Visible Trees---容斥定理
    hdu-4135 Co-prime---容斥定理经典&&求1-m中与n互质的数目
    hdu-1796 How many integers can you find---容斥定理
    hdu-2837 Calculation---指数循环节
    FZU-1759 Super A^B mod C---欧拉降幂&指数循环节
    指数循环节&欧拉降幂
    hdu-3074 Multiply game---线段树+单点更新
    hdu-1792 A New Change Problem---数论&剩余系
    POJ-2429 GCD & LCM Inverse---给出gcd和lcm求原来两个数
    hdu-2685 I won't tell you this is about number theory---gcd和快速幂的性质
  • 原文地址:https://www.cnblogs.com/sundaysme/p/15041114.html
Copyright © 2011-2022 走看看