Dynamic, localized NSString

I need to build an NSString that resembles the following:

Name: Craig Buchanan
Phone: 800-555-1212
Email: name@company.com

Where:

  • each row (e.g. phone) is included or excluded based on the UISwitch value
  • the key part of the line (ie the part to the left of ":") is localized
  • part of the value 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 (am I objective-c noob)?
  • I understand that my array is auto-implemented, but still I am concerned about memory usage. Should I release an auto-release pool? seems a little dangerous.
  • . , , UISwitch , . ?

,

+3
1

, , :

[values addObject:[NSString stringWithFormat:NSLocalizedString(@"Name: %@", @"Name line"), name];

, :

NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
// Do stuff.
[myPool release];

, switch, . , IBOutlet ; -viewForTag: . NSIndexSet, , . , , , . , .

+1

Source: https://habr.com/ru/post/1713276/


All Articles