Huang
Huang

Reputation: 1355

Clean implementation of architecture, Business Objec, iOS

I have a class in objective C which represents a generic business object ( let's say for my question sake its a bankaccount). I have a java C# background (FYI ). Now this bankAccount has an enum property defining what type it is, plus an NSURl property

        //this is in the bankAccount.h file
        typedef enum {Checking, Savings, Investor} AccountType;
        -(NSURL *)url;

Now users can create a new bankaccount and set the enum to the relevant type. after allocating their new bankaccount object they might need to access the url property, so I have to implement a getter for that property which will initialize it properly. My question here is that how can I know what type of a bankaccount the calling class has created of my bankaccount in order to initialize the url property correctly?

like right now this is how I am implementing url in the bankaccount.m file :

 -(NSURL *)url {   
       if (url != NULL) {
       return url;
       }
  NSString *filePath;
  // figure out the file path base on account type
  switch (AccountType) {
  }

  return url;

}

keep in mind that this is in the Bankaccount.m file, which really doesn't know in the calling class what is the instance being created is. Maybe I am confused and maybe its a simple answer but I can't wrap my head around this concept.

Appreciate the help guys.

Upvotes: 0

Views: 172

Answers (1)

Just a coder
Just a coder

Reputation: 16720

I think youre forgetting that you can't exactly save information in an enum. Save the value of the enum in some variable. You do not necessarily have to setup your code like this, but maybe this is more what you are looking for.

// BankAccount.h
#import <Foundation/Foundation.h>
typedef enum {
    Checking = 1,
    Savings = 2,
    Investor = 3
} AccountType;

@interface BankAccount : NSObject
-(void)setAccountType:(AccountType)theType; //Setter
-(NSURL *)url;
@end

// BankAccount.m
#import "BankAccount.h"
@implementation BankAccount

-(void)setAccountType:(AccountType)theType {
    self.bankAccountType = theType;
}

-(NSURL *)url {
    NSURL *someUrl;
    switch (self.bankAccountType) {
        case Checking:
            someUrl = [NSURL URLWithString:@"http://xzyChecking"];
            break;
        case Savings:
            someUrl = [NSURL URLWithString:@"http://xzySavings"];
            break;
        case Investor:
            someUrl = [NSURL URLWithString:@"http://xzyInvestor"];
            break;
        default:
            someUrl = [NSURL URLWithString:@"http://xzyError"];
            break;
    }
    return someUrl;
}

@end

Upvotes: 1

Related Questions