Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7672619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:19:40+00:00 2026-05-31T16:19:40+00:00

I’m still trying to wrap my head around how things should be done in

  • 0

I’m still trying to wrap my head around how things should be done in the object-oriented world and I think my problem is that I don’t understand how to best utilize encapsulation. Specifically, I have lots of small bits of code that I use in several classes in my project. For example:

+ (NSString *)getFormattedDate;
+ (NSString *)getResultsFilePath;
+ (NSError *)removeFileFromCache:(NSString *)fileName;

These are all 3-5 line methods that I use in more than one class. My standard practice has been to put these snippets into a Utility.inc file and call them when I need them. Is that appropriate in the object-oriented world or should each class be self-contained? And if it’s appropriate, would you put the code into a singleton or just a regular class file and [[Utilities alloc] init] in each class where you want to use the methods?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:19:41+00:00Added an answer on May 31, 2026 at 4:19 pm

    Thanks for the answers. I’m not sure that this the right way to do things, but this is what I’ve done on the projects I just submitted.

    I made two classes, one for Utility methods and one for globals. The methods in the Utilities class are all class methods since they operate on files and constants or globals. Then I made a singleton for global variables. I have all of my global constants in the .pch file. Also in the .pch file I put the following two lines of code so that the utilities and globals are available everywhere.

     // File for utilities like file delete, find Documents file
        #import "Utilities.h" 
        #import "Globals.h"
    

    Accessing the methods is straightforward. Here’s an example of a call to both methods to generate an HTML header for an email.

    NSString *gameNameHeader = [NSString stringWithFormat:@"<p>&nbsp</p><h1>%@ Results</h1><h2>%@%@</h2>",GAME_NAME_TITLE,[Utilities formattedClientName], [Utilities formattedDate]];
    

    In case anyone can use it, here is my current version of the code. (Sorry for the formatting-I can’t seem to get the wiki to cooperate.)

    @interface Utilities : NSObject {
    
    
    }
    
    + (NSString *)formattedDate;
    + (NSString *)formattedClientName;
    
    + (NSString *)cachedResultsFilePath;
    + (NSString *)cachedResultsFileContents;
    + (NSString *)resultsFileName;
    + (NSError *)removeFileFromCache:(NSString *)fileName;
    
    + (NSString *)applicationCachesDirectory;
    + (NSString *)applicationDocumentsDirectory;
    + (NSString *)applicationLibraryDirectory;
    
    + (NSError *)copyCachedResultsToFile;
    @end
    
    
    
        #import "Utilities.h"
    @implementation Utilities {
    
        }
    
    + (NSString *)formattedDate {
        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *todaysDate = [dateFormatter stringFromDate:[NSDate date]];
        return todaysDate;
    }
    
    + (NSString *)formattedClientName {
        NSString *client = [NSString stringWithFormat:@" "]; 
        if( [Globals sharedInstance].currentClient ) client = [NSString stringWithFormat:@" %@ ",[Globals sharedInstance].currentClient];
        return client;
    }
    
    + (NSString *)cachedResultsFilePath {
        NSString *resultsFilePath = [[self applicationCachesDirectory] stringByAppendingPathComponent:@"Results.txt"];
        return resultsFilePath;
    }
    
    + (NSString *)cachedResultsFileContents {
        NSStringEncoding encoding; NSError* error = nil;
        NSString *resultsText = [NSString stringWithContentsOfFile:[self cachedResultsFilePath] usedEncoding:&encoding error:&error];
        return resultsText;
    }
    
    + (NSString *)resultsFileName {
        return [NSString stringWithFormat:@"%@ Results%@%@.html",GAME_NAME_TITLE,[self formattedClientName],[self formattedDate] ];
    
    }
    
    + (NSError *)removeFileFromCache:(NSString *)fileName {
        NSError *error = nil;
        NSFileManager *localFileManager=[[NSFileManager alloc] init];
        NSString *fullPath = [NSString stringWithFormat:@"%@/%@", [self applicationCachesDirectory],fileName];
        [localFileManager removeItemAtPath: fullPath error:&error ];
        return error;
    }
    
    + (NSString *)applicationCachesDirectory {
        return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    }
    
    + (NSString *)applicationDocumentsDirectory {
        return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    }
    
    + (NSString *)applicationLibraryDirectory {
    
        return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    }
    
    + (NSError *)copyCachedResultsToFile {
        // Grab the header and footer and put it around the cached data
        NSStringEncoding encoding; NSError *error = nil;
        NSString *htmlHeaderTextPath = [[NSBundle mainBundle] pathForResource:@"HTML_header" ofType:@"html" ]; 
        NSString *htmlHeaderText = [NSString stringWithContentsOfFile:htmlHeaderTextPath usedEncoding:&encoding error:&error];
    
        NSString *cachedResultsText = [NSString stringWithContentsOfFile:[self cachedResultsFilePath] usedEncoding:&encoding error:&error];
        // Write the results to a file if there are any
        if (cachedResultsText) {
            NSString *htmlFooterTextPath = [[NSBundle mainBundle] pathForResource:@"HTML_footer" ofType:@"html" ]; 
            NSString *htmlFooterText = [NSString stringWithContentsOfFile:htmlFooterTextPath usedEncoding:&encoding error:&error];
    
            NSString *gameNameHeader = [NSString stringWithFormat:@"<h1>%@ Results for%@%@</h1>",GAME_NAME_TITLE,[self formattedClientName],[self formattedDate] ];
            NSString *tempStringP1 = [htmlHeaderText stringByAppendingString:gameNameHeader];
            NSString *tempStringP2 = [tempStringP1 stringByAppendingString:cachedResultsText];
    
            NSString *formattedTextForPrinting = [tempStringP2 stringByAppendingString:htmlFooterText];
            NSString *resultsFilePath = [ [Utilities applicationDocumentsDirectory] stringByAppendingPathComponent:[self resultsFileName] ];
            if ( !([[NSFileManager defaultManager] fileExistsAtPath:resultsFilePath]) ) {
                if (! ([[NSFileManager defaultManager] createFileAtPath:resultsFilePath contents:nil attributes:nil]) ) {
                    NSLog(@"Error was code: %d - message: %s", errno, strerror(errno));
                }
            }
            NSFileHandle *fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:resultsFilePath];
            [fileHandler writeData:[formattedTextForPrinting dataUsingEncoding:NSUTF8StringEncoding]];
            [fileHandler closeFile];
    
        }
         return error;
    }
    @end
    

    Globals in a singleton. Probably not thread-safe, but I don’t care right now.

    @interface Globals : NSObject {
    
    
    }
    @property (nonatomic, strong) NSString *currentClient;
    @property (nonatomic, strong) NSString *showmePict;
    @property BOOL checkBoxes;
    
    + (Globals *)sharedInstance;
    
    - (void)resetClient;
    
    @end
    

    @implementation Globals {

    }

    static Globals *singleton = nil;
    @synthesize currentClient = _currentClient;
    @synthesize showmePict = _showmePict;
    @synthesize checkBoxes = _checkBoxes;
    
    +(Globals *) sharedInstance {
    
        NSLog (@"sharedInstance of Globals called.");
    
        if (nil != singleton) return singleton;
        static dispatch_once_t pred;        // lock
        dispatch_once(&pred, ^{             // this code is at most once
            singleton = [[Globals alloc] init];
        });
    
        return singleton;
    
    }
    
    - (void)resetClient {
        self.currentClient = nil;
    }
    
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.