Astro
Astro是一个库,用Swift编写,用于存储常见的实用方法。
目录 通过DocToc生成
需求
- iOS 11+
- Xcode 10.1
安装
CocoaPods (iOS 11+)
要使用Cocoapods将Astro集成到您的Xcode项目中,请在Podfile中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!
pod 'Astro'
如果您不希望使用全部功能,则可以选择其中一种子规范
pod 'Astro/Logging'
pod 'Astro/Security'
pod 'Astro/UI'
模块
日志记录
Log
是一个结构,简化了日志消息的打印。
默认情况下,您将能够记录错误消息。
Log.error("I want to log an error message with \(something)")
但是,如果您想看到更多信息,您可以像这样覆盖日志级别:例如
#if DEBUG
Log.level = .Debug
#else
Log.level = .Silent
#endif
Log.info("I want to log an info message with \(something)")
Log.debug("I want to log a debug message with \(somethingElse)")
Log.warning("I want to log a warning message with \(somethingOtherThanElse)")
只要它符合Logger
协议,您也可以编写自定义的日志记录器。
Log.logger = MyCustomLogger()
安全
KeychainAccess
提供应用程序对设备密钥链存储的访问权限。使用很简单,作为账户的一部分,您可以将在密钥链中放置一个键的字符串(或数据),然后在以后恢复这些值。这使得它成为安全存储特定用户密码或可用于应用程序重用的令牌的好方法。有关可以存储的其他内容的详细信息,请参阅KeychainAccessSpec.swift文件。
// Instantiate the keychain access using a unique account identifier to house your key/values
let keychain = KeychainAccess(account: "[email protected]")
// Store a login token
var loginTokenID = "LoginToken"
let loginTokenValue = "SomeSuperSecretValueAboutACat"
keychain.putString(testKey, value: loginTokenID)
// And pull it back for later use
let loginToken = keychain.getString(loginTokenID)
注意
- 这是一个简单的密钥链存储库,不包含与iCloud或TouchID的任何特定集成
用户界面
UIColor扩展
包括支持十六进制代码的UIColor扩展(例如:#FF0000
)。您现在可以在另一个类扩展中创建您项目色彩板,将所有那些讨厌的颜色放在一起,并用易于理解的名字。
private static let _FF9000 = UIColor(hexString: "#FF9000")!
public static func myApp_BrightOrangeColor() -> UIColor {
return _FF9000
}
在您的应用程序实现中,您可以快速使用这些颜色
let color = UIColor.myApp_BrightOrangeColor()
class AstroViewController: UIViewController {}
let vc = UIStoryboard.main.instantiateView(ofType: AstroViewController.self)
class AstroAnnotationView: MKAnnotationView {
// ...
}
mapView.registerView(ofType: AstroAnnotationView.self)
mapView.dequeueReusableAnnotationView(ofType: AstroAnnotationView.self, for: annotation)
class AstroTableViewCell: UITableViewCell {
// ...
}
class AstroTableViewController: UITableViewController {
// ...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(ofType: AstroTableViewCell.self, for: indexPath)
}
// ...
}
class AstroView: UIView, NibLoadableView {
// ...
}
class AstroViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let astroNibName = AstroView.nibName // "AstroView"
}
}
ReusableCell与NibLoadableView并行使用
如前所述,存在用于
注册方法可以接受只遵循
class BookCell: UICollectionViewCell, NibLoadableView {
// ...
}
class BookListViewController: UIViewController, UICollectionViewDataSource {
@IBOutlet private weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.registerCell(ofType: BookCell.self)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(ofType: BookCell.self, for: indexPath)
return cell
}
}
Utils
Queue
Queue
提供了更美观的界面,以应对将任务调度到不同GCD队列的最常见需求。如果需要更强大的功能,请考虑使用Async。
Queue.Background.execute {
// Do some work...
Queue.Main.executeAfter(delay: 1) {
// Back on main thread
}
}
Change
Change<Value>
封装了同一类型两个值之间的更改
有时您需要旧的和新的属性值以在UI中执行高效或令人愉悦的变化,此类型通过允许您传递单个值来简化这一点。然后可以通过使用change[at: \.value]
索引器获取子更改。
var model: ViewModel {
didSet {
let change = Change(old: oldValue, new: model)
updateUI(with: change)
}
}
func updateUI(with change: Change<Model>) {
title = change.new.title
updateSomeSubview(with: change[at: \.subviewState])
// ...
}
func updateSomeSubview(with change: Change<SubviewState>) {
guard change.isDifferent else { return }
// ...
}
模块管理
随着库的成熟,将有更多类被引入到项目中,我们希望避免它变成一个事物的杂烩。我们打算通过使用pod subspec为这些模块对代码进行功能目录集群化的方式来做到这一点。这样,如果一个项目只需要一个或两个功能,他们可以很容易地抓取这个子集。
所以如果你想在里面添加一些类,考虑一下现有的模块,决定它是属于一个还是应该有一个新的家。如果你不知道,请提问。
最后,如果你被指派帮助维护这个库,你可以查看CocoaPods管理页面了解更多详情。
联系方式
由Robots & Pencils用❤制作(@robotsNpencils)
维护者
许可证
Astro遵循MIT许可证。有关更多信息,请参阅LICENSE文件。