Skip to content

Instantly share code, notes, and snippets.

@AndreyAnt
Created May 4, 2020 11:27
Show Gist options
  • Save AndreyAnt/a195e4574bc66e4012760d1e26c4a4da to your computer and use it in GitHub Desktop.
Save AndreyAnt/a195e4574bc66e4012760d1e26c4a4da to your computer and use it in GitHub Desktop.
Gist for geekbrains lecture №5. SwiftUI course
class MyClass {
@Logged(formatString: "HH:mm dd.MM.yyyy") var myProperty = 0
init() {
print(type(of: _myProperty))
print(type(of: $myProperty))
print(type(of: myProperty))
}
}
protocol MyProtocol {
associatedtype Value
var myProperty: Logged<Value> { get set }
}
@propertyWrapper struct Logged<Value> {
private var value: Value
private let dateFormatter: DateFormatter
init(wrappedValue: Value, formatString: String) {
self.value = wrappedValue
self.dateFormatter = DateFormatter()
self.dateFormatter.dateFormat = formatString
}
private func get() -> Value {
let dateString = dateFormatter.string(from: Date())
print("\(dateString). myProperty read: \(value)")
return value
}
private mutating func set(_ newValue: Value) {
let dateString = dateFormatter.string(from: Date())
print("\(dateString). myProperty set to: \(newValue)")
value = newValue
}
public var wrappedValue: Value {
get {
get()
}
set {
set(newValue)
}
}
public var projectedValue: Self {
return self
}
// public var projectedValue: String {
// return "SomeString"
// }
}
//myClass._myProperty
MyClass().myProperty._value
@propertyWrapper struct Trimmed {
let value: String
init(wrappedValue: String) {
self.value = wrappedValue
}
var wrappedValue: String {
get {
return "smth"
}
set {
// doing smth
}
}
}
@propertyWrapper struct Dashed {
let value: String
init(wrappedValue: String) {
self.value = wrappedValue
}
var wrappedValue: String {
get {
return "smth"
}
set {
// doing smth
}
}
}
class MyClass2 {
// можно
@Dashed var someString: String = "value"
// можно
@Trimmed var someString2: String = "value"
// нельзя
// @Dashed @Trimmed var someString3: String = "value"
}
//@State - Binding<Value>
//@Binding - Binding<Value>
//@ObservedObject - Binding<Value> (*)
//@EnvironmentObject - Binding<Value> (*)
//@Published - Publisher<Value, Never>
import SwiftUI
struct MyView: View {
@State var state = 1
@Binding(get: { return "smth" }, set: { _ in /* doing smth */ }) var binding: String
@ObservedObject var observedObject = ViewState()
@Environment(\.horizontalSizeClass) var horizontalSizeClass
@EnvironmentObject var environmentObject: ViewState
@FetchRequest var someProp
init() {
}
var body: some View {
return Text("Hello world!")
}
}
class ViewState: ObservableObject {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment