Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. extension String {
  2.  
  3. func index(at position: Int, from start: Index? = nil) -> Index? {
  4. let startingIndex = start ?? startIndex
  5. return index(startingIndex, offsetBy: position, limitedBy: endIndex)
  6. }
  7.  
  8. func character(at position: Int) -> Character? {
  9. guard position >= 0, let indexPosition = index(at: position) else {
  10. return nil
  11. }
  12. return self[indexPosition]
  13. }
  14. }
  15.  
  16. if let character = str.character(at: 3) {
  17. print("I found \(character)") // Нашел 1
  18. }
  19.  
  20. if let character = str.character(at: 31) {
  21. print("I found \(character)") //Не найдет так как число слишком большое
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement