I need to build an NSString that resembles the following:
Name: Craig Buchanan
Telephone: 800-555-1212
Email: [email protected]
Where:
- each line (e.g. Telephone) is included or excluded based on the value of a UISwitch
- the key part of the string (i.e. the part to the left of the ':') is localized
- the value part is from a UITextField.
My approach:
NSMutableArray *values = [[NSMutableArray alloc] initWithCapacity:3];
if (self.nameSwitch.isOn)
[values addObject:[NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"Name", @"Name label"), textFieldName.text]];
if (self.telephoneSwitch.isOn)
[values addObject:[NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"Telephone", @"Telephone number label"), textFieldTelephone.text]];
if (self.emailSwitch.isOn)
[values addObject:[NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"Email", @"Email address label"), textFieldEmail.text]];
return [values componentsJoinedByString:@"\r"];
I have a few questions:
- is this a decent approach (i'm an objective-c noob)?
- i realize that my array is autoreleased, but still i'm concerned about memory usage. should i release the auto-release pool? seems a bit dangerous.
- i'm hoping to make the code a bit more dynamic. my initial thought is to create an array of outlet variables, then use the UISwitch's tag to store the key that drives the localization. thoughts?
Thanks for your time,
Craig Buchanan