[Swift通天遁地]5、高级扩展-(9)颜色、设备、UserDefaults、URL等扩展方法

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-twhnhoep-kz.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

目录:[Swift]通天遁地Swiftios

本文将演示颜色、设备、UserDefaults、URL等扩展方法。git

首先确保在项目中已经安装了所需的第三方库。github

点击【Podfile】,查看安装配置文件。web

1 platform :ios, '12.0'
2 use_frameworks!
3 
4 target 'DemoApp' do
5     source 'https://github.com/CocoaPods/Specs.git'
6     pod 'EZSwiftExtensions'
7 end

根据配置文件中的相关配置,安装第三方库。ajax

而后点击打开【DemoApp.xcworkspace】项目文件。swift

在项目导航区,打开视图控制器的代码文件【ViewController.swift】api

  1 import UIKit
  2 //在当前的类文件中,引入已经安装的第三方类库
  3 import EZSwiftExtensions
  4 
  5 class ViewController: UIViewController {
  6 
  7     override func viewDidLoad() {
  8         super.viewDidLoad()
  9         // Do any additional setup after loading the view, typically from a nib.
 10         //颜色的扩展
 11         uiColorExtensions()
 12         //设备类型的扩展方法
 13         uiDeviceExtensions()
 14         //轻量级本地数据存储类型的扩展
 15         userDefaultsExtensions()
 16         //URL网址类型的扩展方法
 17         urLExtensions()
 18     }
 19     
 20     //添加一个方法,用于颜色的扩展
 21     func uiColorExtensions()
 22     {
 23         //第三方类库对颜色类的初始化方法进行了优化,
 24         //使开发者能够根据红绿蓝三原色和透明度等信息,建立所需的颜色。
 25         _ = UIColor(r: 100, g: 100, b: 100)
 26         _  = UIColor(r: 100, g: 100, b: 100, a: 0.5)
 27         
 28         //一样能够经过初始化方法,建立不一样透明度的灰阶颜色
 29         _  = UIColor.init(gray: 100)
 30         _  = UIColor.init(gray: 100, alpha: 0.5)
 31         
 32         //初始化一个默认值为洋红的颜色对象
 33         let myColor5 = UIColor.magenta
 34         //经过对颜色类型扩展的属性,
 35         //能够快速获取颜色对象的四个通道的值
 36         print("myColor5.redComponent:\(myColor5.redComponent)")
 37         print("myColor5.greenComponent:\(myColor5.greenComponent)")
 38         print("myColor5.blueComponent:\(myColor5.blueComponent)")
 39         print("myColor5.alpha:\(myColor5.alpha)")
 40         
 41         //经过初始化方法,能够经过十六进制的字符串,建立所需的颜色,
 42         //而且制定颜色的不透明度。
 43         _  = UIColor(hexString: "0x233C64")
 44         _  = UIColor(hexString: "0x233C64", alpha: 0.6)
 45         //甚至能够去掉前方的十六进制的标志符号
 46         _  = UIColor(hexString: "233C64")
 47         //或者使用常见的#号标志
 48         _  = UIColor(hexString: "#233C64")
 49         
 50         //经过颜色对象的随机颜色的方法,能够得到一个随机的颜色
 51         _  = UIColor.randomColor()
 52     }
 53     
 54     //添加一个方法,设备类型的扩展方法
 55     func uiDeviceExtensions()
 56     {
 57         //经过设备类的扩展方法,
 58         //得到供应商的惟一标志符
 59         print("UIDevice.idForVendor():\(String(describing: UIDevice.idForVendor()))")
 60         //得到并输出设备的系统名称
 61         print("UIDevice.systemName():\(UIDevice.systemName())")
 62         //得到并输出设备的系统版本
 63         print("UIDevice.systemVersion():\(UIDevice.systemVersion())")
 64         //得到并输出设备的名称
 65         print("UIDevice.deviceName():\(UIDevice.deviceName())")
 66         //得到并输出设备的型号
 67         print("UIDevice.deviceModel():\(UIDevice.deviceModel())")
 68         //得到并输出设备的的型号是否可被获取
 69         print("UIDevice.deviceModelReadable():\(UIDevice.deviceModelReadable())")
 70         //得到并输出设备的语言
 71         print("UIDevice.deviceLanguage():\(UIDevice.deviceLanguage())")
 72         
 73         //检测系统的版本号是否在12.0版本之上
 74         print("UIDevice.isSystemVersionOver(12.0):\(UIDevice.isSystemVersionOver("12.0"))")
 75     }
 76     
 77     //添加一个方法,针对轻量级本地数据存储类型的扩展
 78     func userDefaultsExtensions()
 79     {
 80         //得到本地数据存储对象
 81         let Defaults = UserDefaults.standard
 82         //像使用字典同样,依次设置两个键的值
 83         Defaults["userName"] = "Jerry"
 84         Defaults["age"] = 35
 85         
 86         //获取本地存储的值
 87         let userName = Defaults["userName"]
 88         let age = Defaults["age"]
 89         
 90         //在控制台输出相应的数据
 91         print("userName:\(String(describing: userName))")
 92         print("age:\(String(describing: age))")
 93     }
 94     
 95     //添加一个方法,针对URL网址类型的扩展方法
 96     func urLExtensions()
 97     {
 98         //初始化一个网址对象
 99         let url = URL(string: "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=facebook")
100         //经过扩展属性,快速得到网址中的请求参数
101         if let queryParameters = url?.queryParameters
102         {
103             //经过键值查询的方式,获取并输出参数的值
104             print("queryParameters[v]:\(String(describing: queryParameters["v"]))")
105             print("queryParameters[q]:\(String(describing: queryParameters["q"]))")
106             print("queryParameters[other]:\(String(describing: queryParameters["other"]))")
107         }
108     }
109 
110     override func didReceiveMemoryWarning() {
111         super.didReceiveMemoryWarning()
112         // Dispose of any resources that can be recreated.
113     }
114 }
相关文章
相关标签/搜索