This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@_functionBuilder | |
struct SubviewsBuilder { | |
static func buildBlock(_ views: UIView...) -> [UIView] { | |
return views | |
} | |
} | |
extension UIView { | |
func addSubviews(@SubviewsBuilder _ subviews: () -> [UIView]) -> UIView { | |
subviews().forEach { addSubview($0) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@dynamicMemberLookup | |
struct Person { | |
subscript(dynamicMember member: String) -> String { | |
let properties = ["name": "Nikita", "age": "24"] | |
return properties[member, default: ""] | |
} | |
} | |
print(Person().name) // Nikita |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol ProtocolWithAssociatedType { | |
associatedtype T | |
} | |
struct Test<T>: ProtocolWithAssociatedType {} // no compile error | |
struct Test: ProtocolWithAssociatedType {} // error: Type 'Test' does not conform to protocol 'ProtocolWithAssociatedType' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func printData(from keyPaths: KeyPath<[String: Any], Any?>...) { | |
keyPaths.forEach { keyPath in | |
print(dict[keyPath: keyPath]) | |
} | |
} | |
printData(from: \.name, \.age) | |
// Optional("Nikita") | |
// Optional(24) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@dynamicMemberLookup | |
protocol DictionaryDynamicLookup { | |
associatedtype Key | |
associatedtype Value | |
subscript(key: Key) -> Value? { get } | |
} | |
extension DictionaryDynamicLookup where Key == String { | |
subscript(dynamicMember member: String) -> Value? { | |
return self[member] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UserDefaults { | |
subscript<T: RawRepresentable>(key: String) -> T? { | |
get { | |
if let rawValue = value(forKey: key) as? T.RawValue { | |
return T(rawValue: rawValue) | |
} | |
return nil | |
} | |
set { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum AppTheme: Int { | |
case light | |
case dark | |
} | |
class SettingsService { | |
var appTheme: AppTheme { | |
get { | |
if let rawValue: AppTheme.RawValue = UserDefaults.standard[#function], let theme = AppTheme(rawValue: rawValue) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UserDefaults { | |
subscript<T>(key: String) -> T? { | |
get { | |
return value(forKey: key) as? T | |
} | |
set { | |
set(newValue, forKey: key) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SettingsService { | |
var isNotificationsEnabled: Bool { | |
get { | |
let isEnabled = UserDefaults.standard.value(forKey: #function) as? Bool | |
return isEnabled ?? true | |
} | |
set { | |
UserDefaults.standard.setValue(newValue, forKey: #function) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SettingsService { | |
private enum Keys { | |
static let isNotificationsEnabled = "isNotificationsEnabled" | |
} | |
var isNotificationsEnabled: Bool { | |
get { | |
let isEnabled = UserDefaults.standard.value(forKey: Keys.isNotificationsEnabled) as? Bool | |
return isEnabled ?? true |
NewerOlder