import Cocoa
let start = playground.startIndex
let end = playground.index(start, offsetBy: 4)
let range = start...end
let firstFive = playground[range]
// Bronze Challenge
let empty = ""
if empty.startIndex == empty.endIndex {
print("This string is empty.")
} // only when string is empty both startIndex en endIndex properties are equal (0)
if empty.isEmpty {
print("This string is empty.")
} // cleaner way with the isEmpty property
// Silver Challenge
var unicodeScalarsString = ""
for scalar in firstFive.unicodeScalars {
var unicode = String(format: "%04X", scalar.value)
unicodeScalarsString += "\\u{\(unicode)}"
}
print (unicodeScalarsString)
This line of code really interests me. How would I go about reading Apple’s documentation on it? I think I found it in "Xcode > Help > Documentation and API Reference" under "Foundation > String > init(format:_:)" but it doesn’t explain the semantics of the format: string.
The current API documentation for Swift is pretty disappointing: not even a decent table of contents let alone an explanation for “the semantics of the format: string.”
For the silver challenge, I took the “replace” to mean that we should actually be replacing the characters within the original string, and did the following:
var playground = “Hello, playground"
let start = playground.startIndex
let end = playground.index(start, offsetBy: 4)
playground.replaceSubrange(start…end, with:”\u{0048}\u{0065}\u{006C}\u{006C}\u{006F}")