How to map error in Combine

Issue #506 When a function expects AnyPublisher<[Book], Error> but in mock, we have Just func getBooks() -> AnyPublisher<[Book], Error> { return Just([ Book(id: "1", name: "Book 1"), Book(id: "2", name: "Book 2"), ]) .eraseToAnyPublisher() } There will be a mismatch, hence compile error Cannot convert return expression of type ‘AnyPublisher<[Book], Just.Failure>’ (aka ‘AnyPublisher<Array, Never>') to return type ‘AnyPublisher<[Book], Error>’ The reason is because Just produces Never, not Error. The workaround is to introduce Error...

November 14, 2019 · 1 min · Khoa Pham

How to fix unable to infer complex closure return type in SwiftUI

Issue #505 Make sure all String are passed into Text, not Optional<String> VStack { Text(data.title) Text(data.description!) Text(data.text!) }

November 13, 2019 · 1 min · Khoa Pham

How to make Swift Package Manager package for multiple platforms

Issue #504 https://twitter.com/NeoNacho/status/1181245484867801088?s=20 There’s no way to have platform specific sources or targets today, so you’ll have to take a different approach. I would recommend wrapping all OS specific files in #if os and just having one target. For tests, you could do something similar, one test target, but conditional tests Every files are in Sources folder, so we can use platform and version checks. For example Omnia is a Swift Package Manager that supports iOS, tvOS, watchOS, macOS and Catalyst....

November 13, 2019 · 1 min · Khoa Pham

How to make simple Redux for SwiftUI

Issue #502 Mutation is used to mutate state synchronously. Action is like intent, either from app or from user action. Action maps to Mutation in form of Publisher to work with async action, similar to redux-observable AnyReducer is a type erasure that takes the reduce function import Combine import Foundation public protocol Reducer { associatedtype State associatedtype Mutation func reduce(state: State, mutation: Mutation) -> State } public struct AnyReducer<State, Mutation> { public let reduce: (State, Mutation) -> State public init<R: Reducer>(reducer: R) where R....

November 13, 2019 · 2 min · Khoa Pham

How to use Firebase in macOS

Issue #501 Use Catalyst Add to CocoaPods platform :ios, '13.0' target 'MyApp' do use_frameworks! pod 'FirebaseCore' pod 'Firebase/Firestore' end Troubleshooting Select a team for gRPC-C++-gRPCCertificates-Cpp FIRAnalyticsConnector: building for Mac Catalyst, but linking in object file built for iOS Simulator https://stackoverflow.com/questions/57666155/firanalyticsconnector-building-for-mac-catalyst-but-linking-in-object-file-bui The problem was related to the difference between Firebase/Core and FirebaseCore. The first is a subspec of the Firebase pod that depends on FirebaseAnalytics. The second is only the FirebaseCore pod....

November 12, 2019 · 1 min · Khoa Pham

How to use Xcode

Issue #499 Build setting Build Library For Distribution Binary Frameworks in Swift It turns on all the features that are necessary to build your library in such a way that it can be distributed What does this error actually mean? Well, when the Swift compiler goes to import a module, it looks for a file called the Compiled Module for that library. If it finds one of these files, it reads off the manifest of public APIs that you can call into, and lets you use them....

November 12, 2019 · 2 min · Khoa Pham

Links for Xcode

Issue #499 Build setting Build Library For Distribution Binary Frameworks in Swift It turns on all the features that are necessary to build your library in such a way that it can be distributed What does this error actually mean? Well, when the Swift compiler goes to import a module, it looks for a file called the Compiled Module for that library. If it finds one of these files, it reads off the manifest of public APIs that you can call into, and lets you use them....

November 12, 2019 · 2 min · Khoa Pham

How to access view in fragment in Kotlin

Issue #497 Synthetic properties generated by Kotlin Android Extensions plugin needs a view for Fragment/Activity to be set before hand. In your case, for Fragment, you need to use view.btn_K in onViewCreated override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { super.onCreateView(inflater, container, savedInstanceState) val view = inflater.inflate(R.layout.fragment_card_selector, container, false) view.btn_K.setOnClickListener{} // access with `view` return view } Or better, you should only access synthetic properties in onViewCreated...

November 10, 2019 · 1 min · Khoa Pham

How to refresh receipt and restore in app purchase in iOS

Issue #496 Read this Restoring Purchased Products to understand the purposes between the 2. From iOS 7, every app downloaded from the store has a receipt (for downloading/buying the app) at appStoreReceiptURL. When users purchases something via In App Purchase, the content at appStoreReceiptURL is updated with purchases information. Most of the cases, you just need to refresh the receipt (at appStoreReceiptURL) so that you know which transactions users have made....

November 10, 2019 · 2 min · Khoa Pham

How to edit hexo theme hiero

Issue #494 Code https://github.com/iTimeTraveler/hexo-theme-hiero Remove max-width from source/css/style.styl .outer clearfix() // max-width: (column-width + gutter-width) * columns + gutter-width margin: 40px auto padding: 0 gutter-width Change font-size of code block from source/css/_partial/highlight.styl $code-block background: $highlight-background margin: article-padding article-padding * 0 padding: 15px article-padding border-style: solid border-color: color-border border-width: 1px 0 overflow: auto color: $highlight-foreground font-size: 1.0em; line-height: 1em Change font-size of article-header from source/css/_partial/article.styl .article-header h1 margin: 0 0 3px 0; font-size: 26px; line-height: 1....

November 6, 2019 · 1 min · Khoa Pham

How to use Firebase RemoteConfig

Issue #493 Declare in Podfile pod 'Firebase/Core' pod 'Firebase/RemoteConfig' Use RemoteConfigHandler to encapsulate logic. We introduce Key with CaseIterable and defaultValue of type NSNumber to manage default values. import Firebase import FirebaseRemoteConfig final class RemoteConfigHandler { let remoteConfig: RemoteConfig enum Key: String, CaseIterable { case interval = "fetch_interval" var defaultValue: NSNumber { switch self { case .periodicGetSalons: return NSNumber(value: 300) } } } init() { self.remoteConfig = RemoteConfig.remoteConfig() let settings = RemoteConfigSettings() settings....

November 5, 2019 · 1 min · Khoa Pham

How to apply translations to Localizable.strings

Issue #492 Suppose we have a base Localizable.strings "open" = "Open"; "closed" = "Closed"; After sending that file for translations, we get translated versions. "open" = "Åpen"; "closed" = "Stengt"; Searching and copy pasting these to our Localizable.strings is tedious and time consuming. We can write a script to apply that. Remember that we need to be aware of smart and dump quotes .replace(/\"/g, '') .replace(/\"/g, '') const fs = require('fs') const originalFile = 'MyApp/Resources/nb....

November 5, 2019 · 2 min · Khoa Pham

How to use CreateML to classify images

Issue #491 CreateMLUI Playground Read more https://developer.apple.com/documentation/createml/creating_an_image_classifier_model https://captechconsulting.com/blogs/hands-on-with-the-all-new-create-ml-app-machine-learning-for-the-masses Training Sound Classification Models in Create ML - WWDC 2019 Building Activity Classification Models in Create ML - WWDC 2019 Create ML for Everyone

November 4, 2019 · 1 min · Khoa Pham

How to use Firebase AutoML Vision Edge to classify images

Issue #490 Create project on Firebase and choose Vision Edge Vision Edge is part of MLKit, but for custom images training https://console.firebase.google.com/u/0/project/avengers-ad2ce/ml/ Model Avengers_dataset_2019114133437 is training and may take several hours. You will receive an email once training is complete.

November 4, 2019 · 1 min · Khoa Pham

How to use Google AutoML to classify images

Issue #489 Create bucket on Google Cloud Storage https://console.cloud.google.com/storage Create dataset by uploading images to Google AutoML Vision https://console.cloud.google.com/vision

November 4, 2019 · 1 min · Khoa Pham

How to get Binding via dollar prefix in SwiftUI

Issue #488 The dollar is not a prefix, it seems to be a generated code for property wrapper, and each kind of property wrapper can determine which value it return via this dollar sign State and ObservedObject are popular property wrappers in SwiftUI State Read State A persistent value of a given type, through which a view reads and monitors the value. If we have a simple State, we can access its 3 forms...

November 2, 2019 · 3 min · Khoa Pham

How to modify state from state in SwiftUI

Issue #487 In case we have to modify state when another state is known, we can encapsulate all those states in ObservableObject and use onReceive to check the state we want to act on. See code Avengers If we were to modify state from within body function call, we will get warnings Modifying state during view update, this will cause undefined behavior. This is similar to the warning when we change state inside render in React...

November 2, 2019 · 6 min · Khoa Pham

How to show loading indicator in SwiftUI

Issue #486 import SwiftUI struct ActivityIndicator: UIViewRepresentable { @Binding var isAnimating: Bool let style: UIActivityIndicatorView.Style func makeUIView(context: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView { return UIActivityIndicatorView(style: style) } func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext<ActivityIndicator>) { isAnimating ? uiView.startAnimating() : uiView.stopAnimating() } } struct ActivityIndicator_Previews: PreviewProvider { static var previews: some View { ActivityIndicator(isAnimating: .constant(true), style: .large) } }

November 2, 2019 · 1 min · Khoa Pham

How to show image picker in SwiftUI

Issue #485 The easiest way to show image picker in iOS is to use UIImagePickerController, and we can bridge that to SwiftUI via UIViewControllerRepresentable First attempt, use Environment We conform to UIViewControllerRepresentable and make a Coordinator, which is the recommended way to manage the bridging with UIViewController. There’s some built in environment property we can use, one of those is presentationMode where we can call dismiss to dismiss the modal....

November 2, 2019 · 4 min · Khoa Pham

How to add monkey test to iOS apps

Issue #484 Use SwiftMonkey which adds random UITests gestures Add to UITests target target 'MyAppUITests' do pod 'R.swift', '~> 5.0' pod 'SwiftMonkey', '~> 2.1.0' end Troubleshooting Failed to determine hittability of Button Failed to determine hittability of Button: Unable to fetch parameterized attribute XC_kAXXCParameterizedAttributeConvertHostedViewPositionFromContext, remote interface does not have this capability. This happens when using SwiftMonkey and somewhere in our code uses isHittable, so best to avoid that by having isolated monkey test only...

November 1, 2019 · 1 min · Khoa Pham