0

this is my add button:

guard let name = texfiedldName.text else { return }
guard let phoneNumber = texfiedldAge.text else { return }
guard let email = texfiedldEmail.text else { return }

formatting text

let scanner = Scanner(string: phoneNumber)
let validCharacters = CharacterSet.decimalDigits
let startCharacters = validCharacters.union(CharacterSet(charactersIn: "+#"))
var digits: NSString?
var validNumber = ""
while !scanner.isAtEnd {
    if scanner.scanLocation == 0 {
        scanner.scanCharacters(from: startCharacters, into: &digits)
    } else {
        scanner.scanCharacters(from: validCharacters, into: &digits)
    }
    scanner.scanUpToCharacters(from: validCharacters, into: nil)
    if let digits = digits as String? {
        validNumber.append(digits)
    }
}
print(validNumber)

add to Core Data

let person = Person(context: PersistenceService.context)
person.name = name
person.number = Int64(validNumber)!
person.email = email
PersistenceService.saveContext()
self.people.append(person)

this is my output

085293173

but in the tableViewCell

85293173

can you help me please?

Fabio
  • 5,432
  • 4
  • 22
  • 24
  • 1
    Integer values never have leading zeros – vadian May 08 '18 at 06:26
  • check this https://stackoverflow.com/questions/25566581/leading-zeros-for-int-in-swift – Abdelahad Darwish May 08 '18 at 06:32
  • @vadian and why I put in another position return me the correct number in my cell? 3200582527 – Fabio May 08 '18 at 06:38
  • Don't use integers to store phone numbers. – Martin R May 08 '18 at 06:48
  • Essentially a duplicate of https://stackoverflow.com/questions/15743156/nsstring-to-nsnumber-ignores-initial-zeros. – Martin R May 08 '18 at 06:53
  • @Martin R but I want it in Swift... my entity is in Int64... I formatting that in string? – Fabio May 08 '18 at 07:04
  • @Fabio: The answer is still the same: Use a *string* to store the phone number, not an integer. That is independent of the language. – Martin R May 08 '18 at 07:05
  • @Martin R if my entity is in Int64, I formatting the into String? – Fabio May 08 '18 at 07:07
  • Again: Use a *String* attribute for the Core Data entity, **not** Int64. – Martin R May 08 '18 at 07:08
  • do not lose patience bro, ok I try.... and excuse me – Fabio May 08 '18 at 07:11
  • The integer 0000009 is the same as just 9, but the phone number 000008 is not the same as 8, you better change the entity to string – Daniel Krom May 08 '18 at 07:19
  • @DanielKrom I try but return this error: libswiftCore.dylib`_swift_runtime_on_report: -> 0x10d372e70 <+0>: pushq %rbp 0x10d372e71 <+1>: movq %rsp, %rbp 0x10d372e74 <+4>: popq %rbp 0x10d372e75 <+5>: retq 0x10d372e76 <+6>: nopw %cs:(%rax,%rax) – Fabio May 08 '18 at 07:28
  • thx to MartinR and DanielKrom...the error depended on an inexplicable cash ... I created a new entity and all is done :) – Fabio May 08 '18 at 07:51

0 Answers0