swift 获取当前顶层控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var topViewController: UIViewController? {
var resultVC: UIViewController?
resultVC = _topViewController(UIApplication.shared.keyWindow?.rootViewController)
while resultVC?.presentedViewController {
resultVC = _topViewController(resultVC?.presentedViewController)
}
return resultVC
}

func _topViewController(_ vc: UIViewController?) -> UIViewController? {
if (vc is UINavigationController) {
return _topViewController((vc as? UINavigationController)?.topViewController)
} else if (vc is UITabBarController) {
return _topViewController((vc as? UITabBarController)?.selectedViewController)
} else {
return vc
}
return nil
}

Swift全局状态监听小技巧(willSet,didSet)

封装:便于统一管理,方便使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
enum FyAppObserverName:String{
case isLogin = "isLogin" //是否是登录状态
case isVip = "isVip" //是否是Vip
case timeLength = "timeLength" //时长
}

class FyAppStatusManger:NSObject{
static let shared = FyAppStatusManger.init()
var valueChanged:((_ observerName:FyAppObserverName,_ oldValue:Any?,_ newValue:Any?) ->())?

override init(){
super.init()
}

//需要监听变化的值
var isLogin:Bool = false{
didSet{
valueChanged?(.isLogin, oldValue, isLogin)
}
}

var isVip:Bool = false{
didSet{
valueChanged?(.isVip, oldValue, isLogin)
}
}

var timeLength:Int = 0{
didSet{
valueChanged?(.timeLength, oldValue, isLogin)
}
}

}
使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
FyAppStatusManger.shared.valueChanged =  {(observerName,oldValue ,newValue) in
//可以在app需要的地方调用,需求监听的状态可以根据实际情况自由搭配
switch observerName{
case .isLogin:
//登录状态发生改变
break
case .isVip:
//会员状态发生改变
break
default:
break
}
}

FyNetWork

结合RxSwift、Moya、和HandyJSON封装网络请求模板

【该模板已经上传Github】-> 前往Github 获取代码

文件功能

Podfile

1
2
3
//可以根据不同版本,调整库的版本(不同版本可能需要库的版本不同)
pod 'Moya/RxSwift', '~> 12.0.1'
pod 'HandyJSON', '~> 5.0.1'

FyUrls.swift

主要放一些请求Url

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

struct FyUrls {
/// 服务器环境 true: 正服 false: 测服

#if DEBUG
//测试环境
static let service: Bool = false
#else
//正式环境
static let service: Bool = true
#endif

static var domain: String {
// "正服地址" : "测服地址" (这里是网上搜到的开放接口,没有测试地址,两个都写正式地址)
return FyUrls.service ? "https://v1.alapi.cn/" : "https://v1.alapi.cn/"
}


//这里写拼接到域名上的Url
static var searchMusic: String {
return "api/music/search"
}

//.......

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    class func creteImage(bgImage: UIImage, iconImage: UIImage,iconFrame:CGRect,qrCodeImage:UIImage,codeFrame:CGRect,inviteCodeImg:UIImage,frame:CGRect) -> UIImage
{
// 1.开启图片上下文
// UIGraphicsBeginImageContext(bgImage.size)
UIGraphicsBeginImageContextWithOptions(bgImage.size,false,UIScreen.main.scale)
// 2.绘制背景图片
bgImage.draw(in: CGRect(origin: CGPoint.zero, size: bgImage.size))
// 3.绘制头像
iconImage.draw(in: iconFrame)

qrCodeImage.draw(in: codeFrame)

inviteCodeImg.draw(in: frame)
// 4.取出绘制号的图片
let newImage = UIGraphicsGetImageFromCurrentImageContext()
// 5.关闭上下文
UIGraphicsEndImageContext()
// 6.返回合成号的图片
return newImage!
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 //MARK: -传进去字符串,生成二维码图片
class func setupQRCodeImage(_ text: String) -> UIImage {
//创建滤镜
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setDefaults()
//将url加入二维码
filter?.setValue(text.data(using: String.Encoding.utf8), forKey: "inputMessage")
//取出生成的二维码(不清晰)
if let outputImage = filter?.outputImage {
//生成清晰度更好的二维码
let qrCodeImage = Util.setupHighDefinitionUIImage(outputImage, size: 140)
return qrCodeImage
}

return UIImage()
}

//MARK: - 生成高清的UIImage
class func setupHighDefinitionUIImage(_ image: CIImage, size: CGFloat) -> UIImage {
let integral: CGRect = image.extent.integral
let proportion: CGFloat = min(size/integral.width, size/integral.height)

let width = integral.width * proportion
let height = integral.height * proportion
let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceGray()
let bitmapRef = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: 0)!

let context = CIContext(options: nil)
let bitmapImage: CGImage = context.createCGImage(image, from: integral)!

bitmapRef.interpolationQuality = CGInterpolationQuality.none
bitmapRef.scaleBy(x: proportion, y: proportion);
bitmapRef.draw(bitmapImage, in: integral);
let image: CGImage = bitmapRef.makeImage()!
return UIImage(cgImage: image)
}
1
2
3
4
5
6
7
8
9
10
11
    class func getImageFromView(view:UIView) ->UIImage{
// UIGraphicsBeginImageContext(view.bounds.size)
UIGraphicsBeginImageContextWithOptions(view.bounds.size,false,UIScreen.main.scale)
view.layer.render(in: UIGraphicsGetCurrentContext()!)

let image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return image!
}

Swift 简单链表实现

链表中的元素在内存中不是顺序存储的,查找慢,插入、删除只需要对元素指针重新赋值,效率高;数组元素在内存上连续存放,可以通过下标查找元素;插入、删除需要移动大量元素,比较适用于元素很少变化的情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

import UIKit

//链表节点
public class Node{

var value: T //节点存储数据
weak var next: Node? //节点链接到的下一个节点

weak var previous: Node?//节点链接到的上一个节点

init(value: T) {
self.value = value
}
}

//自定义链表
public class NodeList:CustomStringConvertible{
//链表起点节点
private var head:Node?

//链表终点节点
private var tail:Node?

//链表内数据是否为空
public var isEmpty:Bool{
return head == nil
}
//返回第一个节点
public var first:Node?{
return head
}
//返回最后一个节点
public var last:Node?{
return tail
}

//添加一个链接节点数据
public func append(value:T){
let newNode = Node(value: value)
if let tailNode = tail{
newNode.previous = tailNode
tailNode.next = newNode
}else{
head = newNode
}
tail = newNode
}

//删除节点
public func remove(node:Node) -> T{

//保存要删除节点的上一个和下一个节点
let prev = node.previous
let next = node.next

//如果要删处节点的上一节点不为空
if let prev = prev {
//上一个节点链接到的下一个节点,修改为要删除的下一个节点
prev.next = next
}
//如果要删处节点的上一节点为空,说明要删除的是链表起点节点
else {
//把下一个节点改为起点节点
head = next
}

//要删出的上一个节点,链接到上一个节点
next?.previous = prev
if next == nil {
//next为nil说明要删除的是最后一个,把倒数以前倒数第二个,置为最后一个
tail = prev
}

node.previous = nil
node.next = nil
return node.value

}
//删除所有节点数据
public func removeAll() {
head = nil
tail = nil
}

//取对应索引的节点
public func nodeAt(index: Int) -> Node? {
if index >= 0 {
var node = head
var i = index
while node != nil {
if i == 0 { return node }
i -= 1
node = node!.next
}
}
return nil
}

//输出节点数据
public var description: String{
var text = ""
var node = head
while node != nil {
text += "\(node!.value)"

node = node!.next
if node != nil {
text += ","
}
}
return "[" + text + "]"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch:AnyObject in touches {
let t:UITouch = touch as! UITouch
//检测当前触摸的view是否是textField
if t.view == pwdField || t.view == pwdField || t.view == phoneField{
t.view?.becomeFirstResponder()
}else{
phoneField.resignFirstResponder()
pwdField.resignFirstResponder()
verificationField.resignFirstResponder()
self.resignFirstResponder()
}
}

}

通过响应者链获取对应的第一个符合条件的Responsder

传入响应检测的起点Responsder,一般用于UIView中查找响应者链中的控制器

1
2
3
4
5
6
7
func nextResponder(currentView:UIView)->UIViewController{
var vc:UIResponder = currentView
while vc.isKind(of: UIViewController.self) != true {
vc = vc.next!
}
return vc as! UIViewController
}

String->Class

Swift中引入了命名空间的概念,转Class需要拼接命名空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

//这是一个简单的通过字符串类名,转Class然后初始化后跳转的一个小例子
@objc func OnClick(){

//控制器字符串名称
let vcNameString = "OneViewController"
//获取命名空间也就是项目名称
let clsName = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String

//拼接
let className=clsName! + "." + vcNameString

//字符串转Class 需要注意的是这里的`UIViewController`强转必须带上`.Type`,否则转换不成功
let classT = NSClassFromString(className)! as! UIViewController.Type


URLRouter.shared.pushViewController(viewController:classT.init() , animated: true)
}//成功完成跳转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//
// LayoutProperties.swift
// IfeiyvSwift
//
// Created by l on 2019/6/12.
// Copyright © 2019 ifeiyv. All rights reserved.
//

import UIKit
import CommonCrypto

//Mark:------------Layout
struct Screen{

//MARK:屏幕宽度
static var width:CGFloat{
return UIScreen.main.bounds.width
}
//MARK:屏幕高度
static var height:CGFloat{
return UIScreen.main.bounds.height
}
//MARK:分辨率 倍率
static var scale:CGFloat{
return Screen.width/375.0
}
}


extension UIView{

//MARK: 设置或者获取UIView的frame
var fy_frame:CGRect{
set{
self.frame = fy_frame
}
get{
return self.frame
}
}

//MARK: 设置或者获取UIView的origin
var fy_origin:CGPoint{
set{
self.frame = CGRect(x: fy_origin.x, y: fy_origin.y, width: self.fy_width, height: self.fy_height)
}
get{
return self.frame.origin
}
}

//MARK: 设置或者获取UIView的size
var fy_size:CGSize{
set{
self.frame = CGRect(x: self.fy_origin.x, y: self.fy_origin.y, width: fy_size.width, height: fy_size.height)
}
get{
return self.frame.size
}
}


//MARK: 设置或者获取UIView的宽度
var fy_width:CGFloat{
set{
self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: fy_width, height: self.frame.size.height)
}
get{
return self.bounds.size.width
}
}

//MARK: 设置或者获取UIView的宽度
var fy_height:CGFloat{
set{
self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: fy_height)
}
get{
return self.bounds.size.height
}
}


//MARK: 设置或者获取UIView的x坐标
var fy_x:CGFloat{
set{
self.frame = CGRect(x: fy_x, y: self.frame.origin.y, width: self.frame.size.width, height: self.frame.size.height)
}
get{
return self.frame.origin.x
}
}

//MARK: 设置或者获取UIView的y坐标
var fy_y:CGFloat{
set{
self.frame = CGRect(x: self.frame.origin.x, y: fy_y, width: self.frame.size.width, height:self.frame.size.height)
}
get{
return self.frame.origin.y
}
}


//MARK: 设置或者获取UIView的maxX坐标
var fy_maxX:CGFloat{
get{
return self.fy_x+self.fy_width
}
}

//MARK: 设置或者获取UIView的maxY坐标
var fy_maxY:CGFloat{
get{
return self.fy_y+self.fy_height
}
}

}

extension UIView{
//MARK: 切圆角
func corner(byRoundingCorners corners: UIRectCorner, radii: CGFloat) {
let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
}
}




enum FYError: Swift.Error {
case NOFINT
}

extension UINib{
//MARK: 加载XIB
public class func readNibView(str: String) throws -> UIView {
let nib = UINib.init(nibName: str, bundle: nil)
if let view = nib.instantiate(withOwner: nil, options: nil).first as? UIView {
return view
}
throw FYError.NOFINT
}
}


//Mark:------------UIColor
extension UIColor{

func fy_RGB(red:CGFloat,green:CGFloat,blue:CGFloat) -> UIColor {
return fy_RGBA(red: red, green: green, blue: blue, alpha: 1.0)
}
func fy_RGBA(red:CGFloat,green:CGFloat,blue:CGFloat,alpha:CGFloat) -> UIColor {
return UIColor.init(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha)
}

func fy_hexColor(hexColor:String,defaultStringColor:String = "000000",alpha:CGFloat = 1.0) -> UIColor {
var cString: String = hexColor.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)

if cString.count < 6 {
cString = defaultStringColor
}
if cString.hasPrefix("0X") || cString.hasPrefix("0x") {
cString = String(cString.suffix(from: cString.index(cString.startIndex, offsetBy: 2)))
}
if cString.hasPrefix("#") {
cString = String(cString.suffix(from: cString.index(cString.startIndex, offsetBy: 1)))
}
if cString.count != 6 {
cString = defaultStringColor
}

var range: NSRange = NSMakeRange(0, 2)
let rString = (cString as NSString).substring(with: range)
range.location = 2
let gString = (cString as NSString).substring(with: range)
range.location = 4
let bString = (cString as NSString).substring(with: range)

var r: UInt32 = 0x0
var g: UInt32 = 0x0
var b: UInt32 = 0x0
Scanner(string: rString).scanHexInt32(&r)
Scanner(string: gString).scanHexInt32(&g)
Scanner(string: bString).scanHexInt32(&b)
return fy_RGBA(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: alpha)
}

}

//Mark:------------String
extension String{

static let EMPTY = ""
/// String使用下标截取字符串
/// 例: "示例字符串"[0..<2] 结果是 "示例"
subscript (r: Range<Int>) -> String {
get {
let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound)
let endIndex = self.index(self.startIndex, offsetBy: r.upperBound)

return String(self[startIndex..<endIndex])
}
}

//MARK: 返回一个字符串的前n位, 若字符串长不足n,则直接返回当前字符串
func fy_start(length n: Int) -> String {
if self.count < n {
return self
}
if n < 0 {
return self
}
let index = self.index(self.startIndex, offsetBy: n)
return String(self[..<index])
}


//MARK: 返回一个字符串的后n位, 若字符串长不足n,则直接返回当前字符串
func fy_last(length n: Int) -> String {
if self.count < n {
return self
}
let index = self.index((self.endIndex), offsetBy: -n)
return String(self[index...])
}

//MARK: 删除所有的空格
func fy_trimAll() -> String {
return self.replacingOccurrences(of: " ", with: String.EMPTY)
}


//MARK: 判断是否包含数字和字母
func fy_isPassWord() -> Bool {
let numberRegex:NSPredicate = NSPredicate(format: "SELF MATCHES %@", "^.*[0-9]+.*$")

let letterRegex:NSPredicate = NSPredicate(format: "SELF MATCHES %@", "^.*[A-Za-z]+.*$")

if numberRegex.evaluate(with: self) && letterRegex.evaluate(with: self){
return true
}
return false
}


//MARK: md5加密
var fy_md5: String {
let cStr = self.cString(using: .utf8)
let digestLen = Int(CC_MD5_DIGEST_LENGTH)
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: digestLen)
CC_MD5(cStr!,(CC_LONG)(strlen(cStr!)), buffer)

let md5String = NSMutableString()
for i in 0 ..< digestLen {
md5String.appendFormat("%02X", buffer[i])
}
free(buffer)
return md5String as String
}

//MARK: base64编码
var fy_base64Encoding: String {
let data = self.data(using: String.Encoding.utf8)
var base64String = data?.base64EncodedString(options: .lineLength76Characters)
base64String = base64String?.replacingOccurrences(of: "\r", with: "")
return base64String ?? ""
}

//MARK: base64解码
var fy_base64Decoded: String {

let decodedData = Data(base64Encoded: self, options: Data.Base64DecodingOptions(rawValue: 0))
return String(data: decodedData!, encoding: String.Encoding.utf8) ?? ""
}

//MARK: 返回188****8888类型字符串
func fy_getSecretMobileNo() -> String {
if self.isEmpty { return self }
if self.count > 7 {
return "\(self.fy_start(length: 3))****\(self.fy_last(length:4))"
}else{
return self;
}
}


//MARK: 截取从start开始,长度为lenght的字符串
///
/// - Parameters:
/// - start: 开始
/// - lenght: 长度
/// - Returns: 处理结果
func fy_startIndexToLenght(_ start: Int, lenght: Int) -> String {
if start > self.count {
return self
}
let startIndex = self.index(self.startIndex, offsetBy: start)
if self.count < (start + lenght) {
return String(self.suffix(from: startIndex))
}
let endIndex = self.index(self.startIndex, offsetBy: start + lenght)
return String(self[startIndex..<endIndex])
}

//MARK: 判断链接中是否存在中文
func fy_isIncludeChineseIn() -> Bool {
for(_, value) in self.enumerated() {
if value >= "\u{4E00}" && value <= "\u{9FA5}" {
return true
}
}
return false
}

//MARK: 判断是否有效邮箱
///
/// - Returns: 结果
func fy_isPeriodEmail() -> Bool {
let email = "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
let regextestEmails = NSPredicate(format: "SELF MATCHES %@", email)

return regextestEmails.evaluate(with: self)
}

//MARK: 判断是否有效身份证号
///
/// - Returns: Bool
func fy_isPeriodIdCard() -> Bool {

if self.count != 18 {
return false
}

let regex2 = "^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$"
let identityCardPredicate = NSPredicate(format: "SELF MATCHES %@", regex2)
let flag = identityCardPredicate.evaluate(with: self)
if !flag {
// 格式错误
return flag
} else {
// 格式正确,判断合法性
// 将前十七位加权因子保存在数组里
let idCardWiArray = ["7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"]
// 这是除以11后,可能产生的11位余数, 验证码,也保存在数组里
let idCardYArray = ["1", "0", "10", "9", "8", "7", "6", "5", "4", "3", "2"]
var idCardArr = [Int]()
var idCardWiSum = 0
for index in 0..<17 {
let subStrIndex = fy_startIndexToLenght(index, lenght: 1)
let idCardWiIndex = idCardWiArray[index]
// 数组保存所有乘积
idCardArr.append(Int(subStrIndex)! * Int(idCardWiIndex)!)
}

// 将所有乘积相加
for index in 0..<idCardArr.count {
idCardWiSum += idCardArr[index]
}

// 计算出校验码所在数组的位置
let idCardMod = idCardWiSum % 11
// 得到最后一位身份证号码
let idCardLast = fy_startIndexToLenght(17, lenght: 1)
if idCardMod == 2 {
if idCardLast == "X" || idCardLast == "x" {
return true
} else {
return false
}
} else {
if idCardLast == idCardYArray[idCardMod] {
return true
} else {
return false
}
}
}
}

//MARK: 正则匹配6-12位数字和大小写字母组合
///
/// - Returns: Bool
func fy_checkPassword() -> Bool {
let password = "^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,12}"
let regextestPassword = NSPredicate(format: "SELF MATCHES %@", password)
return regextestPassword.evaluate(with:self)
}

//MARK: 判断字符串是否包含其他字符串(必须全部一样)
func fy_contains(find: String) -> Bool{
return self.range(of: find) != nil
}

//MARK: 字符串转换成整型
func fy_toInt() -> Int? {
return Int(self)
}

//MARK: 识别有效手机号
func fy_verifyPhoneNumber() -> Bool {
let mobile = "^1((3[0-9]|4[5-68-9]|5[0-35-9]|6[6]|7[0-9]|8[0-9]|9[89])\\d{8}$)"
let regextestmobile = NSPredicate(format: "SELF MATCHES %@",mobile)
return regextestmobile.evaluate(with: self)
}

//MARK: 识别是否有效座机号 eg: 021-8688995
func fy_landlineNumber() -> Bool {
let strNum = "^(0\\d{2,3}-?\\d{7,8}$)"
let checktest = NSPredicate.init(format: "SELF MATCHES %@", strNum)
let isPhone = checktest.evaluate(with: self)

let strNum1 = "^(\\d{7,8}$)"
let checktest1 = NSPredicate.init(format: "SELF MATCHES %@", strNum1)
let isPhone1 = checktest1.evaluate(with: self)

if isPhone || isPhone1 {
return true
} else {
return false
}
}


}





extension Array{
//MARK: 数组转JSON字符串
func arrayToString() -> String? {
if let data = try? JSONSerialization.data(withJSONObject: self, options: JSONSerialization.WritingOptions.init(rawValue: 0)) {
if let jsonStr = String.init(data: data, encoding: String.Encoding.utf8) {
return jsonStr
}
}
return nil
}
}