[Swift通天遁地]五、高级扩展-(2)扩展集合类型

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

目录:[Swift]通天遁地Swift

本文将演示扩展集合类型实现对集合中数据的多种运算。

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

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

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

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

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

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

现在开始编写代码,快速检测设备的各种属性。

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         // Do any additional setup after loading the view, typically from a nib.
 8         
 9         //数组求和
10         print("sum:([1, 2, 3, 4].sum)")
11         //数组求积
12         print("product:([1, 2, 3, 4].product)")
13         //字符串组成的数组,进行求和运算,
14         //返回拼合而成的字符串
15         print("sum:(["a","b","c","d"].sum)")
16         //字符串数组组成的数组,进行求和运算,
17         //返回拼合而成的字符串数组
18         print("sum:([["a","b"],["c"],["d"]].sum)")
19         //求平均值
20         print("average:([1, 2, 3, 4].average)")
21         
22         print("---------------------")
23         //中间值
24         print("median:([1, 11, 19, 4, -7].median)")
25         //有两个中间值时,取较小的一个
26         print("medianLow:([1.0, 11, 19.5, 4, 12, -7].medianLow)")
27         //有两个中间值时,取较大的一个
28         print("medianHigh:([1, 11, 19, 4, 12, -7].medianHigh)")
29         //取样本方差
30         print("varianceSample:([1.0, 11, 19.5, 4, 12, -7].varianceSample)")
31         //取总体方差
32         print("variancePopulation:([1.0, 11, 19.5, 4, 12, -7].variancePopulation)")
33         //标准样本偏差
34         print("standardDeviationSample:([1.0, 11, 19.5, 4, 12, -7].standardDeviationSample)")
35         //标准总体偏差
36         print("standardDeviationPopulation:([1.0, 11, 19.5, 4, 12, -7].standardDeviationPopulation)")
37         
38         print("---------------------")
39         //倾斜度
40         print("skewness:([1.0, 11, 19.5, 4, 12, -7].skewness)")
41         //峰度
42         print("kurtosis:([1.0, 11, 19.5, 4, 12, -7].kurtosis)")
43         //样本协方差
44         print("covarianceSample:([1, 2, 3.5, 3.7, 8, 12].covarianceSample([0.5, 1, 2.1, 3.4, 3.4, 4]))")
45         //总体协方差
46         print("covariancePopulation:([1, 2, 3.5, 3.7, 8, 12].covariancePopulation([0.5, 1, 2.1, 3.4, 3.4, 4]))")
47         //皮尔逊积差相关系数
48         print("pearson:([1, 2, 3.5, 3.7, 8, 12].pearson([0.5, 1, 2.1, 3.4, 3.4, 4]))")
49     }
50 
51     override func didReceiveMemoryWarning() {
52         super.didReceiveMemoryWarning()
53         // Dispose of any resources that can be recreated.
54     }
55 }
原文地址:https://www.cnblogs.com/strengthen/p/10231366.html