I have the following code to read a credit card number from a text field and store it in a datamodel. The card number is correct when it is stored but later when I try to read from it it has become gibberish. Now I am guessing this is because the memory is reclaimed and it no longer makes sense.
Code to set the card number from the label:
[cardInfo setCardNumber: [textField.text cStringUsingEncoding:[NSString defaultCStringEncoding]]];
Code in the data model:
- (void) setCardNumber:(char *)number{
cardNumber = number;
//Value of cardNumber here is correct
}
Everything looks correct until I use the property to retrieve the value at a later time.
From the docs for
-[NSString cStringUsingEncoding:]:Also, you should never use
+[NSString defaultCStringEncoding]. It’s completely unreliable and can’t generally represent arbitrary strings. From the docs: