enumerateObjectsUsingBlock in Swift

后端 未结 4 1133
孤城傲影
孤城傲影 2021-02-05 02:10

I am trying to iterate through an array using enumerateObjectsUsingBlock to fetch data. How to use enumerateObjectsUsingBlock in Swift ? Please help me with an example.

相关标签:
4条回答
  • 2021-02-05 02:30

    The new enumerate function returns a tuple with indexer and value so you can get similar functionality to enumerateObjectsUsingBlock.

    func myBlock (index: Int, value: Int, inout stop: Bool) -> Void {
        println("Index: \(index) Value: \(value)")
        if index == 3 {
            stop = true
        }
    }
    
    var array = [1,2,3,4,5]
    
    for (index, value) in enumerate(array) {
        var stop = false;
        myBlock(index, value, &stop)
        if stop  {
            break;
        }
    }
    
    //Output...
    //Index: 0 Value: 1
    //Index: 1 Value: 2
    //Index: 2 Value: 3
    //Index: 3 Value: 4
    

    I imagine they haven't exposed enumerateObjectsUsingBlock as you can replicate the functionality with the above code.

    EDIT: Anonymous function was crashing my playground so used an inline function. Also added using stop variable for illustrative purposes.

    0 讨论(0)
  • 2021-02-05 02:31

    I write the extension of CollectionType.

    Any way to stop a block literal in swift

        func forEach(body: ((Self.Generator.Element, Int, inout Bool) -> Void)) {
            var stop = false
            let enumerate = self.enumerate()
            for (index,value) in enumerate {
                if stop { break }
                body(value,index,&stop)
            }
        }
    
    0 讨论(0)
  • 2021-02-05 02:47

    The existing answers can achieve what has been asked in the question. Also, you can write an extension to Array class and add similar method as -

    extension Array{
    func enumerateObject(_ block : (Any, Int,inout Bool)-> Void){
        var stop = false
        for (index, aValue) in self.enumerated(){
            block(aValue,index,&stop);
        }
    }
    

    //uses

    let numArr = [2,4,7,8,9];
    numArr.enumerateObject { (aValue, index, stop) in
        let value = aValue as! Int
        if !stop{
            print("At index \(index) value \(value)");
        }  
        if value == 7{
            print(value);
            stop = true;    
        }
    
    }
    
    0 讨论(0)
  • 2021-02-05 02:56

    Foundation

    enumerateObjectsUsingBlock: is not a method on Array but on NSArray. If you want to use it you'll need an instance of NSArray rather than Array.

    import Foundation
    
    var array: NSArray = ["Some", "strings", "in", "an", "array"]
    
    array.enumerateObjectsUsingBlock({ object, index, stop in
        //your code
    })
    

    if you have an existing array, you can cast a swift Array to an NSArray using as

    var cocoaArray = swiftArray as NSArray
    

    or if you simply import Foundation the Swift compiler will automatically bridge your Swift array to an NSArray and all the NSArray methods will become available for you.

    Swift 1

    or you can just use Swift's enumerate function:

    for (index, value) in enumerate(array) {
        // your code
    }
    

    Swift 2

    In Swift 2, enumerate is no longer a free function, now it's in a protocol extension!

    for (index, value) in array.enumerate() {
        // your code
    }
    

    Swift 3

    And in Swift 3, enumerate was renamed to enumerated

    for (index, value) in array.enumerated() {
        // your code
    }
    
    0 讨论(0)
提交回复
热议问题