How to convert a type of "any" in kind "UnsafeMutablePointer<Int16>" in a swift 3

Feanon

I updated my xcode for the latest beta and there was such a problem. Before my code worked well in objective-c:

short * pointers = (short *)[[params valueForKey:@"Pointers"] pointerValue];

Then I translated part of the code on the swift language:

let pointers = UnsafeMutablePointer<Int16>(((params["Pointers"] as AnyObject).pointerValue)!)

Here, too, everything is good. But this code is not working on the Swift 3. Tell how you can implement on a swift 3. Thank You!

Martin R

The correct way of converting a pointer to UInt16 which is wrapped in an NSValue to UnsafeMutablePointer<Int16> in Swift 3 is

if let ptr = value.pointerValue {
    let u16ptr = ptr.assumingMemoryBound(to: UInt16.self)
    // ...
} else {
    // pointer is nil
}

In your case it should be

if let ptr = (params["Pointers"] as? NSValue)?.pointerValue {
    let u16ptr = ptr.assumingMemoryBound(to: UInt16.self)
    // ...
} else {
    // value does not exist, is not an `NSValue`, or pointer is nil
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert Any to CGFloat in Swift 3

How to convert any kind of white space to a char?

In Swift, how to convert a dictionary with any-type values to a JSON string

Swift: Cannot convert Type<Subtype> to Type<Any>

Swift 3: Cannot convert value of type 'NSMutableDictionary' to expected argument type '[AnyHashable : Any]!'

Scala 3. Kind polymorphism and AnyKind type - any code example?

How to convert Any to Int in swift?

How to fix 'Cannot convert value of type '[Any]' to type 'String' in coercion' error in swift

How to download a result of a fileInput element as a file for any kind of file type?

Convert "Any" array to String in swift 3

how to convert array object into array csv kind of type

Swift: Cannot convert value of type '[Any]' to specified type 'NSString'

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

How do I support this kind of init in swift struct/class type?

How to check whether an object is kind of a dynamic class type in swift?

Swift 3 Type 'Any' has no subscript members

swift 3: Type 'Any?' has no subscript members

swift 3 Type 'Any' has no subscript members?

Type any has no subscript error in swift 3

convert [Dictionary<String,Any?>] to [Dictionary<String,Any!>] ? swift 3

How to call Swift functions of type Any with args of type [Any]

How to convert any textfield input to float in Swift

How to convert any type into String in Julia

How to convert byte array to any type

How to convert from value with Any type to Real?

how to convert python inferred type 'any' in java

IOS/Swift: How to indicate the first position of the $0 in the .firstIndex ? 'Cannot convert value of type 'Any' to expected argument type 'String''

Swift 3 : Convert Dictionary of String type to Int

How to convert [Dictionary<String, Any>] to [String: Any] in swift