1

I've tried dozens of things to get this right and just can't come up with anything that works. Can anyone tell me what's wrong with the following Swift code:

let incomingRequest: CFHTTPMessageRef? = CFDictionaryGetValue(self.incomingRequests as CFMutableDictionaryRef, unsafeAddressOf(incomingFileHandle!)) as CFHTTPMessageRef

The code above gives the error message: 'UnsafePointer<Void>' is not convertible to 'CFHTTPMessageRef'

I guess what I don't understand is how do I convert an 'UnsafePointer' returned by a Core Foundation function to the pointer type it should be (i.e. CFHTTPMessageRef in this case). How do I find documentation on how to do this. I've read everything I can find, but nothing so far explains how to recast return values to what they should have been in the first place. This has to be documented somewhere, doesn't it?

EDIT

Here's the code I'm having trouble with:

let incomingRequest = CFDictionaryGetValue(self.incomingRequests as CFMutableDictionaryRef, unsafeAddressOf(incomingFileHandle!))
        unsafeBitCast(incomingRequest, CFHTTPMessageRef.self)
if (incomingRequest != nil) {
let success: Boolean = CFHTTPMessageAppendBytes(incomingRequest as CFHTTPMessageRef, unsafeAddressOf(data!.bytes) as UnsafePointer<UInt8>, data!.length)
if success { // Do something... }

The CFHTTPMessageAppendBytes call still gives a "Type 'UnsafePointer' does not conform to protocol 'AnyObject'". And the following 'if' check for 'success' complains that "Type 'Boolean' doesn not conform to protocol 'BooleanType'". What the heck is that about? Boolean isn't a Boolean type?

I find the strict type checking of Swift extremely frustrating. So far it is far more difficult to code in than Obj-C, C, or any of the other languages I've used. I'm sure it's because I just don't get it, or haven't found the right documentation, but this is really driving me crazy.

Shuo
  • 8,447
  • 4
  • 30
  • 36
Ron B.
  • 1,091
  • 1
  • 7
  • 13
  • This might help: http://stackoverflow.com/q/24838106/535275 – Scott Hunter Nov 04 '14 at 03:40
  • I checked-out the link, but it doesn't seem to apply. It doesn't show how to cast an UnsafePointer to a pointer type required by other CF functions. If it's not typed properly, they won't accept it. Or perhaps I'm missing something... – Ron B. Nov 04 '14 at 04:47
  • You should use the return value of unsafeBitCast – Shuo Nov 04 '14 at 16:14
  • Can you see my comment? you should at least do "var castedRequest: CFHTTPMessageRef = unsafeBitCast(incomingRequest, CFHTTPMessageRef.self)" – Shuo Nov 04 '14 at 16:28
  • Hi Shuo. YES, sorry I missed that befor (frazzled I guess). Looks like I have to do a similar thing for CF functions that return a Boolean (instead of a Bool). I don't understand the distinction, but it's necessary if you want to directly test the outcome of a Boolean returning function. I wish there was something I could refer to online or in PDF that could explain the difference between Bool and Boolean, and why the distinction is important. And also something to explain why most CF funtions return UnsafePointer when they themselves require explicitly typed objects as input variables. – Ron B. Nov 04 '14 at 16:36

1 Answers1

3

Use unsafeBitCast. As following example:

import Foundation
var x = "1"
var dict : NSMutableDictionary = [x: "2"]
var y = CFDictionaryGetValue(dict as CFMutableDictionaryRef, unsafeAddressOf(x))
let str: NSString = unsafeBitCast(y, NSString.self)
str == "2"

FYI: There is one quora related with unsafeBitCast. http://www.quora.com/Why-does-Swift-not-allow-implicit-type-conversion

Shuo
  • 8,447
  • 4
  • 30
  • 36
  • Thanks for that. I implemented the unsafeBitCast and it compiled, but it didn't effectively change anything. I've edited the original post with the code I'm having trouble with: – Ron B. Nov 04 '14 at 15:48
  • Update your code into your original question pls? Cant see your code. – Shuo Nov 04 '14 at 15:50
  • I updated my original post with the new code fragment. – Ron B. Nov 04 '14 at 16:03