Post

Replies

Boosts

Views

Activity

Reply to Keeping Track of Text Changes over Two Text Fields
I'm still a beginner in using Combine. Is there a better approach in seeing text changes over two text fields at the same time using Combine? Unfortunately, it is hard to find what would be the best practice when using Combine in UIKit apps, as, you may know, there are no simple ways to integrate Combine and UIKit. So, every developer is a beginner. But one thing critically bad in your example is that you create a new instance of LoginViewModel at each time Notification is received. Other than that, I cannot say which is the better as there are very few examples on the web. The following is just that I would write something like this in cases you described: ViewModel import UIKit import Combine class LoginViewModel { //↓Use _plural_ form var cancellables = Set<AnyCancellable>() init() { $myUsername .removeDuplicates() .combineLatest($myPassword.removeDuplicates()) .sink(receiveValue: {username, password in self.validateUser(username: username, password: password) }) .store(in: &cancellables) } convenience init(username: String, password: String) { self.init() myUsername = username myPassword = password } @Published var myUsername: String? @Published var myPassword: String? func validateUser(username: String?, password: String?) { print("\(username ?? "")") print("\(password ?? "")") //If you need to update some UI, add another publisher to which the ViewController can subscribe to. //... } } import UIKit extension LoginViewModel { func bind(_ textField: UITextField, to property: ReferenceWritableKeyPath<LoginViewModel, String?>) { NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: textField) .sink(receiveValue: { result in if let textField = result.object as? UITextField { self[keyPath: property] = textField.text } }) .store(in: &cancellables) } } ViewController import UIKit import Combine class HomeViewController: UIViewController { // MARK: - Variables let viewModel = LoginViewModel() // MARK: - IBOutlet @IBOutlet var usernameTextField: UITextField! @IBOutlet var passwordTextField: UITextField! // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() viewModel.bind(usernameTextField, to: \.myUsername) viewModel.bind(passwordTextField, to: \.myPassword) } }
Topic: UI Frameworks SubTopic: UIKit Tags:
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 Generally, you should not include two or more topics in one thread..., though I'm not sure if this would be a part of the first topic or not. I would do that as follows: ViewModel @Published var isUserValid: Bool = false func validateUser(username: String?, password: String?) { if let username = username, !username.isEmpty, let password = password, !password.isEmpty { isUserValid = true } else { isUserValid = false } } ViewController private var cancellables = Set<AnyCancellable>() // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() viewModel.bind(usernameTextField, to: \.myUsername) viewModel.bind(passwordTextField, to: \.myPassword) viewModel.$isUserValid .removeDuplicates() .sink {isUserValid in print(isUserValid) //... } .store(in: &cancellables) }
Topic: UI Frameworks SubTopic: UIKit Tags:
Oct ’21
Reply to Using .center to move a subview within its parent
As far as I tried your code, the statement primaryPOIMenu.center.x += 88 works as expected. (The background color of POIMenuViews is set to system yellow.) But many things depend on the settings of your storyboard and xib. What do you get when you comment out the line modifying center?         //primaryPOIMenu.center.x += 88   // This NO WORKY!!!  Why???
Topic: UI Frameworks SubTopic: UIKit Tags:
Oct ’21
Reply to Using URLSession in Combine
First of all, you should better read this article carefully: Processing URL Session Data Task Results with Combine (You may have already read it, in such a case, read it again, carefully.) With reading the article, I would write something like this: APIClient import UIKit import Combine class APIClient: NSObject { func fetchData(urlStr: String) -> AnyPublisher<[MyModel], Never> { guard let url = URL(string: urlStr) else { let subject = CurrentValueSubject<[MyModel], Never>([]) return subject.eraseToAnyPublisher() } return URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .decode(type: [MyModel].self, decoder: JSONDecoder()) .replaceError(with: []) .eraseToAnyPublisher() } } ViewModel import Foundation import Combine class ViewModel: NSObject { @IBOutlet var apiClient: APIClient! var cancellables = Set<AnyCancellable>() func getGitData() -> AnyPublisher<[MyModel], Never> { let urlStr = "https://api.github.com/repos/ReactiveX/RxSwift/events" return apiClient.fetchData(urlStr: urlStr) } } No need to use .sink, just return the modified publisher as AnyPublisher. (You may want to use Future when you want to define another async method, but this does not seem to be the case.) Or if you want to use viewModel.dataModels somewhere in the ViewController, I would change the ViewModel as follows: import Foundation import Combine class ViewModel: NSObject { @IBOutlet var apiClient: APIClient! var cancellables = Set<AnyCancellable>() private let dataModelSubject = CurrentValueSubject<[MyModel], Never>([]) var dataModels: [MyModel] { get {dataModelSubject.value} set {dataModelSubject.send(newValue)} } func getGitData() -> AnyPublisher<[MyModel], Never> { let urlStr = "https://api.github.com/repos/ReactiveX/RxSwift/events" apiClient.fetchData(urlStr: urlStr) .sink { result in print("view model: \(result.count)") self.dataModels = result } .store(in: &cancellables) return dataModelSubject.eraseToAnyPublisher() } } This shows a little different behavior than the previous code, as CurrentValueSubject will send its initial data to subscribers. (0 seems to be the count of the initial value in your code.) In your code, you hold instances of CurrentValueSubject only as a local constant and never call send to these instance. That does not seem to be the right usage of CurrentValueSubject.
Topic: UI Frameworks SubTopic: UIKit Tags:
Nov ’21
Reply to SwiftUI: I keep getting this error: "Thread 1: "-[AccountBackEnd accountCompanyName]: unrecognized selector sent to instance 0x6000025aac40""
Thanks for showing the entity definition. I can eliminate one possibility with it. Another possibility causing this issue might reside in this line: @ObservedObject var accountBackEnd: AccountBackEnd = AccountBackEnd() With instantiating a Core Data entity class with .init() like AccountBackEnd(), the instance has nothing to do with the entity definition, so any sort of access to @NSManaged properties will cause the error. Isn't this line in DatabaseViewMain AccountDetailMain(accountBackEnd: accountBackEnd) should be something like this? AccountDetailMain(accountBackEnd: account)
Topic: App & System Services SubTopic: iCloud Tags:
Nov ’21
Reply to What's the expected performance of VNHumandBodyPoseObservation::recognizedPoint
Thanks for showing the link. Is it safe to assume once the observation has been generated, the points are cached in some structure internal to the observation? The problem is that it is not clear what you mean by cached. You should better not expect anything unless it is explicitly documented. But as far as I can see in the docs and in my experiences I have used other Vision features till now, VNHumanBodyPoseObservation is a class type, so the points held in an instance would be kept while the instance is alive. In other words, you should not expect that the points would be updated even if you performed another VNDetectHumanBodyPoseRequest. If you think this is not answering to your question, please explain what you mean by cached more precisely.
Topic: Programming Languages SubTopic: Swift Tags:
Nov ’21
Reply to wwdc20-10073 Recipe Assistant project not working
I have built the RecipeAssistant sample project with Xcode 13.1 on macOS 11.6.1 for iPhone, and run it on iPhone 12 Pro Max (iOS 15.1). It has shown a lot of messages in the debug console but I cannot find any messages like I can't get directions using Recipe Assistant. Sorry about that. neither in the debug console or in the app. Not sure macOS Monterey 12.0.1 is affecting, but can you provide more info about what you have tried?
Topic: App & System Services SubTopic: General Tags:
Nov ’21
Reply to Keeping Track of Text Changes over Two Text Fields
I'm still a beginner in using Combine. Is there a better approach in seeing text changes over two text fields at the same time using Combine? Unfortunately, it is hard to find what would be the best practice when using Combine in UIKit apps, as, you may know, there are no simple ways to integrate Combine and UIKit. So, every developer is a beginner. But one thing critically bad in your example is that you create a new instance of LoginViewModel at each time Notification is received. Other than that, I cannot say which is the better as there are very few examples on the web. The following is just that I would write something like this in cases you described: ViewModel import UIKit import Combine class LoginViewModel { //↓Use _plural_ form var cancellables = Set<AnyCancellable>() init() { $myUsername .removeDuplicates() .combineLatest($myPassword.removeDuplicates()) .sink(receiveValue: {username, password in self.validateUser(username: username, password: password) }) .store(in: &cancellables) } convenience init(username: String, password: String) { self.init() myUsername = username myPassword = password } @Published var myUsername: String? @Published var myPassword: String? func validateUser(username: String?, password: String?) { print("\(username ?? "")") print("\(password ?? "")") //If you need to update some UI, add another publisher to which the ViewController can subscribe to. //... } } import UIKit extension LoginViewModel { func bind(_ textField: UITextField, to property: ReferenceWritableKeyPath<LoginViewModel, String?>) { NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: textField) .sink(receiveValue: { result in if let textField = result.object as? UITextField { self[keyPath: property] = textField.text } }) .store(in: &cancellables) } } ViewController import UIKit import Combine class HomeViewController: UIViewController { // MARK: - Variables let viewModel = LoginViewModel() // MARK: - IBOutlet @IBOutlet var usernameTextField: UITextField! @IBOutlet var passwordTextField: UITextField! // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() viewModel.bind(usernameTextField, to: \.myUsername) viewModel.bind(passwordTextField, to: \.myPassword) } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Is wwdc20-10039 ShapeEditorApp source available
Similar questions are repeatedly posted for over a year in the dev forums, but I have not found the source from Apple. Have you sent a feedback to Apple?
Topic: UI Frameworks SubTopic: SwiftUI 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 Generally, you should not include two or more topics in one thread..., though I'm not sure if this would be a part of the first topic or not. I would do that as follows: ViewModel @Published var isUserValid: Bool = false func validateUser(username: String?, password: String?) { if let username = username, !username.isEmpty, let password = password, !password.isEmpty { isUserValid = true } else { isUserValid = false } } ViewController private var cancellables = Set<AnyCancellable>() // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() viewModel.bind(usernameTextField, to: \.myUsername) viewModel.bind(passwordTextField, to: \.myPassword) viewModel.$isUserValid .removeDuplicates() .sink {isUserValid in print(isUserValid) //... } .store(in: &cancellables) }
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to Using .center to move a subview within its parent
As far as I tried your code, the statement primaryPOIMenu.center.x += 88 works as expected. (The background color of POIMenuViews is set to system yellow.) But many things depend on the settings of your storyboard and xib. What do you get when you comment out the line modifying center?         //primaryPOIMenu.center.x += 88   // This NO WORKY!!!  Why???
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to RunLoop behaves differently between iOS15 and iOS14
I am very confused this issue. Is it a well known issue?  It is not clearly documented what would happen if you call RunLoop.current.run(until:) in the main thread. Aren't you calling it in the main thread? Any sort of undocumented behaviors would change at any time without any notices. Why are you calling RunLoop in your code?
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Oct ’21
Reply to xcode 13.1 swiftui
Firebase is not a framework of Apple's. Better visit the community site or the supporting site of it to getter better responses sooner.
Replies
Boosts
Views
Activity
Oct ’21
Reply to Axie Infinity invitation code
This is not a place to request for an invitation code or something of a specific app. Contact to the author of the app.
Replies
Boosts
Views
Activity
Oct ’21
Reply to SwiftUI: I keep getting this error: "Thread 1: "-[AccountBackEnd accountCompanyName]: unrecognized selector sent to instance 0x6000025aac40""
Can you show the class definition of AccountBackEnd? And the entity definition of AccountBackEnd in your Core Data model?
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to SwiftUI: I keep getting this error: "Thread 1: "-[AccountBackEnd accountCompanyName]: unrecognized selector sent to instance 0x6000025aac40""
Thanks for showing the code. But how is the entity definition in your data model? The error you have described often happens when the class definition and the entity definition are inconsistent.
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to Using URLSession in Combine
First of all, you should better read this article carefully: Processing URL Session Data Task Results with Combine (You may have already read it, in such a case, read it again, carefully.) With reading the article, I would write something like this: APIClient import UIKit import Combine class APIClient: NSObject { func fetchData(urlStr: String) -> AnyPublisher<[MyModel], Never> { guard let url = URL(string: urlStr) else { let subject = CurrentValueSubject<[MyModel], Never>([]) return subject.eraseToAnyPublisher() } return URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .decode(type: [MyModel].self, decoder: JSONDecoder()) .replaceError(with: []) .eraseToAnyPublisher() } } ViewModel import Foundation import Combine class ViewModel: NSObject { @IBOutlet var apiClient: APIClient! var cancellables = Set<AnyCancellable>() func getGitData() -> AnyPublisher<[MyModel], Never> { let urlStr = "https://api.github.com/repos/ReactiveX/RxSwift/events" return apiClient.fetchData(urlStr: urlStr) } } No need to use .sink, just return the modified publisher as AnyPublisher. (You may want to use Future when you want to define another async method, but this does not seem to be the case.) Or if you want to use viewModel.dataModels somewhere in the ViewController, I would change the ViewModel as follows: import Foundation import Combine class ViewModel: NSObject { @IBOutlet var apiClient: APIClient! var cancellables = Set<AnyCancellable>() private let dataModelSubject = CurrentValueSubject<[MyModel], Never>([]) var dataModels: [MyModel] { get {dataModelSubject.value} set {dataModelSubject.send(newValue)} } func getGitData() -> AnyPublisher<[MyModel], Never> { let urlStr = "https://api.github.com/repos/ReactiveX/RxSwift/events" apiClient.fetchData(urlStr: urlStr) .sink { result in print("view model: \(result.count)") self.dataModels = result } .store(in: &cancellables) return dataModelSubject.eraseToAnyPublisher() } } This shows a little different behavior than the previous code, as CurrentValueSubject will send its initial data to subscribers. (0 seems to be the count of the initial value in your code.) In your code, you hold instances of CurrentValueSubject only as a local constant and never call send to these instance. That does not seem to be the right usage of CurrentValueSubject.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to SwiftUI: I keep getting this error: "Thread 1: "-[AccountBackEnd accountCompanyName]: unrecognized selector sent to instance 0x6000025aac40""
Thanks for showing the entity definition. I can eliminate one possibility with it. Another possibility causing this issue might reside in this line: @ObservedObject var accountBackEnd: AccountBackEnd = AccountBackEnd() With instantiating a Core Data entity class with .init() like AccountBackEnd(), the instance has nothing to do with the entity definition, so any sort of access to @NSManaged properties will cause the error. Isn't this line in DatabaseViewMain AccountDetailMain(accountBackEnd: accountBackEnd) should be something like this? AccountDetailMain(accountBackEnd: account)
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to What's the expected performance of VNHumandBodyPoseObservation::recognizedPoint
Thanks for showing the link. Is it safe to assume once the observation has been generated, the points are cached in some structure internal to the observation? The problem is that it is not clear what you mean by cached. You should better not expect anything unless it is explicitly documented. But as far as I can see in the docs and in my experiences I have used other Vision features till now, VNHumanBodyPoseObservation is a class type, so the points held in an instance would be kept while the instance is alive. In other words, you should not expect that the points would be updated even if you performed another VNDetectHumanBodyPoseRequest. If you think this is not answering to your question, please explain what you mean by cached more precisely.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to Buttons To Control Gradients
Can you show your code that explains what you have done till now and what you can do now?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to Swift Tutorial - Building Lists and Navigation - Section 2 - Landmarks Quit Unexpectedly
When some part of the code is not working as expected, you may have some faults in other parts of your code. In this case, you may need to check all the parts where landmarks appears. One more, when your app quit unexpectedly, please show all the messages shown in the debug console.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Nov ’21
Reply to wwdc20-10073 Recipe Assistant project not working
I have built the RecipeAssistant sample project with Xcode 13.1 on macOS 11.6.1 for iPhone, and run it on iPhone 12 Pro Max (iOS 15.1). It has shown a lot of messages in the debug console but I cannot find any messages like I can't get directions using Recipe Assistant. Sorry about that. neither in the debug console or in the app. Not sure macOS Monterey 12.0.1 is affecting, but can you provide more info about what you have tried?
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
Nov ’21