9 Notes February 2021
1. Dynamic string with format
this dynamic link use a single input string
extension String {
// MARK: dynamic string in string for single input
func dynamicString(format: String) -> String {
return String(format: format, self)
}
}
let format = "Hello %@, Welcome!"
let user = "jack"
print("user.dynamicString(format: format) // Hello jack, Welcome
source: https://stackoverflow.com/a/33441982/8366535
2. How to do two concurrent API calls in swift 4
If you have condition when call API at same time but want to sequentially so you can use
DispatchGroup()
let group = DispatchGroup()
override func viewDidLoad() {
super.viewDidload()
callApiOne()
callApiTwo()
}
func callApiOne() {
group.enter()
//call here
// in response
group.leave()
}
func callApiTwo() {
group.enter()
//call here
// in response
group.leave()
}
source: https://stackoverflow.com/a/50557709/8366535
3. Xcode 12 build failed "non zero arm64"
you can manually add the Excluded Architechure in your Pod project's Build Settings, but it will be overwritten when you use pod install.
In place of this, you can add this snippet in your Podfile. It will write the neccessary Build Settings every time you run pod install
"Error"
ld: in /Users/User/Backup/App/App_ios/ios/Pods/Realm/core/librealmcore-ios.a(bptree.o), building for iOS Simulator, but linking in object file built for iOS, for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
post_install do |installer|
installer.pods_project.build_configurations.each do |config|
config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
end
end
4. I Got error Jenkins when try to build for production to App Store
This error gets from Jenkins build result.
[12:33:54]: Could not find an edit version on App Store Connect. Try using '--use_live_version true'
[12:33:54]: Couldn't download already existing screenshots from App Store Connect.
this error can be fix in fastlane you can check in github for more solution
https://github.com/fastlane/fastlane/issues/13522
Source: https://stackoverflow.com/a/66278613/8366535
5. Change tabBar title color not real-time change
I try using appearance for change title tabBar color
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)
but I got a problem when using "UITabBarItem.appearance" is must tap two times for change not realtime
so I've solved this code:
tabBar.tintColor = .green
tabBar.unselectedItemTintColor = .blue
source: https://stackoverflow.com/a/54003278/8366535
6. Add badge to UIView
Custom uiview adding (badge + number) into a corner uiview with programatically
Full code custom class uiview
class CustomUIView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
// MARK: draw circle badge
let badgeView = UIView(frame: CGRect(x: self.frame.width - 26, y: 6, width: 20, height: 20))
badgeView.layer.cornerRadius = badgeView.frame.height / 2
badgeView.layer.borderColor = UIColor.white.cgColor
badgeView.backgroundColor = .salmon
badgeView.borderWidth = 2
badgeView.borderColor = .white
// MARK: add number to circle badge
let number = UILabel()
number.textAlignment = .center
number.center = badgeView.center
number.frame = badgeView.frame
number.textColor = .white
number.font = UIFont.systemFont(ofSize: 12)
number.text = "4"
// Add the border view to fake the border being there
self.addSubview(badgeView)
self.addSubview(number)
}
}
7. Simple Toast Extension
extension UIViewController {
func showToast(message : String, font: UIFont) {
let toastLabel = UILabel(frame: CGRect(
x: self.view.frame.size.width/2 - 75,
y: self.view.frame.size.height-100,
width: 200,
height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.font = font
toastLabel.textAlignment = .center;
toastLabel.text = message
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
}
}
how use: self.showToast(message: "Your Toast Message", font: .systemFont(ofSize: 12.0))
source: https://stackoverflow.com/a/35130932/8366535
8.Try to use "IteratorProtocol"
The makeIterator() function returns an object that inherits the IteratorProtocol protocol. We can use the next() function to get the next value without the need to track the current index. Remember, the next() function returns an optional value.
let names = ["Bob", "Amy", "Mike"]
var nameIterator = names.makeIterator()
print("\(nameIterator.next() ?? "") did the laundry.")
print("\(nameIterator.next() ?? "") did the dishes.")
print("\(nameIterator.next() ?? "") did nothing.")
source: https://medium.com/geekculture/swift-5-use-protocols-to-take-your-code-to-the-next-level-a671972f2f2
9. Error Jenkins when build for production "Value of type 'UINavigationItem' has no member 'backButtonTitle' "
I don't know why in Jenkins this code get an error, when I running in Xcode that no found error but when build in Jenkins it be error, so for solution, I change back to normal backBarButton empty string
Error in Jenkins
navigationItem.backButtonTitle = ""
Solution:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)