Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

key path expression as functions ios swift

struct Car {
    var name: String
    var kind: String
}


let keyPath = Car.name // Create key path
        
var audi = Car(name: "Audi", kind: "luxury") // Create instance of class or struct

print(audi[keyPath: keyPath]) // Access the value of instance
audi[keyPath: keyPath] = "Audi A8 L" // Update the value of instance
print(audi[keyPath: keyPath])

print(audi[keyPath: .kind]) // Short hand version of key path

let cars = [
  audi,
  Car(name: "Škoda", kind: "comfort")
]

let carNameList = cars.map(.name) // in Swift 5.2 we can pass key path as function

print(carNameList)


/* Output will be 
Audi
Audi A8 L
luxury
["Audi A8 L", "Škoda"]
*/
Comment

PREVIOUS NEXT
Code Example
Swift :: ternary operator in swift 
Swift :: get files with file type swift 
Swift :: Swift Add Elements to a Set 
Swift :: library not found for -lalan-sdk-react-native 
Swift :: Type Constraints in Swift Generics 
Swift :: swift increment for loop by 2 
Swift :: xcode enable a button after a text field is filled 
Swift :: spilit string in swift 
Swift :: load plist swift 
Swift :: swift search bar 
Swift :: allowed filename characters swift 
Ruby :: ruby file extension 
Ruby :: ruby remove duplicates from array 
Ruby :: rails generate model 
Ruby :: ruby get substring between two characters 
Ruby :: rails link_to class 
Ruby :: ruby hash transform keys 
Ruby :: ruby 2 decimal 
Ruby :: ruby connect database 
Ruby :: ruby hash merge 
Ruby :: ruby test is number 
Ruby :: uper case in ruby 
Ruby :: contain .where rails 
Ruby :: ruby for each continue 
Ruby :: ruby open file and append 
Ruby :: rails image disappears after showing 
Ruby :: ruby join hash to string 
Ruby :: infinite loop in ruby 
Ruby :: rails migration column types 
Ruby :: Rails checkbox checked/unchecked values 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =