Converting Integer to Character in Swift [duplicate] - swift

This question already has answers here:
How to convert an Int to a Character in Swift
(9 answers)
Closed 2 years ago.
How can i convert an integer say 0 to a , 1 to b , 2 to c in Swift . I know in C++ and Python but could not find a straight forward way in swift
C++ code below
char a = 0 + 97;
char b = 1 + 97;

You can achieve the same thing in Swift like this:
let a = Character(UnicodeScalar(0 + 97))
let b = Character(UnicodeScalar(1 + 97))

Related

Swift way to C-style for loop that alters initial variable? [duplicate]

This question already has answers here:
is there anyway to increment exponentially in swift?
(4 answers)
Closed 2 years ago.
In many languages a for loop is able to alter i like the below. This for loop will double i each loop around.
for(int i = 0; i < total; i = i + i){}
Swift has for var i in stride(from: 0, to: total, by: N)
and for i in 0..<total
but neither allows us to alter i like the C style for loop above.
Is there a Swifty way to do this?
The goal is to change the pace each loop around instead of in a constant way like the N in stride.
You're looking for the sequence global function. The equivalent of your
for(int i = 0; i < total; i = i + i)
(I assume you meant 1, not 0, since otherwise you'd just get 0 forever, eh?) is:
let total = 100 // let's say
let seq = sequence(first:1) {$0 >= 100 ? nil : $0 + $0}
for i in seq {
print(i)
}
Output:
1
2
4
8
16
32
64
128

Why is x? = y valid syntax in Swift? [duplicate]

This question already has answers here:
Swift optionals - Why does var a:Int? a? = 4 return nil
(2 answers)
Closed 3 years ago.
I am surprised that this compiles
var x: Int? = 3
x? = 5
This seems to do the same thing as x = 5, but it doesn't make sense to me that this would be allowed at all. Would it ever behave differently (like if x was a different type, or if it were a property)?
When you make x optional
x? = 5
if x originally is nil then the line won't run , otherwise it'll act as x = 5

Xcode Swift 3 ~ Make speed into an integer [duplicate]

This question already has answers here:
Convert Float to Int in Swift
(15 answers)
Closed 5 years ago.
I understand that Int() is how to make something and integer but this doesn't work when using it with speed. Here is my code:
var speed: CLLocationSpeed = CLLocationSpeed()
speed = location.speed * 2.23694
if location.speed < 1 {
speedLabel.text = "0"
}
else {
speedLabel.text = "\(speed)"
}
Just wondering how to make my speed (mph) into an integer because currently I am getting 2 decimal points which make my app look messy. Thanks in advance
Use a rounding function. In action:
22> round (1.6)
$R10: Double = 2
23> Int(round (1.6))
$R11: Int = 2

How to parse a String like "14px" to get Int value 14 in Swift [duplicate]

This question already has answers here:
Swift How to get integer from string and convert it into integer
(10 answers)
Closed 6 years ago.
I receive from a server API a String like "14px". How to transform this String to Int with value 14 ignoring substring "px"?
If string always come with just px at last you can subscript it and ignore the last 2 characters.
let str = "14px"
var numStr = str.substring(to: str.index(name.endIndex, offsetBy: -2))
print(numStr) // "14"
//Now you can convert "14" to Int
print(Int(numStr))
Or
print(Int(String(str.characters.dropLast(2))))

Matlab, how to convert a string of integers into a vector? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
convert string to number array in matlab
Is there a simple way in Matlab to convert a string like this
'123456789'
into a vector like this ?
[1 2 3 4 5 6 7 8 9]
If all you have is contiguous characters from 0 to 9:
v = double(s)-'0';
double(s) converts a string into an array where each element is the ASCII code of the corresponding character. To obtain the numberic values we subtract '0' (which is in fact 48 in ASCII) and since digits have a sequential representation in ASCII code ('1' = 49, '2' = 50, etc.) we end up with intended result.
one way would be using regexp for this. But of course it only works for single digit numbers.
>> str = '123456789';
>> num = regexp(str,'\d')
num =
1 2 3 4 5 6 7 8 9