Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. extension String {
  2. var isValidCNH: Bool {
  3. let cnhLenght = 11
  4. if self.digitsOnly().length != cnhLenght {
  5. return false
  6. }
  7.  
  8. var countResult = 0
  9. var count = 9
  10.  
  11. for i in 0...9 {
  12. countResult += (Int(String(self[self.index(from: i)])) ?? 0 - 48) * count
  13. count = count - 1
  14. }
  15.  
  16. var dsc = 0
  17. var firstIdentifier = countResult % cnhLenght
  18.  
  19. if firstIdentifier >= 10 {
  20. firstIdentifier = 0
  21. dsc = 2
  22. }
  23.  
  24. countResult = 0
  25. count = 0
  26.  
  27. for i in 0...8 {
  28. count = count + 1
  29. countResult += (Int(String(self[self.index(from: i)])) ?? 0 - 48) * count
  30. }
  31.  
  32. let remainder = countResult % cnhLenght
  33. let secondIndetifier = (remainder >= 10) ? 0 : remainder - dsc
  34. return "\(firstIdentifier)\(secondIndetifier)" == self.substring(from: self.length - 2)
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement