Post

Replies

Boosts

Views

Activity

Reply to Using URLSession in Combine
Okay... The solution to my question is use of the Future guy. class APIClient: NSObject { var cancellables = [AnyCancellable]() @Published var models = [MyModel]() func fetchData(urlStr: String) -> Future<[MyModel], Error> { return Future<[MyModel], Error> { [weak self] promise in guard let url = URL(string: urlStr) else { return promise(.failure("Failure" as! Error)) } guard let strongSelf = self else { return } URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .decode(type: [MyModel].self, decoder: JSONDecoder()) .replaceError(with: []) .sink { completion in if case .failure(let error) = completion { promise(.failure(error)) } } receiveValue: { promise(.success($0)) } .store(in: &strongSelf.cancellables) } } } class ViewModel: NSObject { @IBOutlet var apiClient: APIClient! var cancellables = Set<AnyCancellable>() @Published var dataModels = [MyModel]() func getGitData() -> Future<[MyModel], Error> { let urlStr = "https://api.github.com/repos/ReactiveX/RxSwift/events" return Future<[MyModel], Error> { [weak self] promise in guard let strongSelf = self else { return } strongSelf.apiClient.fetchData(urlStr: urlStr) .sink { completion in if case .failure(let error) = completion { promise(.failure(error)) } } receiveValue: { promise(.success($0)) print("view model: \($0.count)") } .store(in: &strongSelf.cancellables) } } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Nov ’21
Reply to Observing Changes in Multiple @Published Variables at the Same Time?
I think I've figured it out for myself. It looks like I can use Publishers.CombineLatest(,) as follows. class ViewController: UIViewController { var cancellables = Set<AnyCancellable>() @Published var userText: String = "" @Published var passText: String = "" override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: usernameTextField) .sink(receiveValue: { (result) in if let myField = result.object as? UITextField { if let text = myField.text { self.userText = text } } }) .store(in: &cancellables) NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: passwordTextField) .sink(receiveValue: { (result) in if let myField = result.object as? UITextField { if let text = myField.text { self.passText = text } } }) .store(in: &cancellables) Publishers.CombineLatest($userText, $passText) .sink { (result0, result1) in print(result0, result1) }.store(in: &cancellables) } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Oct ’21
Reply to Updating @State Variable Depending ForEach Row Selection
It seems that I have achieved my objective with the following. But I'm not sure if it's an efficient approach. import SwiftUI struct ContentView: View { @State var numbers = [2021, 9, 30] var body: some View { let firstLocalYear = 2021 let firstLocalMonth = 9 let firstLocalDay = 24 let firstLastDay = 30 NavigationView { List { Section(header: Text("Current month")) { ForEach(firstLocalDay ..< firstLastDay) { i in HStack { Text("\(firstLocalMonth)-\(i + 1)") Spacer() Button("Select") { self.numbers = [firstLocalYear, firstLocalMonth, i + 1] } NavigationLink( destination: TimeView(numbers: $numbers), label: { Text("") }) } } } } } } } struct TimeView: View { @Binding var numbers: [Int] var body: some View { HStack { Text(String(numbers[0])) Text(String(numbers[1])) Text(String(numbers[2])) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to Combining More Than Four @Published Variables in Combine?
Actually, no...
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to Combining More Than Four @Published Variables in Combine?
Maybe, subscribing to an array of them?
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to Question free app
It depends on the purpose of your app. After a trial period, if the app has no features to use, then it will be rejected for sure.
Replies
Boosts
Views
Activity
Nov ’21
Reply to how to use Apple Pay without backend server or stripe.
To my knowledge, Apple Pay is not a payment processing service system. It's a mediator or a digital wallet that stores privacy data and allows the user to make a payment through a third-party processing system. So the answer to your question is no, I suppose.
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to How do I check personal information before enrollment?
Go to Apple Developer and click on Membership after signing in.
Replies
Boosts
Views
Activity
Nov ’21
Reply to Using URLSession in Combine
Okay... The solution to my question is use of the Future guy. class APIClient: NSObject { var cancellables = [AnyCancellable]() @Published var models = [MyModel]() func fetchData(urlStr: String) -> Future<[MyModel], Error> { return Future<[MyModel], Error> { [weak self] promise in guard let url = URL(string: urlStr) else { return promise(.failure("Failure" as! Error)) } guard let strongSelf = self else { return } URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .decode(type: [MyModel].self, decoder: JSONDecoder()) .replaceError(with: []) .sink { completion in if case .failure(let error) = completion { promise(.failure(error)) } } receiveValue: { promise(.success($0)) } .store(in: &strongSelf.cancellables) } } } class ViewModel: NSObject { @IBOutlet var apiClient: APIClient! var cancellables = Set<AnyCancellable>() @Published var dataModels = [MyModel]() func getGitData() -> Future<[MyModel], Error> { let urlStr = "https://api.github.com/repos/ReactiveX/RxSwift/events" return Future<[MyModel], Error> { [weak self] promise in guard let strongSelf = self else { return } strongSelf.apiClient.fetchData(urlStr: urlStr) .sink { completion in if case .failure(let error) = completion { promise(.failure(error)) } } receiveValue: { promise(.success($0)) print("view model: \($0.count)") } .store(in: &strongSelf.cancellables) } } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to Using URLSession in Combine
I wonder if I need to use Future to return data?
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to Observing Changes in Multiple @Published Variables at the Same Time?
I think I've figured it out for myself. It looks like I can use Publishers.CombineLatest(,) as follows. class ViewController: UIViewController { var cancellables = Set<AnyCancellable>() @Published var userText: String = "" @Published var passText: String = "" override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: usernameTextField) .sink(receiveValue: { (result) in if let myField = result.object as? UITextField { if let text = myField.text { self.userText = text } } }) .store(in: &cancellables) NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: passwordTextField) .sink(receiveValue: { (result) in if let myField = result.object as? UITextField { if let text = myField.text { self.passText = text } } }) .store(in: &cancellables) Publishers.CombineLatest($userText, $passText) .sink { (result0, result1) in print(result0, result1) }.store(in: &cancellables) } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Keeping Track of Text Changes over Two Text Fields
May I ask how I can receive a bool result in my view controller like func validateUser(username: String?, password: String?) -> AnyPublisher<Bool, Never> { if let username = username, let password = password { if !username.isEmpty && !password.isEmpty { // true } } } , please?
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Combine with UITableView
I don't know if my approach in reading view model data is correct. At least, I don't think it's efficient in the view controller.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Updating @State Variable Depending ForEach Row Selection
It seems that I have achieved my objective with the following. But I'm not sure if it's an efficient approach. import SwiftUI struct ContentView: View { @State var numbers = [2021, 9, 30] var body: some View { let firstLocalYear = 2021 let firstLocalMonth = 9 let firstLocalDay = 24 let firstLastDay = 30 NavigationView { List { Section(header: Text("Current month")) { ForEach(firstLocalDay ..< firstLastDay) { i in HStack { Text("\(firstLocalMonth)-\(i + 1)") Spacer() Button("Select") { self.numbers = [firstLocalYear, firstLocalMonth, i + 1] } NavigationLink( destination: TimeView(numbers: $numbers), label: { Text("") }) } } } } } } } struct TimeView: View { @Binding var numbers: [Int] var body: some View { HStack { Text(String(numbers[0])) Text(String(numbers[1])) Text(String(numbers[2])) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’21
Reply to Changing a Status With @EnvironmentObject in Another View
Oops... Silly me... If it's logged in, I only show Text("Yes, I'm logged in")
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
Sep ’21
Reply to Toggling Values on EnvironmentValue (EditMode)
Probably not because this EnvironmetValue is not dichotomous?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’21
Reply to Resources for development
Are you talking about Apple's marketing resources??
Topic: App & System Services SubTopic: Hardware Tags:
Replies
Boosts
Views
Activity
Sep ’21
Reply to Practical Use of Combine's Subject
Oh, I see. Silly me... Thanks a lot.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Aug ’21