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 8165209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:29:02+00:00 2026-06-06T19:29:02+00:00

I am trying to use a singleton to store variables that will be used

  • 0

I am trying to use a singleton to store variables that will be used across multiple view controllers. I need to be able to get the variables and also set them. How do I call a method in a singleton to change the variables stored in the singleton.

 total+=1079;
[var setTotal:total];

where var is a

static Singleton *var = nil;

I need to update the total and send to the setTotal method inside the singleton. But when I do this the setTotal method never gets accessed. The get methods work but the setTotal method does not. Please let me know what should. Below is some of my source code

   //
//  Singleton.m
//  Rolo
//
//  Created by  on 6/28/12.
//  Copyright (c) 2012 Johnny Cox. All rights reserved.
//

#import "Singleton.h"

@implementation Singleton
@synthesize total,tax,final;

#pragma mark Singleton Methods

+ (Singleton *)sharedManager
{
    static Singleton *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[Singleton alloc] init];
        // Do any other initialisation stuff here
    });
    return sharedInstance;
}

+(void) setTotal:(double) tot
{
    Singleton *shared = [Singleton sharedManager];
    shared.total = tot;
    NSLog(@"hello");
}
+(double) getTotal
{
    Singleton *shared = [Singleton sharedManager];
    NSLog(@"%f",shared.total);
    return shared.total;
}
+(double) getTax
{
    Singleton *shared = [Singleton sharedManager];
    NSLog(@"%f",shared.tax);
    return shared.tax;
}
@end


//
//  Singleton.h
//  Rolo
//
//  Created by  on 6/28/12.
//  Copyright (c) 2012 Johnny Cox. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Singleton : NSObject
@property (nonatomic, assign) double total;
@property (nonatomic, assign) double tax;
@property (nonatomic, assign) double final;

+ (id)sharedManager;
+(double) getTotal;
+(void) setTotal;
+(double) getTax;

@end
  • 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-06-06T19:29:03+00:00Added an answer on June 6, 2026 at 7:29 pm

    You have attempted to implement your getters and setter as class methods instead of instance methods. Attributes are properties of your singleton, not of its class. For more on instance methods vs class methods in objective-c see here or here

    You’ll have better luck with something more like:

    -(double) getTotal;
    -(void) setTotal:(double) total;
    -(double) getTax;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to work out the correct pattern to use for a Singleton that
I'm trying to unit-test some classes that make use of a Singleton class whose
I have a singleton here that I am trying to use the same instance
I'm trying to use Google Guice with the @Inject and @Singleton properties as follows:
I'm trying to create a simple to use singleton class to connect to mysql
I am trying to use the singleton method to access a global object (in
I'm trying to create a Singleton object that deals with servicing an event listener:
I'm trying to fix a design flaw that I recently ran across in some
I'm trying to use a Singleton Class (its a program-wide debug logger called 'PrisLog')
I am trying to create a modular structure that will eventually live inside another

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.