unsafe-pointers

UnsafeMutablePointer<CFTypeRef> in Swift 3

淺唱寂寞╮ 提交于 2019-12-01 19:01:53
问题 I'm attempting to call SecItemCopyMatching in my keychain utility class in order to get data out of the keychain, yet I'm running into a problem with getting the result argument, UnsafeMutablePointer<CFTypeRef?> . The original statement (in Swift 2, before migrating to Swift 3) was // query is a dictionary of [String : AnyObject] var result: Data? let status = withUnsafeMutablePointer(to: &result) { SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0)) } But in Swift 3, you are

UnsafeMutablePointer<CFTypeRef> in Swift 3

与世无争的帅哥 提交于 2019-12-01 18:29:22
I'm attempting to call SecItemCopyMatching in my keychain utility class in order to get data out of the keychain, yet I'm running into a problem with getting the result argument, UnsafeMutablePointer<CFTypeRef?> . The original statement (in Swift 2, before migrating to Swift 3) was // query is a dictionary of [String : AnyObject] var result: Data? let status = withUnsafeMutablePointer(to: &result) { SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0)) } But in Swift 3, you are now required to call .withMemoryRebound in order to view memory. Based on what Xcode tells you to do,

Cast to different C struct unsafe pointer in Swift

跟風遠走 提交于 2019-12-01 08:29:16
I want to call the Posix socket functions socket and bind from Swift. socket is pretty easy—it takes Int32 s, but bind is causing a problem, because I have a sockaddr_in pointer, but it wants a sockaddr pointer. In C, this would be a cast, like: bind(sock, (struct sockaddr *)&sockAddress, sizeof(sockAddress)) Here's an attempt in Swift: let sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) var sockAddress = sockaddr_in() bind(sock, &sockAddress, UInt32(MemoryLayout<sockaddr_in>.size)) The bind line fails to compile with: cannot convert value of type 'sockaddr_in' to expected argument type

How do I cast an __NSMallocBlock__ to its underlying type in Swift 3?

北城余情 提交于 2019-11-30 05:12:41
问题 I had a trick to help test UIAlertController that worked in Swift 2.x: extension UIAlertController { typealias AlertHandler = @convention(block) (UIAlertAction) -> Void func tapButtonAtIndex(index: Int) { let block = actions[index].valueForKey("handler") let handler = unsafeBitCast(block, AlertHandler.self) handler(actions[index]) } } This fails under Swift 3.x with fatal error: can't unsafeBitCast between types of different sizes , which tempts me to believe there might be a way to make the

Extension for Generic Type `UnsafeMutablePointer<UInt8>`

杀马特。学长 韩版系。学妹 提交于 2019-11-29 12:44:18
I'd like to create an extension for UnsafeMutablePointer that only affects UnsafeMutablePointer<UInt8> ... I understand that these instructions are pertinent, but I'm not sure how: When you extend a generic type, you do not provide a type parameter list as part of the extension’s definition. Instead, the type parameter list from the original type definition is available within the body of the extension, and the original type parameter names are used to refer to the type parameters from the original definition. Basically, I'm trying to use this method: func toSwift(length: Int) -> [Int] { var

Converting an UnsafePointer with length to a Swift Array type

不打扰是莪最后的温柔 提交于 2019-11-29 02:53:46
I'm looking for the simplest ways to achieve reasonable C interoperability in Swift, and my current block is converting an UnsafePointer<Int8> (which was a const char * ), into an [Int8] array. Currently, I have a naïve algorithm that can take an UnsafePointer and a number of bytes and converts it to an array, element by element: func convert(length: Int, data: UnsafePointer<Int8>) { let buffer = UnsafeBufferPointer(start: data, count: length); var arr: [Int8] = [Int8]() for (var i = 0; i < length; i++) { arr.append(buffer[i]) } } The loop itself can be sped up by using arr.reserveCapacity

Swift 3 UnsafePointer($0) no longer compile in Xcode 8 beta 6

╄→尐↘猪︶ㄣ 提交于 2019-11-28 19:04:08
My code snipet as follows …: let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) { SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)) } … does no longer compile with the following error which I don't understand: "'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type." What to do to fix it? From the Release Notes of Xcode 8 beta 6: An Unsafe[Mutable]RawPointer type has been introduced, replacing Unsafe[Mutable]Pointer<Void> . Conversion from UnsafePointer<T> to UnsafePointer<U> has been disallowed.

Swift UnsafeMutablePointer<Unmanaged<CFString>?> allocation and print

放肆的年华 提交于 2019-11-28 09:26:58
I'm new to swift and I have some difficulties to deal with pointers of unmanaged CFString (or NSString). I'm working on a CoreMIDI project that implies usage of UnsafeMutablePointer?> as you can see in this function : func MIDIObjectGetStringProperty(_ obj: MIDIObjectRef, _ propertyID: CFString!, _ str: UnsafeMutablePointer<Unmanaged<CFString>?>) -> OSStatus My problem is that I want to allocate a buffer to receive the content of the property (_str) then call the function above, and finally print the content in the console by using println. At the moment I wrote this : // Get the first midi

Extension for Generic Type `UnsafeMutablePointer<UInt8>`

拟墨画扇 提交于 2019-11-28 06:36:28
问题 I'd like to create an extension for UnsafeMutablePointer that only affects UnsafeMutablePointer<UInt8> ... I understand that these instructions are pertinent, but I'm not sure how: When you extend a generic type, you do not provide a type parameter list as part of the extension’s definition. Instead, the type parameter list from the original type definition is available within the body of the extension, and the original type parameter names are used to refer to the type parameters from the

Converting an UnsafePointer with length to a Swift Array type

余生颓废 提交于 2019-11-27 17:26:35
问题 I'm looking for the simplest ways to achieve reasonable C interoperability in Swift, and my current block is converting an UnsafePointer<Int8> (which was a const char * ), into an [Int8] array. Currently, I have a naïve algorithm that can take an UnsafePointer and a number of bytes and converts it to an array, element by element: func convert(length: Int, data: UnsafePointer<Int8>) { let buffer = UnsafeBufferPointer(start: data, count: length); var arr: [Int8] = [Int8]() for (var i = 0; i <