Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension String {
- var isValidCNH: Bool {
- let cnhLenght = 11
- if self.digitsOnly().length != cnhLenght {
- return false
- }
- var countResult = 0
- var count = 9
- for i in 0...9 {
- countResult += (Int(String(self[self.index(from: i)])) ?? 0 - 48) * count
- count = count - 1
- }
- var dsc = 0
- var firstIdentifier = countResult % cnhLenght
- if firstIdentifier >= 10 {
- firstIdentifier = 0
- dsc = 2
- }
- countResult = 0
- count = 0
- for i in 0...8 {
- count = count + 1
- countResult += (Int(String(self[self.index(from: i)])) ?? 0 - 48) * count
- }
- let remainder = countResult % cnhLenght
- let secondIndetifier = (remainder >= 10) ? 0 : remainder - dsc
- return "\(firstIdentifier)\(secondIndetifier)" == self.substring(from: self.length - 2)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement