This file contains hidden or 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
| // Create a new search subject with a default value | |
| let searchSubject = CurrentValueSubject<String, Never>("") | |
| // Subscribe to updates | |
| searchSubject | |
| .debounce(for: 0.3, scheduler: DispatchQueue.main) | |
| .map(searchBook(for:)) | |
| .flatMap {$0.map(\.author)} | |
| .subscribe(on: DispatchQueue.main) | |
| .sink { author in |
This file contains hidden or 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
| // Create a new search subject with a default value | |
| final class ViewController: UIViewController { | |
| // Published subject initialized to zero | |
| @Published var buttonTapSubject: Int = 0 | |
| var cancellables = Set<AnyCancellable>() | |
| let button = UIButton() | |
| let label = UILabel() | |
This file contains hidden or 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
| Just("hello world!") | |
| .sink { str in | |
| print("the string is:", str) | |
| } |
This file contains hidden or 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
| // Create a new search subject with a default value | |
| class ViewController: UIViewController { | |
| @Published var buttonTapSubject: Int = 0 | |
| @Published var textSubject: String = "" | |
| var cancellables = Set<AnyCancellable>() | |
| let button = UIButton() | |
| let label = UILabel() | |
| override func viewDidLoad() { |
This file contains hidden or 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
| // many publishers | |
| var cancellables = Set<AnyCancellable>() | |
| $textSubject | |
| .compactMap { $0 } | |
| .assign(to: \.text, on: label) | |
| .store(in: &cancellables) | |
| // or single publisher |
This file contains hidden or 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
| import AVKit | |
| struct ContentView: View { | |
| @State var videos: [Video] = [] | |
| var body: some View { | |
| VideoView() | |
| .onReceive( | |
| NotificationCenter.default.publisher(for: .AVPlayerItemDidPlayToEndTime), |
This file contains hidden or 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
| ["hello world"] | |
| .publisher | |
| .subscribe(on: DispatchQueue.global(qos: .background)) // 1 | |
| .map { string -> String in | |
| print("Is main thread in map:", Thread.isMainThread) | |
| return string | |
| } | |
| .breakpoint(receiveOutput: { str in | |
| print("current str:", str) | |
| return Thread.isMainThread // if main thread, breakpoint |
This file contains hidden or 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
| [ | |
| { | |
| "name": "Gary Tokman", | |
| "bio": "iOS dev working with SwiftUI", | |
| "imageUrl": "https://pbs.twimg.com/profile_images/1395577843476742144/MOWWQV-V_400x400.jpg", | |
| "twitter": "f6ary" | |
| }, | |
| { | |
| "name": "Elon Musk", | |
| "bio": "CEO of Tesla", |
This file contains hidden or 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
| { | |
| "businesses": [ | |
| { | |
| "id": "htEuhPBhBgMs6ShlT3G3JA", | |
| "alias": "regina-pizzeria-boston-28", | |
| "name": "Regina Pizzeria", | |
| "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/rPG03lJzJVlrTJNrQnuQxg/o.jpg", | |
| "is_closed": false, | |
| "url": "https://www.yelp.com/biz/regina-pizzeria-boston-28?adjust_creative=P9N85ndOnt6FtF1FkbyulA&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=P9N85ndOnt6FtF1FkbyulA", | |
| "review_count": 2099, |
This file contains hidden or 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
| // Grab everything before the cursor | |
| var before = textDocumentProxy.documentContextBeforeInput | |
| var completePriorString = "" | |
| while let beforeInput = before, !beforeInput.isEmpty { | |
| completePriorString = beforeInput | |
| let length = beforeInput.lengthOfBytes(using: String.Encoding.utf8) | |
| textDocumentProxy.adjustTextPosition(byCharacterOffset: -length) |