Stella 包含一组实用工具,可在 Swift 中进行 iOS 开发期间使用。
您可以使用 Swift Package Manager 安装 Stella
。从 Xcode 11 开始提供。只需搜索 icapps/ios-stella
并安装它。
Stella 可通过 CocoaPods 获得。 要安装它,只需将以下行添加到您的 Podfile
中
pod 'Stella', git: 'https://github.com/icapps/ios-stella.git', commit: '...'
传递正确的提交引用,以确保您的代码在未来的更新中不会中断。
要使用 Carthage 将 Stella 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它"
github "icapps/ios-stella" ~> 1.5.1
然后,运行以下命令来构建 Stella 框架
carthage update
执行额外的步骤,以便使用此框架设置 Carthage:Carthage 的 README
我们有一种更简洁的使用 NSUserDefaults
的方法。 通过扩展 DefaultsKeys
类来定义用户默认值。
extension DefaultsKeys {
// Writes a string object to the defaults with the 'stringValue' key.
static let stringValue = DefaultsKey<String?>("stringValue")
// Writes an integer to the defaults with the 'integerValue' key.
static let integerValue = DefaultsKey<Int?>("integerValue")
// Writes a double to the defaults with the 'doubleValue' key.
static let doubleValue = DefaultsKey<Double?>("doubleValue")
// Writes a float to the defaults with the 'floatValue' key.
static let floatValue = DefaultsKey<Float?>("floatValue")
// Writes a bool to the defaults with the 'booleanValue' key.
static let booleanValue = DefaultsKey<Bool?>("booleanValue")
// Writes a date object to the defaults with the 'dateValue' key.
static let dateValue = DefaultsKey<NSDate?>("dateValue")
// Writes a dictionary object to the defaults with the 'dictValue' key.
static let dictValue = DefaultsKey<[String: Any]?>("dictValue")
}
您可以使用 Defaults
类上的 subscript
从/向 NSUserDefaults
读取/写入。
Defaults[.stringValue] = "A string value"
print(Defaults[.stringValue]) // Prints 'A string value'
Defaults[.integerValue] = 123
print(Defaults[.integerValue]) // Prints '123'
Defaults[.doubleValue] = 123.123
print(Defaults[.doubleValue]) // Prints '123.123'
Defaults[.floatValue] = 123.321
print(Defaults[.floatValue]) // Prints '123.312'
Defaults[.booleanValue] = true
print(Defaults[.booleanValue]) // Prints 'true'
Defaults[.dateValue] = NSDate()
print(Defaults[.dateValue]) // Prints '1996-12-19T16:39:57-08:00'
Defaults[.dictValue] = ["SomeKey" : "SomeValue"]
let value = Defaults[.dictValue]
print(value["SomeKey"]) // Prints 'SomeValue'
我们有一种更简洁的使用 Keychain
的方法。 通过扩展 Keys
类来定义用户默认值。
extension Keys {
// Writes a string object to the keychain with the 'stringValue' key.
static let stringValue = Key<String?>("stringValue")
}
您可以使用 Keychain
类上的 subscript
从/向 Keychain
读取/写入。 它支持常规 String
值以及 Codable
对象。
Keychain[.stringValue] = "A string value"
print(Keychain[.stringValue]) // Prints 'A string value'
```swift
Keychain[.codableValue] = ClassConformingToCodable()
print(Keychain[.codableValue]) // Prints '<ClassConformingToCodable: 0x0123456789>'
In some cases you want to be able to set additional keychain query paramaters on an item.
```swift
static let noBackupValue = Key<String?>("noBackup", {
return [kSecAttrAccessible as String: kSecAttrAccessibleAlwaysThisDeviceOnly]
}())
使用这个方便的本地化函数在短时间内本地化一个键。
let key = "this_is_your_localization_key"
print(key.localizedString)
// The debug console will print the localized
// string found in your .strings file.
从可能超出范围的数组中获取元素。
let array = [1, 2, 3, 4]
array[safe: 2] // Returns 3
arra[safe: 10] // Returns nil
返回从前端/后端开始的有限子集的数组。
let array = [1, 2, 3, 4]
array.truncate(by: 2) // Returns [1, 2]
array.reverseTruncate(by: 2) // Returns [3, 4]
返回是否在数组中找到元素。
let array = [1, 2, 3, 4]
array.contains(2) // Returns true
array.contains(10) // Returns false
返回一个具有取决于 Hashable
值的唯一值的数组。
let array = [1, 2, 3, 2, 4, 1]
array.unique // Returns [1, 2, 3, 4]
从数组中删除一个元素并返回删除的索引。
var array = [1, 2, 3, 4]
array.remove(3) // Returns 2
array // Mutated to [1, 2, 4]
从 bundle 中快速获取营销版本和构建版本。
let bundle = Bundle.main
bundle.shortVersionString // Returns 1.2.3
bundle.bundleVersion // Returns 1 (the build version)
快速在某个视图周围添加阴影
let view = UIView()
view.layer.applyShadow()
let customShadowView = UIView()
customShadowView.layer.applyShadow(color: .red, opacity: 0.2, x: 0, y: 4, blur: 10, spread: 0)
快速从某个视图中删除阴影
let view = UIView()
view.layer.removeShadow()
在度数和弧度之间相互转换。
CGFloat(180).degreesToRadians // Returns .pi
CGFloat.pi.radiansToDegrees // Returns 180.0
获取管理您的视图的 UIViewController
。
let controller = UIViewController()
controller.view.respondingController // Returns the controller instance.
以类型安全的方式注册和重用 cells。
// Register a cell from a nib with the same name.
collectionView.register(CustomCollectionViewCell.self)
// Register a reusable view from a nib with the same name.
collectionView.register(CustomReusableView.self, forSupplementaryViewOfKind: "Some")
// Dequeue a cell of type CustomCollectionViewCell
collectionView.dequeueReusableCell(for: indexPath) as CustomCollectionViewCell
// Dequeue a reusable view of type CustomReusableView
collectionView.dequeueReusableSupplementaryView(ofKind: "Some", for: indexPath) as CustomReusableView
以类型安全的方式注册和重用 cells。
// Register a cell from a nib with the same name.
tableView.register(CustomTableViewCell.self)
// Register a footer view from a nib with the same name.
tableView.registerHeaderFooter(CustomFooterView.self)
// Dequeue a cell of type CustomTableViewCell
tableView.dequeueReusableCell(forIndexPath: indexPath) as CustomTableViewCell
// Dequeue a cell of type CustomTableViewCell with a custom identifier.
tableView.dequeueReusableCell(forIdentifier: "identifier") as CustomTableViewCell
// Dequeue a header view of type CustomReusableView with a custom identifier.
tableView.dequeueReusableHeaderFooter(forIdentifier: "identifier") as CustomReusableView
// Dequeue a header view of type CustomReusableView.
tableView.dequeueReusableHeaderFooter(forIdentifier: "identifier") as CustomReusableView
// Get the types cell for row.
tableView.cellForRow(at: indexPath) as CustomTableViewCell
快速将子视图约束到父视图的边界。
您可以选择添加 insets,以在视图内设置一些间距。
view.constraint(to: superview)
view.constraint(to: superview, insets: .zero)
// Same as above but take safe area's into account.
view.constraint(to: superview, safeAreaInsets: .zero)
获取对重用标识符的访问权限。
// Return the name of the view's class as it's reuse identifier.
UIView.reuseIdenfier
// Return the name of the view's class as it's nib name.
UIView.nibName
// Return the nib matching the class name of the view.
UIView.nib
从具有相同名称的 nib 文件加载 UIView
。
务必对目标属性进行类型化,以便加载正确的 nib 文件。
let view: SomeView = UIView.loadFromNib()
轻松地添加和删除控制器作为子视图控制器。
您可以选择添加 insets,以在容器视图内设置一些间距。
// Add the controller to the container view, pin it and handle the containment correctly.
rootController.add(childController: controller, to: containerView)
rootController.add(childController: controller, to: containerView, insets: .zero)
// Add the same child controller as above, but take the safe area's into account.
rootController.add(childController: controller, to: containerView, safeAreaInsets: .zero)
// Remove the controller and handle the containtment correctly.
rootController.remove(childController: controller)
直接从故事板加载视图控制器。
// Load the initial controller from the storyboard.
let controller = SomeController.from(storyboard: "StoryboardName")
// Pass a custom bundle to load from.
let controller = SomeController.from(storyboard: "StoryboardName", bundle: CustomBundle())
// Load a controller with the given storyboard identifier.
let controller = SomeController.from(storyboard: "StoryboardName", identifier: "SomeControllerIdentifier")
获取当前呈现在应用程序顶部的 UIViewController
。 UINavigationController
或 UITabBarController
是否在播放并不重要。
let controller = UIAlertController(...)
// Present an alert an top of all the controllers.
otherController.topMostViewController.present(controller, animated: false, completion: nil)
添加了一些易于使用的可选初始化器。
URL(string: nil)
UIImage(data: nil)
Swiftlint
编码准则实现更改。deprecate
)Stella 在 MIT 许可下可用。 有关更多信息,请参见 LICENSE 文件。