user2713634
user2713634

Reputation: 13

Cannot invoke 'CFHTTPMessageCopySerializedMessage' with an argument list of type '(Unmanaged<CFHTTPMessage>!)'

I followed with the Apple's documentation, but in Swift, then I came across this error.

let url = "http://www.apple.com"
let myURL = CFURLCreateWithString(kCFAllocatorDefault, url, nil);

let myRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, "GET", myURL, kCFHTTPVersion1_1)
let mySerializedRequest = CFHTTPMessageCopySerializedMessage(myRequest)

Cannot invoke 'CFHTTPMessageCopySerializedMessage' with an argument list of type '(Unmanaged< CFHTTPMessage>!)'

Upvotes: 1

Views: 427

Answers (1)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

Here is your working code:

let url = "http://www.apple.com"
let myURL = CFURLCreateWithString(kCFAllocatorDefault, url, nil)
let myRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, "GET", myURL, kCFHTTPVersion1_1).takeRetainedValue()
let mySerializedRequest = CFHTTPMessageCopySerializedMessage(myRequest).takeRetainedValue()

You use takeRetainedValue when the unmanaged object has a +1 retain count and you want ARC to take care of releasing the object when you're done.

In your case the definition of CFHTTPMessageCopySerializedMessage is:

func CFHTTPMessageCopySerializedMessage(message: CFHTTPMessage!) -> Unmanaged<CFData>!

So you have to pass the argument of type CFHTTPMessage and your myRequest type without using takeRetainedValue() is Unmanaged<CFHTTPMessage>! so CFHTTPMessageCopySerializedMessage will never accept it thats why you have to add takeRetainedValue() at last like this:

let myRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, "GET", myURL, kCFHTTPVersion1_1).takeRetainedValue()

And it will work fine.

Upvotes: 1

Related Questions