Calling multiple methods at once


#1

When populating an object its frustrating to type the name of the object over and over again.
i.e. [item setItemName:@“Red Sofa”];
[item setSerialNumber:@“A1B2C”];

is there a way to populate item with just one line?

I came up with this solution… it works, but is it optimal?

CMKItem.h

#import <Foundation/Foundation.h>

@interface CMKItem : NSObject
{
    NSString *_itemName;
    NSString *_serialNumber;
    int _valueInDollars;
    NSDate *_dateCreated;
}

- (void)setItemName:(NSString *)str;
- (NSString *)itemName;

- (void)setSerialNumber:(NSString *)str;
- (NSString *)serialNumber;

- (void)setValueInDollars:(int)v;
- (int)valueInDollars;

- (NSDate *)dateCreated;

// Declare the multi method method.
- (void)multiItemName:(NSString *)itemName serialNumber:(NSString *)sN valueInDollars:(int)vID;

@end

CMKItem.m


#import "CMKItem.h"

@implementation CMKItem

// override the init method
- (instancetype)init
{
    self.serialNumber = @"100920";
    self.itemName = @"CMKCorp Item";
    self.valueInDollars = 50;
    
    return self;
}

- (void)setSerialNumber:(NSString *)str
{
    _serialNumber = str;
}
- (NSString *)serialNumber
{
    return _serialNumber;
}
- (void)setItemName:(NSString *)str
{
    _itemName = str;
}
- (NSString *)itemName
{
    return _itemName;
}
- (void)setValueInDollars:(int)v
{
    _valueInDollars = v;
}
- (int)valueInDollars
{
    return _valueInDollars;
}
- (NSDate *)dateCreated
{
    return _dateCreated;
}
// implement the multi method method.
- (void)multiItemName:(NSString *)itemName serialNumber:(NSString *)sN valueInDollars:(int)vID
{
    [self setItemName];
    [self setSerialNumber:sN];
    [self setValueInDollars:vID];
}

main.m

// set a pointer to a new instance of CMKItem
CMKItem *firstItem = [CMKItem new];

// populate the instance of CMKItem with objects and a value.
[firstItem multiItemName:@"Red Sofa" serialNumber:@"A1B2C" valueInDollars:100];

// log the values set in CMKItem
NSLog(@"%@\n%@\n%d\n%@\n",
              firstItem.itemName,
              firstItem.serialNumber,
              firstItem.valueInDollars,
              firstItem.dateCreated);

#2

[quote][code]// set a pointer to a new instance of CMKItem
CMKItem *firstItem = [CMKItem new];

// populate the instance of CMKItem with objects and a value.
[firstItem multiItemName:@“Red Sofa” serialNumber:@“A1B2C” valueInDollars:100];

[/code][/quote]
That is very unorthodox.

Use a convenience constructor, a class method that creates a fully configured object:

CMKItem *firstItem = [CMKItem itemWithName:@"Red Sofa" serialNumber:@"A1B2C" valueInDollars:100];
...
...
@interface CMKItem : NSObject
{
...
}
...
+ (instancetype)itemWithName:(NSString *)n serialNumber:(NSString *)s valueInDollars:(int)v;
@end

[Become a competent programmer faster than you can imagine: pretty-function.org]


#3

If you’ve not yet noticed, ObjC is all about convention. At the very least, the “multi…” name provides zero clue as to what it’s really doing. But even trying something like “multiSetItemName:…” still doesn’t help much.

If it’s the typing that’s the issue, perhaps a change in technique would help. First type

   [item set

then copy the text, complete the line, and paste/complete for as many additional setters as you need.

To assist with more complex repetitive typing, check out the “Code Snippet Library” section of chapter 8, pages 174-176.