9 Notes May 2021
1. Codable = Decodable + Encodable
What is Codable? Codable is to convert the JSON data object to an actual Swift class or struct and vice versa.
It’s usually used when the case you wanna retrieve or parse JSON data from the server like the APIs.
source: https://medium.com/doyeona/codable-decodable-encodable-feat-json-5643dc3d7766
2. In Xcode Version 12.5 warning when creating a class in protocol
Xcode 12 when I create protocol "class" it be a warning, you should be changed to "AnyObject"
3. Amount Round down swift
let amountRoundDownOneHundred = 123456
print((amountRoundDownOneHundred/100) * 100) // 123400
4. Error use windows UIScreen
Error when use -> self.window = UIWindow(frame: UIScreen.main.bounds)
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'
terminating with uncaught exception of type NSException
CoreSimulator 757.5 - Device: iPhone 12 (0000-0B56-000-B641-0000) - Runtime: iOS 14.5 (18E182) - DeviceType: iPhone 12
Solution if simulator or device run in ios 13, * delete self.window = UIWindow(frame: UIScreen.main.bounds) . if you don't have sceneDelegate
5. Blink Black screen after lunchscreen
Solution:
Call topOverlay function in didFinishLaunchingWithOptions Appdelegate:
private func topOverlay() {
self.window?.windowLevel = UIWindow.Level(rawValue: CGFloat.greatestFiniteMagnitude)
self.window?.backgroundColor = .darkSkyBlue
self.window?.frame = UIScreen.main.bounds
self.window?.rootViewController = UIViewController()
self.window?.alpha = 1.0
self.window?.isHidden = false
self.window?.makeKeyAndVisible()
}
6. Convert Date time format
Convert date time with string data 2021-05-20 13:47:31 to this string data Thursday, May 20, 2021 - 13:47. so follow this code below.
private func dateConvert(strDate: String) -> String {
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "EEEE, MMM d, yyyy - HH:mm"
if let date = dateFormatterGet.date(from: strDate) {
return dateFormatterPrint.string(from: date)
} else {
return strDate
}
}
result:
let dateFrom = "2021-05-20 13:47:31"
prtint(dateConvert(strdate: dateFrom))
// Thursday, May 20, 2021 - 13:47
7. Rotate UIImageView or UIView
use this extension
/**
Rotate a view by specified degrees
- parameter angle: angle in degrees
*/
func rotate(angle: CGFloat) {
let radians = angle / 180.0 * CGFloat.pi
let rotation = self.transform.rotated(by: radians)
self.transform = rotation
}
8. Fastlane command not found in terminal
zsh: command not found: fastlane
in terminal:
$ sudo gem install fastlane -NV
source: https://stackoverflow.com/a/43088169/8366535
9. Error pod install in Big Sur
When running in OS BigSur and you run pod install in terminal get this error
Your Podfile requires that the plugin `cocoapods-binary` be installed.
this one of solutions I've found
$sudo arch -x86_64 gem install ffi
$arch -x86_64 pod install
No comments: