Swift: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'

Luke Pistrol

I have a little Problem with my Code after updating to Swift 3. I had this Code before the conversion:

extension NSData {
  func castToCPointer<T>() -> T {
    let mem = UnsafeMutablePointer<T>.alloc(sizeof(T.Type))
    self.getBytes(mem, length: sizeof(T.Type))
    return mem.move()
  }
}

And I converted it to this Code and in the 3rd line I get an Error

... Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'

extension Data {
  func castToCPointer<T>() -> T{
    let mem = UnsafeMutablePointer<T>.allocate(capacity: MemoryLayout<T.Type>.size)
    self.copyBytes(to: mem, count: MemoryLayout<T.Type>.size)
    //self.copyBytes(to: mem, count: MemoryLayout<T.Type>.size)
    return mem.move()
  }
}

Does anyone know how to get rid of this?

Martin R

copyBytes expects a UnsafeMutableBufferPointer as argument:

extension Data {
    func castToCPointer<T>() -> T {
        let mem = UnsafeMutablePointer<T>.allocate(capacity: 1)
        _ = self.copyBytes(to: UnsafeMutableBufferPointer(start: mem, count: 1))
        return mem.move()
    }
}

(allocate() takes the number of "items" as argument, not the number of bytes.)

But note that your method leaks memory, the allocated memory is deinitialized (with move()) but also has to be deallocated:

extension Data {
    func castToCPointer<T>() -> T {
        let mem = UnsafeMutablePointer<T>.allocate(capacity: 1)
        _ = self.copyBytes(to: UnsafeMutableBufferPointer(start: mem, count: 1))
        let val =  mem.move()
        mem.deallocate(capacity: 1)
        return val
    }
}

A simpler solution would be (from round trip Swift number types to/from Data):

extension Data {
    func castToCPointer<T>() -> T {
        return self.withUnsafeBytes { $0.pointee }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

swift cannot convert value of type 'Plugin.Type' to expected argument type 'MyClass?'

Swift Error: Cannot convert value of type 'ArraySlice' to expected argument type

Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift

Swift cannot convert value of type to expected argument in Realm

Why when converting Float to CGFloat Xcode give me a warning "Cannot convert value of type 'CGFloat' to expected argument type 'UnsafeMutablePointer"?

Swift error: '&' used with non-inout argument of type 'UnsafeMutablePointer'

Cannot convert value of type '()' to expected argument type '() -> ()'

Swift closure: Cannot convert value of type '(_) -> Bool' to expected argument type

Cannot invoke initializer for type 'UnsafeMutablePointer'

UnsafeMutablePointer to expected argument type UnsafeMutablePointer<_> in Swift 3

Swift generics error: Cannot convert value of type 'Type<T>' to expected argument type 'Type<_>'

Swift Generics: Cannot convert value of type to expected argument type

Swift 4: Cannot convert value of type '(_) -> ()' to expected argument type '() -> ()' or Argument passed to call that takes no arguments

Swift generic func cannot convert value of type to expected argument type

Swift category: "Cannot convert value of type to expected argument type 'AnyObject!'

Swift: Cannot convert value of type 'NSDate' to expected argument type 'NSDateComponents'

Cannot convert value of type '[String]?' to expected argument type 'String' Swift

Cannot invoke 'enumerateObjects' with an argument list of type '((AnyObject!, NSInteger, UnsafeMutablePointer<ObjCBool>) -> ())'

Swift 3.0 cannot convert value of type (_, _)->() to Expected argument type 'ObjectsOrErrorBlock'

Cannot convert value of type [()] to expected argument type ()

Cannot convert value of type () to expected argument type bool (Swift)?

swift Cannot convert value of type 'in_addr_t' (aka 'UInt32') to expected argument type 'UnsafeMutablePointer<in_addr_t>!'

Cannot convert value of type 'UserInfoEntity!.Type' to expected argument type 'NSEntityDescription'

swift cannot convert value of type (_, _) -> _ to expected argument type '((_, CGFloat)) -> _

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

Cannot convert value of type [Type] to expected argument type 'some View'

Swift - Cannot convert value of type 'Any?' to expected argument type 'String'

Cannot convert value of type 'UnsafePointer<UnsafeMutablePointer<Float>>?' to expected argument type 'UnsafePointer<Float>?'

Swift Cannot convert value of type '()' to expected argument type '() -> Void'