Single quotes with characters in fast

I made C, C ++, Java, and this language taught me that characters are enclosed in single quotes (mostly with the correct syntax), but the lines are duplicated. Is this a quick syntax that allows only characters to be inside single quotes or is there some reasonable reason (logic) behind the suggestion of such a syntax.

let char1: Character = "A" //correct let char2: Character = 'B' //incorrect 
+6
source share
2 answers

The state of compiler technology has changed a lot since the development of the first C compiler. Compilers have become much smarter about things on their own, including the intended type of expressions, without the help of programmers.

Char's output against string literals is one such example. Theoretically, the current structure of C allows us to deduce the type of literal in many contexts. For example, in the code below, the compiler has enough information to handle single-character strings, as if they were character literals:

 void foo(char c); char s[] = "xyz"; // None of the below would compile char a = "a"; foo("b"); if (s[1] == "c") { ... } 

However, at the same time, it was easier to ask the programmer to tell the compiler that "a" , "b" and "c" are actually 'a' , 'b' and 'c' . Moreover, since function prototypes were not introduced until the ANSI C, foo("b") assumption was found in the original version of the K & R language.

The help of a programmer is no longer required when the language has a type inference system, so Swift designers decided to combine the syntax for string and character constants.

+6
source

There is no such thing as a single quote in Swift.

The only way to declare a character is to use the explicit type Character .

 let a: Character = "a" 

You can read more about the Swift lexical structure here https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html

+7
source

All Articles