emoji cannot be a literal and what else? - swift

A literal is the source code representation of a value of a type, such as a number or string
There are 3 kinds of literals in Swift: Integer Literals, Floating-Point Literals and String Literals (please correct me if I'm wrong), Is that means (My Guess) any elements which not belong to a type of Integer, Floating or String is not considered as a literal, and will trigger an error when used as literals
According to what I guess I've tried this let aEmoji = πŸ˜€
Question1: Is my guess accurate? If not, I appreciate you could correct me.
Question2: Is there anything else shouldn't use as a literal? (would be nice you could give me some example)
Thanks

A string literal is wrapped in double quotes
let aEmoji = "πŸ˜€"
From the documentation:
A string literal is a fixed sequence of textual characters
surrounded by a pair of double quotes ("").

Yes, anything that isn't an integer literal (1), floating-point literal (1.0) or String literal ("foo"), Array literal ([foo]), Dictionary literal ([foo : bar]), bool literal (true/false) isn't a literal and would cause an error.
Anything that isn't one of the literals above isn't a literal, and could cause an error (if it's an invalid syntax).
You can make put an emoji in a string literal, however: let aEmoji = "πŸ˜€"

You can include emojis in a literal String or Character expression by setting it off with double quotes.
The type inferrer will default the expression to a String literal, unless the Character type is specified.
let unicornString = "πŸ¦„"
let unicornChar : Character = "πŸ¦„"
Else the compiler will treat the emoji (or any unicode character sequence) as an identifier (because emoji can be variable names and such).
The following would be valid:
let πŸ”‘ = "myPassword"
user.authenticateWithPassword(πŸ”‘)

Related

How do I parse out a number from this returned XML string in python?

I have the following string:
{\"Id\":\"135\",\"Type\":0}
The number in the Id field will vary, but will always be an integer with no comma separator. I'm not sure how to get just that value from that string given that it's string data type and not real "XML". I was toying with the replace() function, but the special characters are making it more complex than it seems it needs to be.
is there a way to convert that to XML or something that I can reference the Id value directly?
Maybe use a regular expression, e.g.
import re
txt = "{\"Id\":\"135\",\"Type\":0}"
x = re.search('"Id":"([0-9]+)"', txt)
if x:
print(x.group(1))
gives
135
It is assumed here that the ids are numeric and consist of at least one digit.
Non-regex answer as you asked
\" is an escape sequence in python.
So if {\"Id\":\"135\",\"Type\":0} is a raw string and if you put it into a python variable like
a = '{\"Id\":\"135\",\"Type\":0}'
gives
>>> a
'{"Id":"135","Type":0}'
OR
If the above string is python string which has \" which is already escaped, then do a.replace("\\","") which will give you the string without \.
Now just load this string into a dict and access element Id like below.
import json
d = json.loads(a)
d['Id']
Output :
135

Swift-How to write a variable or a value that is changing between parenthesis

(in swift language) For example " A + D " I want the string A to stay all the time but the value of D changes depending on let's say Hp, so when Hp is fd the string will be "A + fd" and etc
I mean like( "A + %s" % Hp ) for the string in python. Such as here: What does %s mean in Python?
If you are talking about %s, then it's a c-style formatting key, which awaits string variable or value in the list of arguments. In Swift, you compose strings using "\(variable)" syntax, which is called String interpolation, as explained in the documentation:
String Interpolation
String interpolation is a way to construct a new String value from a
mix of constants, variables, literals, and expressions by including
their values inside a string literal. You can use string interpolation
in both single-line and multiline string literals. Each item that you
insert into the string literal is wrapped in a pair of parentheses,
prefixed by a backslash ():
Source: official documentation
Example:
var myVar = "World"
var string = "Hello \(myVar)"
With non-strings:
let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// Output: message is "3 times 2.5 is 7.5"

How can I convert a single Character type to uppercase?

All I want to do is convert a single Character to uppercase without the overhead of converting to a String and then calling .uppercased(). Is there any built-in way to do this, or a way for me to call the toupper() function from C without any bridging? I really don't think I should have to go out of my way for something so simple.
To call the C toupper() you need to get the Unicode code point of the Character. But Character has no method for getting its code point (a Character may consist of multiple code points), so you have to convert the Character into a String to obtain any of its code points.
So you really have to convert to String to get anywhere. Unless you store the character as a UnicodeScalar instead of a Character. In this case you can do this:
assert(unicodeScalar.isASCII) // toupper argument must be "representable as an unsigned char"
let uppercase = UnicodeScalar(toupper(CInt(unicodeScalar.value)))
But this isn't really more readable than simply using String:
let uppercase = Character(String(character).uppercased())
just add this to your program
extension Character {
//converts a character to uppercase
func convertToUpperCase() -> Character {
if(self.isUppercase){
return self
}
return Character(self.uppercased())
}
}

Convert Character to Integer in Swift

I am creating an iPhone app and I need to convert a single digit number into an integer.
My code has a variable called char that has a type Character, but I need to be able to do math with it, therefore I think I need to convert it to a string, however I cannot find a way to do that.
In the latest Swift versions (at least in Swift 5) there is a more straighforward way of converting Character instances. Character has property wholeNumberValue which tries to convert a character to Int and returns nil if the character does not represent and integer.
let char: Character = "5"
if let intValue = char.wholeNumberValue {
print("Value is \(intValue)")
} else {
print("Not an integer")
}
With a Character you can create a String. And with a String you can create an Int.
let char: Character = "1"
if let number = Int(String(char)) {
// use number
}
The String middleman type conversion isn’t necessary if you use the unicodeScalars property of Swift 4.0’s Character type.
let myChar: Character = "3"
myChar.unicodeScalars.first!.value - Unicode.Scalar("0")!.value // 3: UInt32
This uses a trick commonly seen in C code of subtracting the value of the char ’0’ literal to convert from ascii values to decimal values. See this site for the conversions: https://www.asciitable.com
Also there are some implicit unwraps in my answer. To avoid those, you can validate that you have a decimal digit with CharacterSet.decimalDigits, and/or use guard lets around the first property. You can also subtract 48 directly rather than converting ”0” through Unicode.Scalar.

Need code for removing all unicode characters in vb6

I need code for removing all unicode characters in a vb6 string.
If this is UTF-16 text (as normal VB6 String values all are) and you can ignore the issue of surrogate pairs, then this is fairly quick and reasonably concise:
Private Sub DeleteNonAscii(ByRef Text As String)
Dim I As Long
Dim J As Long
Dim Char As String
I = 1
For J = 1 To Len(Text)
Char = Mid$(Text, J, 1)
If (AscW(Char) And &HFFFF&) <= &H7F& Then
Mid$(Text, I, 1) = Char
I = I + 1
End If
Next
Text = Left$(Text, I - 1)
End Sub
This has the workaround for the unfortunate choice VB6 had to make in returning a signed 16-bit integer from the AscW() function. It should have been a Long for symmatry with ChrW$() but it is what it is.
It should beat the pants off any regular expression library in clarity, maintainability, and performance. If better performance is required for truly massive amounts of text then SAFEARRAY or CopyMemory stunts could be used.
Public Shared Function StripUnicodeCharactersFromString(ByVal inputValue As String) As String
Return Regex.Replace(inputValue, "[^\u0000-\u007F]", String.Empty)
End Function
Vb6 - not sure will
sRTF = "\u" & CStr(AscW(char))
work? - You could do this for all char values above 127
StrConv is the command for converting strings.
StrConv Function
Returns a Variant (String) converted as specified.
Syntax
StrConv(string, conversion, LCID)
The StrConv function syntax has these named arguments:
Part Description
string Required. String expression to be converted.
conversion Required. Integer. The sum of values specifying the type of conversion to perform. `128` is Unicode to local code page (or whatever the optional LCID is)
LCID Optional. The LocaleID, if different than the system LocaleID. (The system LocaleID is the default.)