Swift - Snippets: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
 
Zeile 46: Zeile 46:
 
== Extensions ==
 
== Extensions ==
 
  [[Swift - View Extensions]]
 
  [[Swift - View Extensions]]
 
== Klassen ==
 
=== Klassen vergleichen ===
 
How can I check if two instances of a class are the same
 
 
In Swift, you can check if two instances of a class are the same by using the equality operator (==) or the identity operator (===).
 
 
The equality operator == checks if two instances have the same values for their properties. In order for this to work, the class must conform to the Equatable protocol and implement the == operator:
 
 
<syntaxhighlight lang="swift">
 
 
class MyClass: Equatable {
 
    let property: Int
 
 
    static func == (lhs: MyClass, rhs: MyClass) -> Bool {
 
        return lhs.property == rhs.property
 
    }
 
}
 
 
let instance1 = MyClass(property: 1)
 
let instance2 = MyClass(property: 1)
 
let isEqual = instance1 == instance2 // true
 
</syntaxhighlight>
 
 
The identity operator === checks if two instances refer to the same memory address. It checks if two instances are the same instance.
 
 
<syntaxhighlight lang="swift">
 
 
let instance1 = MyClass(property: 1)
 
let instance2 = MyClass(property: 1)
 
let isIdentical = instance1 === instance2 // false
 
</syntaxhighlight>
 
Note: It is recommended to use the === operator if you need to check if two instances are the same instance, and use the == operator if you need to check if two instances have the same values for their properties.
 

Aktuelle Version vom 3. Februar 2023, 20:50 Uhr

Links[Bearbeiten]

Swift (Programmiersprache)
https://www.swift.org/

Random / Range[Bearbeiten]

Mit Random erzeugt man Zufallszahlen. Mit dem Range Operator kann man Zahlenbereiche definieren.

let score = Int.random(in: 1...10)
let rating = Int.random(in: 1..<10)
let floatNumber = Float.random(in: 1...10)
let bool = Bool.random(in: 1...10)

Time[Bearbeiten]

Swift - delay code execution

Sound[Bearbeiten]

Swift - Play a Sound

Gestures and Motions[Bearbeiten]

Swift - Shake Motion

String Manipulation[Bearbeiten]

Swift - get file extension
Swift - Strings als Double validieren

Files[Bearbeiten]

How can I get the filename without the extension?[Bearbeiten]

In Swift, you can get the file name without the extension by using the lastPathComponent property of the URL class and then removing the extension using the deletingPathExtension method. Here is an example:

let fileURL = URL(fileURLWithPath: "/path/to/file.txt")
let fileNameWithoutExtension = fileURL.deletingPathExtension().lastPathComponent
print(fileNameWithoutExtension) // Output: "file"

You can also achieve the same result using the NSString API, here's an example:

let filePath = "/path/to/file.txt"
let fileNameWithoutExtension = (filePath as NSString).deletingPathExtension
print(fileNameWithoutExtension) // Output: "file"

JSON in Swift[Bearbeiten]

Swift & JSON

Farben[Bearbeiten]

UIColor Erweiterung für Hexadezimalwerte

Extensions[Bearbeiten]

Swift - View Extensions