GithubHelp home page GithubHelp logo

xhl916235259 / closureskit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lacklock/closureskit

0.0 2.0 0.0 43 KB

swift closures utilities inspired by BlocksKit

License: MIT License

Swift 98.13% Objective-C 1.87%

closureskit's Introduction

ClosuresKit

Build Status Language Carthage compatible License

This project is heavily inspired by the popular BlocksKit.

在OC时代,我经常使用BlocksKit,在RAC引入之前,blockskit让代码优雅了许多。然而swift发布后,因为swift是门强类型语言,不再推荐使用runtime相关的方法。在swift中,如果想使用runtime的有些特性需要特别的声明比如@objc这样。由此,blockskit并没有swift版本。然而,即使去掉了runtime的一些东西,blockskit也有一些扩展的方法很实用。所以,我就自己写了一些能想到能做到的贴近blockskit的常见方法:ClosuresKit。 主要有以下3个部分的扩展 ###Foundation ####集合类型增加了几个判断

  • cs_match 根据传入的闭包找出匹配的一条数据,filter返回的是一个集合,match只返回一条数据,这是区别。示例如下:
    let testSequence = [2,5,6,9]
  
    func testMatch() {
        let result = testSequence.cs_match { (value) -> Bool in
            return value == 5
        }
        assert(result==5,"match failed")
    }
  • cs_any 参数和前面一样,返回的是布尔值,通过传入的闭包判断是否集合中有数据符合这个闭包。
  • cs_all 参数和前面一样,返回的是布尔值,通过传入的闭包判断是否集合中所有数据符合这个闭包。
  • cs_none 参数和前面一样,返回的是布尔值,通过传入的闭包判断是否集合中没有任何一条数据符合这个闭包。

####NSObject可以方便的关联对象 封装了NSObject的objc_setAssociatedObject的方法。 几个api的区别也就是关联的策略不同,比如reatian、copy、weak等。

class AssocaitedObjectTests: XCTestCase {
    static var identifier = "identifier"

    func testCopyAssociateValue(){
        let test:NSMutableString="first"
        let view = UIView()
        view.cs_associateCopyOfValue(test, key: &AssocaitedObjectTests.identifier)
        test.appendString("t")
        let result = view.cs_associateValueForKey(&AssocaitedObjectTests.identifier) as! NSString
        assert(result=="first","CopyAssociateValue failed")
    }
    
}

这是单元测试中写的一个关联对象策略用copy的示例代码。 ####通知 可以通过cs_addNotificationObserverForName方便的添加对某个通知的处理:

 func testObserverNotification() {
        let notificationName = "Test"
        cs_addNotificationObserverForName(name: "Test", object: nil) { (notification) in
            assert(notification.userInfo!["value"] as! String == "param",#function)
        }
        NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: self,userInfo:["value":"param"])
    }

####NSTimer 可以在添加timer时传入闭包直接处理回调。示例代码:

    var count=0
    var timer:NSTimer?

    func testTimerWithtimeInterval() {
        timer = NSTimer.cs_scheduledTimerWithTimeInterval(1, repeats: false, userInfo: ["key":"value"]) {[unowned self] (timer) in
            assert(timer.userInfo!["key"]=="value", #function)
            print("times:\(self.count)")
        }
        timer!.fire()
    }

###UIKIt 给UIControl加了一个方便处理UIControlEvents的方法cs_addEventHandlerForEvents,示例如下:

   btn.cs_addEventHandlerForEvents(.TouchUpInside) { (sender) in
            print("TouchUpInside")
        }

###UIGesture 可以给UIView方便的直接添加手势,支持链式编程,可以在添加手势时那个闭包里配置,连续处理几种不同的状态:

     label.cs_addPanGesture { (gestureRecognizer) in
            gestureRecognizer.maximumNumberOfTouches=2
        }.whenBegan { (gestureRecognizer) in
            print("began")
        }

如果不用配置,配置的闭包可以直接为空:

       label.cs_addPanGesture().whenChanged { (gestureRecognizer) in
            print("changed")
        }

也可以同时给几个状态添加同一个处理闭包:

lbState.nc_addPanGesture().whenStatesHappend([.Ended,.Changed]) { (gestureRecognizer) -> Void in
            
        }

还给tap和swipe添加了两个快捷的处理方法:

    label.cs_whenTapped { (tapGestureRecognizer) in
            print("tapped")
        }
        
     view.cs_whenSwipedInDirection(.Down) { (gestureRecognizer) in
            print("down")
        }

目前想到的api就这些,如果有需要添加的可以直接提到issue里,我会及时处理的,也欢迎直接提pull request。

#Installation

###Carthage github "lacklock/ClosuresKit"

#Contact

Weibo : @没故事的卓同学

License

ClosuresKit is available under the MIT license. See the LICENSE file for more info.

closureskit's People

Contributors

lacklock avatar fcgeek avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.