数组的enumerateObjects

280 阅读1分钟
import Foundation

// oc -> BOOL *
// swift -> UnsafeMutablePointer<ObjCBool>

class Example {
	func test() {
    	// 要在swift用使用NS需要导入Foundtion
      var arr = NSArray(Objects: 11, 22, 33, 44)
      arr.enumerateObjects { (element, idx, stop) in
          if idx == 2 {
              stop.pointee = true
          }

          print(idx, element)
      }
    }
    
    // 可读性更好的做法
    func test1() {
    	var arr = NSArray(Objects: 11, 22, 33, 44)
        for (idx, element) in arr.enumerated() {
        	print(idx, element)
        	if idx == 2 {
                break;
            }
        }
    }
}