Swift - Strings: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „== Links == Swift Programmiersprache == String - Snippets == === String(format:) === The String(format:) initializer is used to create a new string by fo…“)
 
 
(7 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 
== Links ==
 
== Links ==
  [[Swift Programmiersprache]]
+
  [[Swift (Programmiersprache)]]
  
 
== String - Snippets ==
 
== String - Snippets ==
=== String(format:) ===
+
=== Strings formatieren mit String(format:) ===
 
The String(format:) initializer is used to create a new string by formatting a set of variables using a format string.
 
The String(format:) initializer is used to create a new string by formatting a set of variables using a format string.
  
Zeile 9: Zeile 9:
  
 
For example, the following code creates a string that contains a placeholder for an integer value:
 
For example, the following code creates a string that contains a placeholder for an integer value:
<syntaxhighlight lang="Swift">
+
<syntaxhighlight lang="swift">
 
let value = 42
 
let value = 42
 
let string = String(format: "The value is %d", value)
 
let string = String(format: "The value is %d", value)
Zeile 25: Zeile 25:
  
 
In this example, the %@ placeholder is replaced with the value of the name variable, and the %d placeholder is replaced with the value of the age variable. The resulting string is then printed to the console.
 
In this example, the %@ placeholder is replaced with the value of the name variable, and the %d placeholder is replaced with the value of the age variable. The resulting string is then printed to the console.
 +
 +
<syntaxhighlight lang="Swift">
 +
let value: Float = 3.14159265
 +
let formattedString = String(format: "%.2f", value)
 +
</syntaxhighlight>
 +
 +
=== Data Objekte in Strings umwandeln ===
 +
Data Objekte kann man oft nicht direkt verwenden (z.B. beim JSON Decoding) Deshalb muss man sie manchmal erst umwandeln.
 +
 +
<syntaxhighlight lang="swift">
 +
if let safeData = data { // Optional Binding = Safety Belt für Optionals
 +
  let dataString = String(data: safeData, encoding: .utf8) // String aus Datenobjekt
 +
  print("received data: \(String(describing: dataString))") // String ausgeben
 +
}
 +
</syntaxhighlight>

Aktuelle Version vom 19. Januar 2023, 11:55 Uhr

Links[Bearbeiten]

Swift (Programmiersprache)

String - Snippets[Bearbeiten]

Strings formatieren mit String(format:)[Bearbeiten]

The String(format:) initializer is used to create a new string by formatting a set of variables using a format string.

The format string is a string that contains placeholders for the variables that you want to insert into the string. These placeholders are represented using special syntax, such as %d for integers, %f for floating point numbers, and %@ for objects.

For example, the following code creates a string that contains a placeholder for an integer value:

let value = 42
let string = String(format: "The value is %d", value)
print(string)  // Output: "The value is 42"

In this example, the %d placeholder in the format string is replaced with the value of the value variable, which is 42. The resulting string is then printed to the console.

You can use multiple placeholders in the format string to include multiple variables in the string. For example:

let name = "Alice"
let age = 30
let string = String(format: "%@ is %d years old", name, age)
print(string)  // Output: "Alice is 30 years old"

In this example, the %@ placeholder is replaced with the value of the name variable, and the %d placeholder is replaced with the value of the age variable. The resulting string is then printed to the console.

let value: Float = 3.14159265
let formattedString = String(format: "%.2f", value)

Data Objekte in Strings umwandeln[Bearbeiten]

Data Objekte kann man oft nicht direkt verwenden (z.B. beim JSON Decoding) Deshalb muss man sie manchmal erst umwandeln.

if let safeData = data { // Optional Binding = Safety Belt für Optionals
  let dataString = String(data: safeData, encoding: .utf8) // String aus Datenobjekt
  print("received data: \(String(describing: dataString))") // String ausgeben
}