Apple Interview Question
Software DevelopersCountry: United States
Essentially the same question:
careercup.com/question?id=4509450021896192
Since interviewers gives milliseconds and might have a devices background, depending on allowed jitter, you might need to use RTC events, if that's easily available on your OS. You can craft this into the entioned algo.
I guess that's why it's a decent question to ask.
Some fundamentals behind this would be knowing what a function inside of a function is? Apple refers to this as a closure expression, and can be short handed as a trailing closure.
Good examples of these are URLSessions and UIView.animate methods. You're essentially passing a closure (which is a function) and saying, "I'm not sure what the value is, but here's a recipe on how to use the value when you get it."
Sleeping would work but you'll most likely doing that on main which isn't the best idea. A good example of using both of these together would be to actually just do:
func execute(after time: Int, f: @escaping () -> Void) {
DispatchQueue.main.async(deadline: .now() + .milliseconds(time), execute: f)
}
// Usage:
execute(after: 250) { [weak self] in
self?.title = "Example"
}
The weak self is known as a capture list which protects from creating reference cycles and executing when the instance has been deinitialized.
This question was asked to one of my friends, so I am not sure about the exact question details. I have the following questions:
- lkjhgfdsa December 10, 20171. How is a function passed to another function?
2. Can't we just use the sleep method?
I think this to be a system design question.