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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10246820.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

目录:[Swift]通天遁地Swift

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

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

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

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

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

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

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

  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 }
原文地址:https://www.cnblogs.com/strengthen/p/10246820.html