Dynamic string replacement


#1

Hi there,

I recently posted a topic on Polymorphic methods, which I’m slowly starting to understand, thanks to the help of ibex10.

I’ve run into another issue that I’m hoping someone can help solve. I’m not quite sure how to explain the problem (or whether there’s a name for what I’m trying to do). Here’s the scenario:

I’ve got an XML document that looks something like this:


<sender>
        <name>${senderName}</name>
        <country>${senderCountry}</branch>
        <number>${senderNumber}</accountNumber>
        <school>${senderSchool}</school>
        <company>${senderCompany}</company>
</sender>

<recipient>
        <name>${recipientName}</name>
        <country>${recipientCountry}</branch>
        <number>${recipientNumber}</accountNumber>
        <school>${recipientSchool}</school>
        <company>${recipientCompany}</company>
</recipient>

The placeholders in the XML document (ie: ${senderName} ) need to be replaced from the values that exist in the objects that are passed to a method (described below). The object types that are passed into the method are of type (Person *).

Now, depending on the Person type, some of those properties may not exist. This is what the class interfaces look like:

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *country;
@property (nonatomic, copy) NSString *number;

@end

@interface Student : Person

@property (nonatomic, copy) NSString *school;

@end

@interface Worker : Person

@property (nonatomic, copy) NSString *company;

@end

The question I have is what is the best way to perform the placeholder replacements based on the Person type that’s being passed into the replacement method?

- (void)performReplacementWithSender:(Person *)sender AndRecipient:(Person *)recipient
{
        // Best way to perform these dynamic string replacements?

        // sender.school won't work, as Person doesn't have a property named "school", whereas a Student does.
        // recipient.company won't work, as Person doesn't have a property named "company", whereas a Worker does.
}

// Sample calls
///////////////////

// Ex: 1
Student *student = [Student alloc] init];
student.name = "Frankie";
student.school = "Hard Knocks";

Worker *worker = [Worker alloc] init];
worker.name = "Joe";
worker.company = "Acme Inc.";

[performReplacementWithSender:student AndRecipient:worker];

// Ex: 2
Student *studentA = [Student alloc] init];
student.name = "Frankie";
student.school = "Hard Knocks";

Student *studentB = [Student alloc] init];
student.name = "Jack";
student.school = "Some Highschool";

[performReplacementWithSender:studentA  AndRecipient];

As you can see, not every Person has a school property, nor does every Person have a company property. Student types have school, whereas Worker types have company.

While this is very vague description, I’m trying to figure out how best to perform the replacement regardless of the object that’s being used to fill the values.

Any help or guidance is very much appreciated.

Thanks!