Understanding the Swift Grammar for Converting Numbers into Strings Using Backslashes

Asked 2 years ago, Updated 2 years ago, 30 views

Swift seems to be able to convert Float variables into strings with the following code:

let testArray: Float = [0.1, 0.2, 0.3]
var message=""
message.append("test-------\(testArray[0]), \(testArray[1]), \(testArray[2])|")

print(message)

However, I am not sure what grammar this code works with.
Why can I use backslash and parentheses to convert numbers into strings?

Also, with this code, do some devices have problems?
I've just received a report that my code doesn't work in some environments.
I'm trying to find out where the cause is.
Do you think that if you use this kind of grammar, it may not work in some environments?

swift

2022-09-30 14:50

2 Answers

However, I am not sure what grammar this code works with.

There is already a link in the comment, but it is also a function called String Interpolation, which is clearly stated in the Swift book.

Also, I think it's very difficult not to have a proper Japanese translation, but if you're programming on Swift, you should read Swift books at least once.In addition to the web version, there is also an iBooks version.

Why can I use backslash and parentheses to convert numbers into strings?

This is because the Swift compiler and Swift's String type are configured.More specifically, the Swift compiler has been created to say that when the Swift compiler finds the literal notation for string interpolation "...\(...)", it decomposes part by part and then invokes the method defined by ExpressibleByStringInterpretation.

For the String type, you can convert any non-numeric expression into a string, no matter what type of expression you write in \(...).(You should not rely on conversion results that are not specified in the document.)

Also, with this code, do some devices have problems?

Do you think that if you use this kind of grammar, it may not work in some environments?

If you explain the proper behavior as described above, it's not possible because it's so complicated internally that you can write a single book, but string interpolation itself is processed in a common part of all Swift processing systems, so it's hard to think of a code like this.

I can't say anything more in the simple case described in your question.First of all, it would be better to understand the conditions under which the event is reproduced, and then ask again with sufficient information.If I were you, I would think that the code is written that is environmentally dependent and not directly related to using these grammar

and look for the cause.


2022-09-30 14:50

How variables in \() are converted to strings (String type) is defined by the property description of the protocol CustomStringConvertible.

CustomStringConvertible

structure Hello:CustomStringConvertible{
    var name —String
    
    var description: String {
        return "Hello, " + name
    }
}

print("Say\(Hello(name: "World")") // Say Hello, World

Now I've been told that my code doesn't work in some environments, so I'm trying to figure out where the cause is.

I think we need to find out if the type of variable in question complies with CustomStringConvertible or if the property description is defined properly.


2022-09-30 14:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.