9 Notes April 2021
1. How to detect Push Notification content
I have a problem when getting multiple notifications to need a different direct page so
this one of the solution filter by content notification
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let detectTitleContent = response.notification.request.content.title
print(detectTitleContent ) //"title content"
}
2 .Extension string convert data in string to dictionary
example case when get data like this below
"errorMessage" : "{\"de\":\"Bezahlung fehlgeschlagen! Die maximale Zahlung ist $ 5.000.\",\"en\":\"Payment failed! Maximum payment is $ 5.000.\"}",
extension String {
func convertToDictionary() -> [String: Any]? {
if let data = self.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
}
3. Freeze when camera stop recording IPhone X
When I use a timer for auto stop recording in iPhone X it gets to freeze, if I use another iPhone it's running normally
so this is my solution
Before
func startTimeRecording(maxTimeInSec: Int = 10) {
var currentTime: Int = 0
isRecording = true
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { _ in
DispatchQueue.main.async {
currentTime += 1
self.timeLabel.isHidden = false
self.timeLabel.text = String(format: "00:%02i", currentTime)
if currentTime >= maxTimeInSec {
self.timer?.invalidate()
self.takeButton.isSelected = false
Loading.show()
DispatchQueue.global().asyncAfter(deadline: .now() + Double(maxTimeInSec)) {
self.cameraManager.stopVideoRecording { [weak self] url, _ in
self?.handleStopVideo(url: url)
}
}
}
}
})
}
After
func startTimeRecording(maxTimeInSec: Int = 10) {
var currentTime: Int = 0
isRecording = true
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { _ in
DispatchQueue.main.async {
currentTime += 1
self.timeLabel.isHidden = false
self.timeLabel.text = String(format: "00:%02i", currentTime)
if currentTime >= maxTimeInSec {
self.timer?.invalidate()
self.takeButton.isSelected = false
Loading.show()
}
}
})
DispatchQueue.global().asyncAfter(deadline: .now() + Double(maxTimeInSec)) {
self.cameraManager.stopVideoRecording { [weak self] url, _ in
self?.handleStopVideo(url: url)
}
}
}
4. What is a Variadic Parameter?
A Variadic Parameter is defined using a value type followed by 3 dots, for example:
mutating func add(_ newContent: Content...) {
content.append(contentsOf: newContent)
}
source: https://www.avanderlee.com/swift/variadic-parameters/
5. Diffrence dispaque main and global.
Difference between dispaque.main.asyc
and dispaque.global()
is Dispatched: means put task in queue
Global
- if Global queue dispatched in Main queue as sync, the dispatched task will work on same thread of Main queue and dispatched task added to Global queue, And this task will freezing the thread
- If Global queue dispatched in Main queue as async , the dispatched task will work on other thread of Main queue and dispatched task added to Global queue, And this task will not freezing the thread
Main
- If Main queue dispatched in Main queue as async , the dispatched task will work on same thread of Main queue
- If Main queue dispatched in Main queue as sync will make exception because make deadlock
6. System under test
System under test (SUT) refers to a system that is being tested for correct operation. According to ISTQB it is the test object. The term is used mostly in software testing.
7. What map() ?
The word all three methods share is “map”, which in this context means “transform from one thing to another.” So, the map() method lets us write code to double all the numbers in an array:
example:
let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 }
8. What flatMap() ?
flatMap(): transform then flatten
You’ve now seen map() transforming an array of integers into an array of integers (doubling them), transforming an array of integers into an array of strings, and transforming an array of strings into an array of integers. That last transformation returned optional integers, so we also looked at how compactMap() will perform the same transformation but then unwrap the optionals and discard any nil values.
Then we looked at how map() works on optionals: if it has a value it gets unwrapped, transformed, and rewrapped, but if it is nil then it stays as nil.
Now consider this code:
let number: String? = getUser(id: 97)
let result = number.flatMap { Int($0) }
9. What compactMap() ?
compactMap(): transform then unwrap
Working with optionals can be annoying, but compactMap() can make life much easier: it performs a transformation (the “map” part of its name), but then unwraps all the optionals and discards any that are nil.
So, this line of code does the same string to integer conversion, but results in an array of integers rather than an array of optional integers:
let definitelyNumbers = strings.compactMap { Int($0) }
No comments: