How to convert Int8 to Character? - swift

I need to convert Int8 to Character. How can I do it?
I tried use UnicodeScalar
var result += Character(UnicodeScalar(data[i]))
... but got this error:
Cannot invoke initializer for type 'UnicodeScalar' with an argument
list of type (Int8)

Unicode.Scalar can be initalized just with paramaters of certain types which you can find in docs.
I would suggest you using init(_:) which takes UInt8 which tells that given number is positive (this is required for creating UnicodeScalar). So, you can try to cast your Int8 to UInt8 and then your initializer takes parameter of correct type
let int: Int8 = data[i]
if let uint = UInt8(exactly: int) {
let char = Character(UnicodeScalar(uint))
}

Maybe you need just convert whole data to a string:
var result = String(data: data, encoding: .ascii)

Related

Swift --- Convert Char to Int in a for in loop

I'd like to convert each char in a string for-in loop, but failed
Here's my code
let stringImported: String = "12345678"
for char in stringImported {
print(Int(char))
}
Get this error
Initializer 'init(_:)' requires that 'String.Element' (aka 'Character') conform to 'BinaryInteger'
There is no Int initializer that takes a Character but Character has a property called wholeNumberValue which returns an optional Int:
let stringImported = "12345678"
for char in stringImported {
print(char.wholeNumberValue ?? "nil")
}
UPDATE: Int cannot operate over chars, it has to be converted to String. Code snippet change to reflect that.
The cast toInt does not guarantee that the conversion will be successful. You need to provide a default value:
let stringImported: String = "12345678"
for char in stringImported {
print("\(Int(String(char)) ?? 0)")
}
Convert Character to String first and then convert it to Int will success
let stringImported: String = "12345678"
for char in stringImported {
print(Int(String(char)))
}
also check this stack overflow question and answer.
Maybe this question is duplicated
Convert Character to Integer in Swift

conversion of Int value String using user defaults having facing an error

UserInfoDefault.saveUserType(user: String(self.type))
I have to convert a type value that is Int and I have to convert it into String value to use it into the User defaults... But facing an issue :
Cannot invoke initializer for type 'String' with an argument list of type '(Int?)'
Note the question mark, there is no init method for String that takes an optional Int. You need to unwrap it first.
let value = myOptionalInt ?? 0
let stringValue = String(value)
or use if let or guard depending on what suits your case the best

"Type of expression is ambiguous without more context" when creating UInt16 array from Bluetooth characteristic

I am using Xcode 9.2 and I don't understand the reason behind the error
Type of expression is ambiguous without more context
I am getting some input when trying to create and wordArray as showed below. If I define it as UInt8 array it does work but not if I do as Uint16 since I get the error.
The original data, Characteristic.value comes from a BLE characteristic
let characteristicData = Characteristic.value
let byteArray = [UInt8](characteristicData!)
print("\(Characteristic.uuid) value as byte is->",byteArray)
let wordArray = [UInt16](characteristicData!)//Type of expression is ambiguous without more context
print("\(Characteristic.uuid) value as word is->",wordArray)
Why does this happen and how I can fix it?
characteristicData has the type Data and that conforms to the
(RandomAccess)Collection protocol with UInt8 as element type, that's why you can
initialize an [UInt8] array from it:
let byteArray = [UInt8](characteristicData)
You could equivalently write
let byteArray = Array(characteristicData)
To interpret the data as an array of a different type, use
the generic
func withUnsafeBytes<ResultType, ContentType>(_ body: (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType
method:
let wordArray = characteristicData.withUnsafeBytes {
[UInt16](UnsafeBufferPointer(start: $0, count: characteristicData.count/2))
}
Here the ContentType is inferred automatically as UInt16.

Convert UnsafeMutablePointer<UInt> to UInt in Swift

I got from a function Swift result in type UnsafeMutablePointer<UInt>
Can I cast it to UInt?
Just use the memory property to access the underlying data.
let ptr: UnsafeMutablePointer<UInt> = funcReturningMutablePtr()
let theValue: UInt = ptr.memory
The type annotations are for clarity, but are not necessary.

Swift: Convert Int16 to Int32 (or NSInteger)

I'm really stuck! I'm not an expert at ObjC, and now I am trying to use Swift. I thought it would be much simpler, but it wasn't. I remember Craig said that they call Swift “Objective-C without C”, but there are too many C types in OS X's foundation. Documents said that many ObjC types will automatically convert, possibly bidirectionally, to Swift types. I'm curious: how about C types?
Here's where I'm stuck:
//array1:[String?], event.KeyCode.value:Int16
let s = array1[event.keyCode.value]; //return Int16 is not convertible to Int
I tried some things in ObjC:
let index = (Int) event.keyCode.value; //Error
or
let index = (Int32) event.keyCode.value; //Error again, Swift seems doesn't support this syntax
What is the proper way to convert Int16 to Int?
To convert a number from one type to another, you have to create a new instance, passing the source value as parameter - for example:
let int16: Int16 = 20
let int: Int = Int(int16)
let int32: Int32 = Int32(int16)
I used explicit types for variable declarations, to make the concept clear - but in all the above cases the type can be inferred:
let int16: Int16 = 20
let int = Int(int16)
let int32 = Int32(int16)
This is not how this type of casting works in Swift. Instead, use:
let a : Int16 = 1
let b : Int = Int(a)
So, you basically instantiate one variable on base of the other.
In my case "I was trying to convert core data Int16 to Int" explicit casting didn't worked. I did this way
let quantity_string:String = String(format:"%d",yourInt16value)
let quantity:Int = Int(quantity_string)