ASCII-to-character conversion issues in Swift

Source: Internet
Author: User

We're dealing with characters in C + +, which is usually the case

char a = ‘A‘ // A = 65printf("‘%c‘ = %d", a + 1, a + 1) // ‘B‘ = 66

This does not work in Swift, which is known to absorb all the advantages of C + +, because
Character and INT cannot be computed and will be error-

var c:Character = "A"c + 1 // 这里是错误的Xcode 会告诉你 Binary operator ‘+‘ cannot be applied to operands of type ‘Character‘ and ‘Int‘

So what's not in the textbook is a problem.

There's no internet.

But the textbook says an example has an String.unicodeScalars array of ASCII (or Unicode) values that can return a string.

So UnicodeScalar

This class was found.

The character "A" can be expressed in this way

let A = Character(UnicodeScalar(65))

It's worth 65 to be able to do so

let A_ASCII = UnicodeScalar("A") // 65

ASCII-to-character conversion issues in Swift

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.