2016-11-29 6 views
2

Swift 3でこれを書くにはどうすればよいですか?Swift3:2種類のuint64タイプを組み合わせる

let ld = NSDataDetector(types: NSTextCheckingResult.CheckingType.address | NSTextCheckingResult.CheckingType.phoneNumber) 

これは私が得るものです:

バイナリ演算子| 2つのNSTextCheckingResult.CheckingTypeオペランドに適用することはできません。

私はそれらがどちらもUInt64だと知っていますが、私はそれらをどのように組み合わせるか分かりません。

答えて

0

タイプCheckingTypeがint変異体ではないようにこれらの定数の生の値を使用する:NSTextCheckingResult.CheckingType.addressで

NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue) 
0

アドレスが列挙場合、ないUInt64型です。生の値がUInt64型であるあなたは、このように生の値を使用することができますので、

do{ 

let ld = try NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue) 

}catch{ 
    print("error") 
} 
+0

をさらにより多くの、それは投げることができますエラーが発生しました..よく使うブロックを試してください try catchブロックで答えを編集しました –

1

は、私が個人的にCheckingType値の配列を使用することにより、機能的なアプローチで行くと思います。この

do { 
    let ld = try NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue) 
} 
catch { 

} 
0

を試してみてください。これは、コードの重複を削減し、スキャナに新しいチェックタイプを追加することが容易になります:

let detectorTypes = [ 
    NSTextCheckingResult.CheckingType.address, 
    NSTextCheckingResult.CheckingType.phoneNumber 
].reduce(0) { $0 | $1.rawValue } 
let detector = try? NSDataDetector(types: detectorTypes) 

あるいは、さらに値の接頭辞で重複を減らすために:

let types: [NSTextCheckingResult.CheckingType] = [.address, .phoneNumber] 
let detector = try? NSDataDetector(types: types.reduce(0) { $0 | $1.rawValue }) 
関連する問題