[翻译] TransitionKit

简介:

TransitionKit

 

https://github.com/blakewatters/TransitionKit

 

 

 

A simple, elegantly designed block based API for implementing State Machines in Objective-C

一套简单,优雅的基于block设计的API,用以实现OC中的状态机.

 

TransitionKit is a small Cocoa library that provides an API for implementing a state machine in Objective C. It is full-featured, completely documented, and very thoroughly unit tested. State machines are a great way to manage complexity in your application and TransitionKit provides you with an elegant API for implementing state machines in your next iOS or Mac OS X application.

TransitionKit是一个简单的Cocoa库,提供API用以实现OC的状态机.它有着完整的特性,全面的文档以及经过了详细的单元测试.状态机是一个用以解决复杂的UI与数据源业务交互逻辑的一种方案,也许就会用在你的下一款为iOS或者OS X设计的项目当中.

 

Features

  • Supports an arbitrary number of States and Events. 支持任意数目的状态以及事件
  • States and Events support a thorough set of block based callbacks for responding to state transitions. 状态与事件支持一系列基于block的回调,用以响应状态的改变
  • States, Events, and State Machines are NSCopying and NSCoding compliant, enabling easy integration with archiving and copying in your custom classes. 状态,事件以及状态机遵循NSCopying与NSCoding协议,即使你的类中需要支持NSCopying,NSCoding,你也可以方便的使用状态机
  • Strongly enforced. The state machine includes numerous runtime checks for misconfigurations, making it easy to debug and trust your state machines. 状态机非常可靠,可以方便你debug,放行使用
  • Transitions support the inclusion of arbitrary user data via a userInfo dictionary, making it easy to broadcast metadata across callbacks. Transitions支持你传递字典,你可以方便的将元数据进行广播,通过回调的方式
  • Completely Documented. The entire library is marked up with Appledoc. 有着完整的文档,方便你查找
  • Thorougly unit tested. You know it works and can make changes with confidence. 通过了单元测试,请相信,这个状态机是非常可靠的
  • Lightweight. TransitionKit has no dependencies beyond the Foundation library and works on iOS and Mac OS X. 轻量级,TransitionKit不依赖于Foundation框架,你可以便利的在iOS于Mac OS X中使用

 

Installation via CocoaPods

The recommended approach for installing TransitionKit is via the CocoaPods package manager, as it provides flexible dependency management and dead simple installation. For best results, it is recommended that you install via CocoaPods >= 0.16.0 using Git >= 1.8.0 installed via Homebrew.

推荐的安装方式是通过CocoaPods包来安装,它提供了灵活的依赖关系,并让安装变得非常简易.建议你通过Homebrew,使用CocoaPods >= 0.16.0或者是Git >= 1.8.0来安装.

Install CocoaPods if not already available:

安装CocoaPods,如果你还没用过CocoaPods

$ [sudo] gem install cocoapods
$ pod setup

Change to the directory of your Xcode project, and Create and Edit your Podfile and add TransitionKit:

进入你的Xcode工程目录,创建,编辑你的Podfile,然后添加TransitionKit:

$ cd /path/to/MyProject
$ touch Podfile
$ edit Podfile
platform :ios, '5.0' 
# Or platform :osx, '10.7' pod 'TransitionKit', '~> 2.0.0'

Install into your project:

安装到你的项目当中:

$ pod install

Open your project in Xcode from the .xcworkspace file (not the usual project file)

然后,打开.xcworkspace文件即可

$ open MyProject.xcworkspace

 

Examples

The following example is a simple state machine that models the state of a Message in an Inbox.

以下示例是一个简单的使用示例,用以反映状态机的基本运作.

TKStateMachine *inboxStateMachine = [TKStateMachine new];

TKState *unread = [TKState stateWithName:@"Unread"]; [unread setDidEnterStateBlock:^(TKState *state, TKTransition *transition) { [self incrementUnreadCount]; }]; TKState *read = [TKState stateWithName:@"Read"]; [read setDidExitStateBlock:^(TKState *state, TKTransition *transition) { [self decrementUnreadCount]; }]; TKState *deleted = [TKState stateWithName:@"Deleted"]; [deleted setDidEnterStateBlock:^(TKState *state, TKTransition *transition) { [self moveMessageToTrash]; }]; [inboxStateMachine addStates:@[ unread, read, deleted ]]; inboxStateMachine.initialState = unread; TKEvent *viewMessage = [TKEvent eventWithName:@"View Message" transitioningFromStates:@[ unread ] toState:read]; TKEvent *deleteMessage = [TKEvent eventWithName:@"Delete Message" transitioningFromStates:@[ read, unread ] toState:deleted]; TKEvent *markAsUnread = [TKEvent eventWithName:@"Mark as Unread" transitioningFromStates:@[ read, deleted ] toState:unread]; [inboxStateMachine addEvents:@[ viewMessage, deleteMessage, markAsUnread ]]; // Activate the state machine [inboxStateMachine activate]; [inboxStateMachine isInState:@"Unread"]; // YES, the initial state // Fire some events NSDictionary *userInfo = nil; NSError *error = nil; BOOL success = [inboxStateMachine fireEvent:@"View Message" userInfo:userInfo error:&error]; // YES success = [inboxStateMachine fireEvent:@"Delete Message" userInfo:userInfo error:&error]; // YES success = [inboxStateMachine fireEvent:@"Mark as Unread" userInfo:userInfo error:&error]; // YES success = [inboxStateMachine canFireEvent:@"Mark as Unread"]; // NO // Error. Cannot mark an Unread message as Unread success = [inboxStateMachine fireEvent:@"Mark as Unread" userInfo:nil error:&error]; // NO // error is an TKInvalidTransitionError with a descriptive error message and failure reason

 

Unit Tests

TransitionKit is tested using the Kiwi BDD library. In order to run the tests, you must do the following:

TransitionKit使用了Kiwi BDD库进行测试.为了运行这个测试,你需要做如下几步:

  1. Install the dependencies via CocoaPods: pod install 先通过CocoaPods来安装依赖
  2. Open the workspace: open TransitionKit.xcworkspace 然后打开TransitionKit.xcworkspace
  3. Run the specs via the Product menu > Test 然后选择Product menu > Test 来运行

 

Contact

Blake Watters

 

License

TransitionKit is available under the Apache 2 License. See the LICENSE file for more info.

TransitionKit支持Apache 2 协议,请查看Apache 2 协议了解更多.

 

目录
相关文章
|
11月前
|
人工智能 安全 测试技术
山石网科×阿里云通义灵码,开启研发“AI智造”新时代
山石网科与阿里云的合作由来已久,双方在云计算、网络安全等领域保持着紧密的合作关系。此次山石网科全面接入阿里云通义灵码专属版,是双方合作关系的进一步深化,也是双方共同推动AI技术在网络安全领域应用的一次重要实践。未来,山石网科将继续与阿里云携手共进,积极探索AI技术在网络安全领域的创新应用,不断提升产品研发效率和服务质量,为用户提供更智能、更安全的网络安全解决方案,共同守护数字世界的安全!
山石网科×阿里云通义灵码,开启研发“AI智造”新时代
|
11月前
|
算法
MATLAB在风险管理中的应用:从VaR计算到压力测试
本文介绍如何使用MATLAB进行风险管理,涵盖风险度量(如VaR)、压力测试和风险分解。通过历史模拟法、参数法和蒙特卡洛模拟法计算VaR,评估投资组合在极端市场条件下的表现,并通过边际VaR和成分VaR识别风险来源。结合具体案例和代码实现,帮助读者掌握MATLAB在风险管理中的应用,确保投资组合的稳健性。
Parsing error: The keyword ‘const‘ is reserved
Parsing error: The keyword ‘const‘ is reserved
|
分布式计算 大数据 Hadoop
MapReduce:大数据处理的基石
【8月更文挑战第31天】
576 0
|
数据采集 弹性计算 供应链
阿里云服务器节省计划、按量付费、抢占式实例、包年包月四种付费类型区别及适用场景参考
阿里云服务器ECS提供灵活的计费方式,在通过云服务器ECS产品页面购买的时候,默认可选的付费类型为包年包月、按量付费和抢占式实例,而节省计划是在已经持有一定数量的按量付费实例,希望进行成本优化时通过“节省计划购买方案测算”选择的计费模式。本文来为大家介绍一下这些付费类型的主要区别及适用场景参考。
阿里云服务器节省计划、按量付费、抢占式实例、包年包月四种付费类型区别及适用场景参考
|
机器学习/深度学习 传感器 自然语言处理
时间序列预测的零样本学习是未来还是炒作:TimeGPT和TiDE的综合比较
最近时间序列预测预测领域的最新进展受到了各个领域(包括文本、图像和语音)成功开发基础模型的影响,例如文本(如ChatGPT)、文本到图像(如Midjourney)和文本到语音(如Eleven Labs)。这些模型的广泛采用导致了像TimeGPT[1]这样的模型的出现,这些模型利用了类似于它们在文本、图像和语音方面获得成功的方法和架构。
496 1
|
Java 应用服务中间件 nginx
DockerFile详解以及测试案例
DockerFile详解以及测试案例
420 0
|
SQL 存储 缓存
18种接口实用优化方案总结
18种接口实用优化方案总结
386 0
|
前端开发 JavaScript
webpack原理篇(五十三):Tapable插件架构与Hooks设计
webpack原理篇(五十三):Tapable插件架构与Hooks设计
511 0
webpack原理篇(五十三):Tapable插件架构与Hooks设计