Converting Swift Array to Cfarray in Xcode 8 (Swift 3)

Converting Swift array to CFArray in Xcode 8 (Swift 3)

It works if you add the cast as CFArray:

let colors = [fromColor.cgColor, toColor.cgColor] as CFArray

or you can add the cast in a call:

let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors:[fromColor.cgColor, toColor.cgColor] as CFArray, locations:[0.0, 1.0])!

In Swift 3 (Xcode 8 beta 6), implicit casting to bridged types has been removed. In some cases, like this one, you will need to add explicit casting to make it work.

How to convert CFArray to Swift Array?

Here is how to do this, based on the current state of the Swift compiler and Swift documentation. Hopefully this gets cleaned up in later betas.

UPDATE: Since Beta 5, reinterpretCast has been renamed to unsafeBitCast, and a CTLine object must be sent to it as an input. Way #2 still does not work.


Way #1 - Use the CFArray directly

let line: CTLine = reinterpretCast(CFArrayGetValueAtIndex(lines, 0))

Regarding Gary Makin's comments - The Ref can be dropped from CTLineRef, but this does not change ARC vs non-ARC. According to Using Swift with Cocoa and Objective-C pages 53-54, ref and non-ref are identical to the compiler. Attempting to call CFRelease causes a compiler error.


Way #2 - Convert the CFArray to a Swift array - Does not currently work

Ideally, we want to convert lines to a Swift array of CTLine objects since we know that's what is returned by CTFrameGetLines, giving us type safety after the conversion. Probably due to a compiler bug, the array can be converted to an [AnyObject] array, but not to [CTLine]. According to Apple's documentation, this should work:

let linesNS: NSArray  = CTFrameGetLines(frame)
let linesAO: [AnyObject] = linesNS as [AnyObject]
let lines: [CTLine] = linesAO as [CTLine]

This converts CFArray to NSArray, then NSArray to Swift Array [AnyObject], then downcasts that array to the specific type CTLine. This compiles, but when it is run, there is an EXC_BREAKPOINT crash on the last line.

CGGradient issues with swift 3

Cast the type

let colours = [tColour.cgColor, bColour.cgColor] as CFArray
let colorSpace = CGColorSpaceCreateDeviceRGB()
let gradient = CGGradient(colorsSpace: colorSpace, colors: colours , locations: nil)

Migration to Swift 3

What you were doing was always wrong. If you have no value to supply at initialization time, use an Optional. Ideally you should declare this as the actual type of value that it will be when gets a value (rather than a catch-all type such as AnyObject). But if you can't do that, then just use Any?:

var service : Any?

Or, if this thing's type is known — for example, if you know it's going to be a Dictionary — then declare it as a Dictionary, possibly by supplying an empty Dictionary, like this:

var service = [AnyHashable:Any]()

PaintCode StyleKit Swift 3.0 Error with CGGradient

There are a few more things you need to change:

  1. UIColor.whiteColor() is now UIColor.white.
  2. .CGColor is now .cgColor
  3. You need to cast your colors array to CFArray. Implicit casts to bridged types are no longer done in Swift 3.
  4. Swift can infer the type of redGradient so you can drop the : CGGradient.

With these changes, the code becomes:

static let redGradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(),
colors: [UIColor.white.cgColor, UIColor.white.blendedColorWithFraction(0.5, ofColor: StyleKitMarkSix.red).cgColor, StyleKitMarkSix.red.cgColor] as CFArray,
locations: [0, 0, 1])!

Issue when I'm trying to draw gradient in swift

Swift 3

let colors = [UIColor.red.cgColor, UIColor.green.cgColor,
UIColor.blue.cgColor, UIColor.yellow.cgColor
] as CFArray

Swift 2

You can annotate constant with explicit type CFArray:

let colors: CFArray = [UIColor.redColor().CGColor, ...

Cannot convert value of type 'SecCertificate' to expected argument type 'UnsafeMutablePointer UnsafeRawPointer? !'

The line causing the error, is for creating a CFArray.

One simple way to create a CFArray, is to use bridging the Swift Array to CFArray.

Try changing the line:

let certArrayRef = CFArrayCreate(nil, cert, 1, nil)

To:

let certArrayRef = [cert] as CFArray

How to use CFDictionarySetValue in swift?

In the first code snippet, you have a call to CFArrayGetValueAtIndex, which returns a dictionary that you pass to CFDictionarySetValue.

In the second code snippet, you don't call CFArrayGetValueAtIndex. You just pass the array to CFDictionarySetValue. Since an array is not a dictionary, you get a fatal error.



Related Topics



Leave a reply



Submit