func BNRTimeBlock(_ block: @noescape () -> Void) -> TimeInterval {
var info = mach_timebase_info()
guard mach_timebase_info(&info) == KERN_SUCCESS else { return -1 }
let start = mach_absolute_time()
block()
let end = mach_absolute_time()
let elapsed = end - start
let nanos = elapsed * UInt64(info.numer) / UInt64(info.denom)
return TimeInterval(nanos) / TimeInterval(NSEC_PER_SEC)
}
And call it like
let time1 = BNRTimeBlock {
print("groovy")
// do other work.
}
print(" took (time1)")
print?
Just adopt CustomStringConvertible and implement description:
extension Weight: CustomStringConvertible {
var description: String {
return "(pounds) lbs"
}
}
If you're inheriting from NSObject, then you actually already have a description method, and will get an error to the tune of "redundant conformance of blah blah blah". In that case, you'd do this:
extension IndoorCyclingClass {
override var description: String {
return "(title) - (classDescription)"
}
}
enum FilterOptionValue {
case oneSided(value: Double)
case twoSided(low: Float, high: Double)
}
You can unpack it piecewise on demand with
if case let .twoSided(low, high) = thingieOption.value {
minThingie = Int(low)
upperThingie = Int(high)
}
#if 0 to tear out chunk of your file.
#if in Swift generally requires the guts of the conditional to be syntactically correct (even if it's semantically nonsense).
You can abuse the Swift version check to do something similar to #if 0:
...
#if swift(>=666)
aj2n42j4n23jnjsdnfjsnfjnunrun unr unwu nudjfn jsnf jn
var window: UIWindow?
#endif
...
The compiler still does some processing of the code, so you might get an error (like if that cat-stomp at the beginning started with a digit, Swift tries interpreting 2n42j4n23jnjsdnfjsnfjnunrun as an integer literal and fails)
(Thanks to Jeremy Sherman for the idea)
NotificationCenter.default.post(name: Notification.Name("FishBattery"), object: tour)
Receiving end
NotificationCenter.default.addObserver(
forName: Notification.Name("FishBattery"),
object: tour,
queue: OperationQueue.main) { [weak self] notification in
guard let tour = notification.object as? Tour else {
return
}
if tour == self?.tour {
self?.uploadIfNeeded()
}
}
(lldb) expr -l swift -- let $ook = "verb"
(lldb) expr -l swift -- print("I seem to be a ($ook)")
I seem to be a verb
And if you want to call your own stuff (say the project name is C-Interop):
(lldb) expr -l swift -- import C_Interop (lldb) expr -l swift -- SomeClass().useAnother()This creates a new instance of
SomeClass and calls the useAnother method.
(Thanks to Zach Waldowski for this one.)