Swift: Count Items Matching a Rule
swift\nclass Solution {\n    func countMatches(_ items: [[String]], _ ruleKey: String, _ ruleValue: String) -> Int {\n        let rule = ruleKey == "type" ? 0 :
```swift\nclass Solution {\n    func countMatches(_ items: [[String]], _ ruleKey: String, _ ruleValue: String) -> Int {\n        let rule = ruleKey == "type" ? 0 : (ruleKey == "color" ? 1 : 2)\n        return items.reduce(0) { $0 + ($1[rule] == ruleValue ? 1 : 0)}\n    }\n}\n```\n\n```swift\nimport XCTest\n\n// Executed 2 tests, with 0 failures (0 unexpected) in 0.009 (0.013) seconds\n\nclass Tests: XCTestCase {\n    private let s = Solution()\n    func testExample1() {\n        let items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]]\n        XCTAssertEqual(s.countMatches(items, "color", "silver"), 1) // success\n    }\n    func testExample2() {\n        let items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]]\n        XCTAssertEqual(s.countMatches(items, "type", "phone"), 2) // success\n    }\n}\n\nTests.defaultTestSuite.run()\n```
['Swift']