Swift学习

swift-基础-进阶2

2016-08-15  本文已影响16人  埃林的奶酪

分类

extension String

{

    // MARK: - 沙盒路径

    func cachesDir() -> String {

        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!

        

        let lastName = (self as NSString).lastPathComponent

        

        let filePath = (path as NSString).stringByAppendingPathComponent(lastName)

        return filePath

    }

    

    func documentDir() -> String {

        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!

        

        let lastName = (self as NSString).lastPathComponent

        

        let filePath = (path as NSString).stringByAppendingPathComponent(lastName)

        return filePath

    }

    

    func tmpDir() -> String {

        let path = NSTemporaryDirectory()

        

        let lastName = (self as NSString).lastPathComponent

        

        let filePath = (path as NSString).stringByAppendingPathComponent(lastName)        

        return filePath

    }

}

命名空间

let aClass : AnyClass = NSClassFromString("XMGWeibo." + "HomeViewController")!
        // 从info.plist动态获取命名空间

       // 这里通过key去字典取值,取到的是anyObject类型的,是可选类型。要对其操作必须解包。这里用guard来解包
        guard let name = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] else

        {

            KSLog("name为空")

            return

        }

        // 拼接命名空间.类名
        // NSClassFromString返回的类型是AnyClass?

        let aClass: AnyClass? = NSClassFromString("\(name)."+controllerName)

Swift中的类

回到上面的命名空间问题

// 根据完整类名创建控制器,这里aClass要强转成UIViewController类型的才能用init方法
// aClass是可选类型,这里用guard来解包
guard let classType = aClass as? UIViewController.Type else{

    return

}

let childViewController = classType.init()

异常

// filePath是根据一个key来取值的,可能不存在,是可选类型,需要解包

        guard let filePath = NSBundle.mainBundle().pathForResource("MainVCSetting.json", ofType: nil) else

        {

            KSLog("JSON文件不存在")

            return

        }

        // data也是可选类型。如果不用guard处理,用alt点击可以看到有个问号。(用guard处理了就看不到了)

        guard let data = NSData(contentsOfFile: filePath) else

        {

            KSLog("加载二进制数据失败")

            return

        }

       
        do

        { // json转对象,顺便强转为字典数组类型

            let obj = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [[String: AnyObject]]

            

            for dict in obj {
                // 上面的字典里value是AnyObject类型,需要强转为String

                let title = dict["title"] as? String

                let VcName = dict["vcName"] as? String

                let imageName = dict["imageName"] as? String

                addChildViewController(VcName, title: title, imageName: imageName)

            }

        }catch

        {

            KSLog("接受服务器控制器数据失败,显示默认控制器")

            addChildViewController("HomeController", title: "首页", imageName: "tabbar_home")

            addChildViewController("MessageController", title: "消息", imageName: "tabbar_message_center")

            addChildViewController("DiscoverController", title: "发现", imageName: "tabbar_discover")

            addChildViewController("ProfileController", title: "我", imageName: "tabbar_profile")

        }

        

        // 这里的obj是个可选类型,如果出错,那么是nil

        let obj1 = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)

        // 这里如果出错,那么直接崩

        let obj2 = try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)

断言

assert(access_token != nil, "使用loadUserInfo()方法前必须先授权")
上一篇下一篇

猜你喜欢

热点阅读