【在 iOS 开发路上的大小事-Day09】将常用的 Function 写成一个 class,让各个档案都能使用

在开发上,常常会有一些 Function 是会在各个档案中使用的,如果每次都要在需要用到这个 Function 的时候都重写一次一样的 Function,实在是有点没效率

所以就来开始吧!

实作!

这边我是打算示范 Alert 提示框跟取得系统时间这两个 Function,因为这两个算是我蛮常用到的

首先先新增一个 Swift 档案

档名就自行取,但记得开头要大写英文字母!!!

新增好之後,里面会是空的,像下面这样,CustomFunc 是我刚刚取的档名

import Foundation

class CustomFunc {
    
}

之所以会选 Alert 跟一般函式来做示范
是因为两者有一点点不一样,一个是 UI 元件,一个是普通 Function

先来示范 Alert 的写法,这边示范的是只有一个关闭按钮的,由於 Alert 是属於 UI 元件,也就是 UIAlertController,所以在最上面要记得引入 UIKit,不然可是无法使用的!

还有一点是如果是一人开发的话,那可能还好,如果是多人开发的话,那建议还是写一下每个参数的说明会比较好一点,不然修 bug 的时候可能会找到怀疑人生?

import Foundation
import UIKit

class CustomFunc {
    /// 提示框
    /// - Parameters:
    ///   - title: 提示框标题
    ///   - message: 提示讯息
    ///   - vc: 要在哪一个 UIViewController 上呈现
    ///   - actionHandler: 按下按钮後要执行的动作,没有的话,就填 nil
    class func customAlert(title: String, message: String, vc: UIViewController, actionHandler: (() -> Void)?) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let closeAction = UIAlertAction(title: "关闭", style: .default) { action in
            actionHandler?()
        }
        alertController.addAction(closeAction)
        vc.present(alertController, animated: true)
    }
}

再来是普通 Function 的写法,这边我是以取得系统时间的 Function 来做示范,其实就跟一般写法差不多

import Foundation
import UIKit

class CustomFunc {
    ...
    
    // MARK: - 取得送出/更新留言的当下时间
    class func getSystemTime() -> String {
        let currectDate = Date()
        let dateFormatter: DateFormatter = DateFormatter()
        dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
        dateFormatter.locale = Locale.ReferenceType.system
        dateFormatter.timeZone = TimeZone.ReferenceType.system
        return dateFormatter.string(from: currectDate)
    }
}

两个合在一起就像下面这样

import Foundation
import UIKit

class CustomFunc {
    /// 提示框
    /// - Parameters:
    ///   - title: 提示框标题
    ///   - message: 提示讯息
    ///   - vc: 要在哪一个 UIViewController 上呈现
    ///   - actionHandler: 按下按钮後要执行的动作,没有的话就填 nil
    class func customAlert(title: String, message: String, vc: UIViewController, actionHandler: (() -> Void)?) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let closeAction = UIAlertAction(title: "关闭", style: .default) { action in
            actionHandler?()
        }
        alertController.addAction(closeAction)
        vc.present(alertController, animated: true)
    }
    
    // MARK: - 取得送出/更新留言的当下时间
    class func getSystemTime() -> String {
        let currectDate = Date()
        let dateFormatter: DateFormatter = DateFormatter()
        dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
        dateFormatter.locale = Locale.ReferenceType.system
        dateFormatter.timeZone = TimeZone.ReferenceType.system
        return dateFormatter.string(from: currectDate)
    }
}

今天分享的东西其实也不是很重要,不会也没关系,只是会了之後,可以提升开发时的效率~


<<:  [Day - 06 ] - Spring Conditional 运用与原理

>>:  [Day06] Vue i18n - Number Formatting (Currency 货币)

Day 24:路由搜查队-route.query

目前我们实现了即时搜寻显示书名相符的书单资料,但是当我切换到其他路由之後又想再回到上一页的搜寻结果,...

冒险村04 - Create PR with default template

04 - Create PR with default template 在 Github 多人开发...

Day_21 : 让 Vite 来开启你的Vue 之 跨元件资料传递 Provide & Inject

Hi Dai Gei Ho~ 我是 Winnie ~ 在今天文章中,我们要来说说 Vue3 Comp...

D-20. 预设更改DBMS 、bundle指令 、Gemfile && Reverse String II && III

开始Rails new前新手可以先做的。 Rails是允许客制生成框架内容的,下方即为指令,会出现很...

[Vue2Leaflet系列二] Leaflet Plugins with Vue

本篇文章请参考 [Vue2Leaflet系列一] 从vue-cli安装到建置地图 之前介绍过Leaf...