0

I have a shared singleton classNSMutableArray [ICGlobals sharedApplianceCount](first time using this pattern so bear with me if ive done something really silly here)

.h

#import <Foundation/Foundation.h>

@interface ICGlobals : NSObject

{

NSMutableArray* applianceCount;
}


@property (nonatomic, retain) NSString *applianceCount;

+ (ICGlobals *)sharedApplianceCount;

@end

.m

#import "ICGlobals.h"

@implementation ICGlobals

static ICGlobals *sharedApplianceCount = nil;


+ (ICGlobals *)sharedUser {
if(sharedApplianceCount == nil){
    sharedApplianceCount = [[super allocWithZone:NULL] init];
  }
return sharedApplianceCount;
}

+ (id)allocWithZone:(NSZone *)zone {
return [self sharedApplianceCount];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
@end

In "another view controller" im trying to add the row count of my table view (changeable amount of rows) = self.circuits.count

Having tried this

[[ICGlobals sharedApplianceCount] addObject: self.circuits.count,nil]];

and

[[ICGlobals sharedApplianceCount]   = [[NSMutableArray alloc] init];

[[ICGlobals sharedApplianceCount] addObject: self.circuits.count,Nil]];

I get no visible @interface error saying my singleton class declares the selector

same with

NSNumber* numberOfRows = [NSNumber numberWithInteger:self.circuits.count];

[[ICGlobals sharedApplianceCount]addObject:[NSMutableArray arrayWithObjects:numberOfRows, nil]];

and with

[ICGlobals sharedApplianceCount] = self.circuits.count;

I get expression assignable. Singleton class has been imported.

2
  • Are trying to make a singleton ? Commented Feb 23, 2014 at 16:03
  • trying to add objetcs to it in another view controller Commented Feb 23, 2014 at 16:06

2 Answers 2

1

You have an inconsistency in your interface declaration. You declare ivar of type NSMutableArray and then a NSString property. Firstable, you don't need to declare ivar, declaring a property does it for you. So your interface should look like:

@interface ICGlobals : NSObject

@property (nonatomic, retain) NSMutableArray *applianceCount;

+ (ICGlobals *)sharedApplianceCount;

@end

Furthermore, you have a naming glitch. You should not use name applianceCount for an array. In general, naming convention of Cocoa suggests that count should be a number (int or NSUInteger). I would change this property name to applianceCounts.

Then, when you initialize your singletone, you can also initialize the array:

+ (ICGlobals *)sharedUser 
{
    if(sharedApplianceCount == nil)
    {
        sharedApplianceCount = [[super allocWithZone:NULL] init];
        sharedApplianceCount.applianceCounts = [[NSMutableArray alloc] init];
    }

    return sharedApplianceCount;
}

Finally, here is how to add data to your singletone's applianceCounts array from view controller.

NSNumber* numberOfRows = [NSNumber numberWithInteger:self.circuits.count];

[[ICGlobals sharedApplianceCount].applianceCounts addObject:numberOfRows];

This should point you to right direction. I don't fully get what you are trying to achieve like I don't understand why you want to have an array there, so if you need further help please let me know in the comments.

I fully recommend you reading about naming conventions. A good start is this article:

Introduction to Coding Guidelines for Cocoa.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for explaining that to me, first time with singleton and thats a great example. Im storing the row count in an array so I can use this figure for a calculation in another view/s. (and learn about singletons) If theres a more efficent way of doing then I would explore that. Using app dleegate seem frowned upon
Getting: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[ICGlobals sharedApplianceCount]: unrecognized selector sent to class 0x18b330' using the above code?
1

I would recommend some refactoring to your class. First you make the interface like this:

@interface ICGlobals : NSObject
// add the app count but make it private, because you will provide methods to access it
@property (nonatomic, readonly) NSString *applianceCount;

// return ICGlobals instance 
+ (ICGlobals)sharedCounter;

- (NSInteger)count;
- (void)addObject:(id)object;

now in .m file

@implementation ICGlobals

static ICGlobals *sharedApplianceCount = nil;
// this is your method, just changed the name
+ (ICGlobals *)sharedCounter {
if(sharedApplianceCount == nil){
    sharedApplianceCount = [[super allocWithZone:NULL] init];
  }
return sharedApplianceCount;
}

// instance methods goes here 
- (NSInteger)count
{
    return _applicationCount.count;
}

- (void)addObject:(id)object
{
    [_applicationCount addObject:object];
}

Now call [[ICGlobals sharedCount]addObject:object] from any viewController

2 Comments

This gives me <ICglobals2: 0x17571850> in my log. I have changed @property (nonatomic, readonly) NSString applianceCount; to a mustabel array and + (ICGlobals)sharedCounter; to + (ICGlobals)sharedCounter;
No + (ICGlobals)sharedCounter; needs *, @property (nonatomic, readonly) NSString *applianceCount; I think needs to be Mutable array? [[ICGlobals sharedCount]addObject:object] needs to be [[ICGlobals sharedCounter]addObject:object] Done those but just get <ICGlobals: 0xc1134d0> when logged

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.