Swift 之数组过滤 filter 方法


    func findEmoticon(string: String) -> CZEmoticon? {
        //
        for p in packages {
            // 1
            //let result = p.emoticons.filter({ (em) -> Bool in
            //    return em.chs == string
            //})
            
            // 2
            //let result = p.emoticons.filter() { (em) -> Bool in
            //    return em.chs == string
            //}
            
            // 3
            //let result = p.emoticons.filter() {
            //    return $0.chs == string
            //}
            
            // 4
            let result = p.emoticons.filter() { $0.chs == string }
            
            //
            if result.count == 1 {
                return result[0]
            }
        }
        
        return nil
    }


你可能感兴趣的:(Swift 之数组过滤 filter 方法)