• Block写法


    看到一个很不错的Block写法总结,为了方便拷过来了,参考链接:http://www.jianshu.com/p/f827a15aa78f

    As a local variable:

    returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};

    As a property:

    @property (nonatomic, copy) returnType (^blockName)(parameterTypes); 

    As a method parameter:

    - (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;

    As an argument to a method call:

    [someObject someMethodThatTakesABlock:^returnType (parameters) {...}];

    As a typedef:

    typedef returnType (^TypeName)(parameterTypes);
    TypeName blockName = ^returnType(parameters) {...};
     
     
     
    Swift:

    As a variable:

    var closureName: (ParameterTypes) -> ReturnType


    As a type alias:

    typealias ClosureType = (ParameterTypes) -> ReturnType


    As a constant:

    let closureName: ClosureType = { ... }


    As a parameter to another function:

    funcName(parameter: (ParameterTypes) -> ReturnType)

    Note: if the passed-in closure is going to outlive the scope of the method, e.g. if you are saving it to a property, it needs to be annotated with @escaping.

    As an argument to a function call:

    funcName({ (ParameterTypes) -> ReturnType in statements })


    As a function parameter:

    array.sorted(by: { (item1: Int, item2: Int) -> Bool in return item1 < item2 })


    As a function parameter with implied types:

    array.sorted(by: { (item1, item2) -> Bool in return item1 < item2 })


    As a function parameter with implied return type:

    array.sorted(by: { (item1, item2) in return item1 < item2 })


    As the last function parameter:

    array.sorted { (item1, item2) in return item1 < item2 }


    As the last parameter, using shorthand argument names:

    array.sorted { return $0 < $1 }


    As the last parameter, with an implied return value:

    array.sorted { $0 < $1 }  


    As the last parameter, as a reference to an existing function:

    array.sorted(by: <)


    As a function parameter with explicit capture semantics:

    array.sorted(by: { [unowned self] (item1: Int, item2: Int) -> Bool in return item1 < item2 })


    As a function parameter with explicit capture semantics and inferred parameters / return type:

    array.sorted(by: { [unowned self] in return $0 < $1 })
  • 相关阅读:
    H5+CSS3知识点
    三维立体
    HTML5的Audio标签打造WEB音频播放器
    video.js-H5视频播放库
    js事件(Event)知识整理
    高端大气上档次的fullPage.js
    javascript的正则表达式学习
    轮播图—运动框架来实现(拥有透明度效果)
    Android FM模块学习之四源码分析(3)
    Android FM模块学习之四源码学习(2)
  • 原文地址:https://www.cnblogs.com/staRR-k2/p/7655875.html
Copyright © 2020-2023  润新知