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 协议了解更多.

[翻译] TransitionKit的更多相关文章

  1. 《Django By Example》第五章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者@ucag注:大家好,我是新来的翻译, ...

  2. 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...

  3. [翻译]开发文档:android Bitmap的高效使用

    内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...

  4. 【探索】机器指令翻译成 JavaScript

    前言 前些时候研究脚本混淆时,打算先学一些「程序流程」相关的概念.为了不因太枯燥而放弃,决定想一个有趣的案例,可以边探索边学. 于是想了一个话题:尝试将机器指令 1:1 翻译 成 JavaScript ...

  5. 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...

  6. 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...

  7. 《Django By Example》第一章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:本人目前在杭州某家互联网公司工作, ...

  8. 【翻译】Awesome R资源大全中文版来了,全球最火的R工具包一网打尽,超过300+工具,还在等什么?

    0.前言 虽然很早就知道R被微软收购,也很早知道R在统计分析处理方面很强大,开始一直没有行动过...直到 直到12月初在微软技术大会,看到我软的工程师演示R的使用,我就震惊了,然后最近在网上到处了解和 ...

  9. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点

    在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...

随机推荐

  1. Linux的MySQL不能远程访问

    1.首先,你要确认用户是否只允许localhost访问: 在linux下登录mysql mysql -uroot -p密码;     use mysql;     select `host`,`use ...

  2. java.io.Serializable的作用

    Serializable,之前一直有使用,默认的实体类就会实现Serializable接口,对具体原因一直不是很了解,同时如果没有实现序列化,同样没什么影响,什么时候应该进行序列化操作呢?今天查了下资 ...

  3. [转载]Angular4 组件通讯方法大全

    组件通讯,意在不同的指令和组件之间共享信息.如何在两个多个组件之间共享信息呢. 最近在项目上,组件跟组件之间可能是父子关系,兄弟关系,爷孙关系都有.....我也找找了很多关于组件之间通讯的方法,不同的 ...

  4. EMC,EMI,EMS,ESD分别是什么?有什么区别和联系?

    一.EMC EMI EMS定义: EMC(ElectromagneticCompatibility) 电磁兼容,是指设备或系统在电磁环境中性能不降级的状态.电磁兼容,一方面要求系统内没有严重的干扰源, ...

  5. BG.Hive - part1

    1. Hive架构 What is hive? Facebook,https://en.wikipedia.org/wiki/Apache_Hive a> 一种工具,可以通过SQL轻松的访问数据 ...

  6. 【原】公司P2P平台的功能拆分

    银行回调:由原来写在PC门户项目中拆分开来,作为一个专门处理回调的项目,配置多个数据源,实时写入数据库. 定时回查:由原来写在PC后台管理项目中拆分开来,作为一个专门回查银行网关的项目. 请求银行:由 ...

  7. @Value失效的问题

    @Value 会在@Controller中失效,失效原因涉及源码问题就不一一叙述了,一般加上@Service,@Component就能解决.如果是在Controller中使用建议新建一个配置类,然后在 ...

  8. 为什么说 LINQ 要胜过 SQL

    如果你还没有沉溺于 LINQ,就会想这有啥大惊小怪的.SQL 并没有坏掉,为什么还要对它进行修补呢? 为什么我们还需要另外一种查询语言呢? 流行的说法是 LINQ 同 C#(或者 VB)集成在了一起, ...

  9. MVC 中文显示乱码问题

    在学习中遇到中文乱码的问题,在网上搜了一下大神的解决方法,总结了一点知识点. 项目中的代码 //GET: /HelloWorld/Welcome/ public string Welcome(stri ...

  10. win8.1怎么安装iis

    进入系统后,在左下角处点鼠标右键,再点击[程序和功能]如下图所示:       2 进入程序和功能界面后,点击[启用或关闭Windows功能] 在WINDOWS功能对话框中找到[internet in ...