6

Is there an easy way to do this that I'm missing? Right now I'm doing this like so:

let doesConformNumber: NSNumber = NSNumber(unsignedChar: UTTypeConformsTo(utiCF, typeCF))
if doesConformNumber.boolValue {
    return true
}

If I try to do a simple cast like so:

let testBool: Bool = UTTypeConformsTo(utiCF, typeCF)

I get the error 'Boolean' is not convertible to 'Bool'

Anyone have a cleaner way of doing this conversion?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
arcticmatt
  • 1,956
  • 1
  • 19
  • 36
  • 3
    `let testBool = doesConformNumber.boolValue` not enough? – Bryan Chen Aug 29 '14 at 03:06
  • the checked answer is cleaner, which is all I wanted – arcticmatt Aug 29 '14 at 15:56
  • Note that `UTTypeConformsTo` returns a CoreFoundation boolean, `Boolean`; an Objective-C boolean is a `BOOL`. The question is a good one, the summary is just a bit misleading. But probably in a good way for searches. :) – Steven Fisher Mar 30 '15 at 20:07
  • 1
    Please note that at some point in the years since this question was asked, Apple changed the way Swift exposes `UTTypeConformsTo` so that it returns a native Swift `Bool`. No conversion is necessary in modern Swift. – rob mayoff Nov 30 '18 at 21:28

1 Answers1

6

UTTypeConformsTo() returns a Boolean, which is a type alias for Int8 and not directly convertible to Bool. The simplest way would be

let testBool : Bool = UTTypeConformsTo(utiCF, typeCF) != 0

where the type annotation is actually not necessary:

let testBool = UTTypeConformsTo(utiCF, typeCF) != 0
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    what's the difference between Bool and Boolean? –  Apr 24 '15 at 08:51
  • 1
    @jawanam: `Boolean` is a "historic Mac type" and defined as `Int8`. `Bool` is the Swift boolean type. Compare http://stackoverflow.com/questions/27304158/type-boolean-does-not-conform-to-protocol-booleantype. – Martin R Apr 24 '15 at 08:56
  • So it should use Bool over Boolean whenever it's possible? –  Apr 25 '15 at 03:39