0

I have a class method which takes an Array as the parameter

+(void)classMethod:(NSArray*)array;

A message is being sent to this method from an outside ViewController with the value of the array.

[ViewController classMethod:ValueofArray]

Now i want to access the value of that array from an instance method in the same ViewController as the Class method

-(void)instanceMethod;

How is that possible ?

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
newiosguy
  • 29
  • 1
  • 7

4 Answers4

2

You would have to create a static class level variable to hold the value.

For example:

@implementation SOMyObject

static NSArray *thisArray;


+(NSArray*) thisArray
{
    return thisArray;
}

+(void) myMethod:(NSArray*) array;
{
    thisArray = array;
}

-(void) doWork
{
    //[SOMyObject thisArray]
}

@end
John Koerner
  • 37,428
  • 8
  • 84
  • 134
1

You wrote:

Now i want to access the value of that array from an instance method in the same ViewController as the Class method

But the point of a class method is that it is not an instance of the class. You use a class method when you don't need to see any of the variables or properties of class. For example a method like [MyClass countVowels:someString]; could perform its actions without any 'state', so it could be a candidate for a class method.

On the other hand, if you write "[myClass countVowels];" it implies something like this:

MyClass * myClass = [[MyClass alloc] initWithString:@"some string]];
int howMany = [myClass countVowels];

This 2nd version would presumably store the string passed to the init method, and later count the vowels in that string.

Hope that helps.

Dave
  • 7,552
  • 4
  • 22
  • 26
  • I have mixed feelings on this answer. In such, a class method could indeed perform a function (as per the example that you mention), but there's no reason why it can't be used to set (or increment/decrement) a shared static variable. – JRG-Developer Feb 01 '13 at 04:28
  • Very true, but that should be left to folks who know how to do that and all the ramifications of such code. I was tempted to mention the common case of singleton; or how the static method could grab the 'sharedInstance' of that class; but didn't want to lead him down that rabbit hole. ;) – Dave Feb 01 '13 at 04:34
0

You could create a static class variable, outside the header and implementation of the class.

Here's an example:

// MyClass.h

@interface MyClass : NSObject
+ (void)setClassArray:(NSArray *)array;
+ (NSArray *)classArray;
@end


// MyClass.m

#import "MyClass.h"

static NSArray *_myClassArray;

@implementation MyClass

+ (NSArray *)classArray
{
    return _myClassArray;
}

+ (void)setClassArray:(NSArray *)array
{
    if (_myClassArray != array)
    {
        _myClassArray = array;
    }
}

@end

Hence, you would set the value of the class array like this:

[MyClass setClassArray:[NSArray arrayWithObject:@"Some_Objects"]];

Within an instance of the class, you could access it like this:

- (void)myInstanceMethod
{
    // setting to another ivar is optional and only shown for clarity

   NSArray *array = _myClassArray;
    // ... do whatever you want to do with it here...
}
JRG-Developer
  • 12,454
  • 8
  • 55
  • 81
0

It is true that class methods are usually (and supposed to be) state-less functional entities, what you're looking for is possible. In this case, there is a single array that all instances of MyViewController want to use, so other classes setting that info just don't care.

MyViewController.m

static NSArray *staticArray;

+ (void)aClassMethod:(NSArray *)array
{
    staticArray = array;
}

- (void)anInstanceMethod
{
    NSLog(@"My staticArray: %@", staticArray);
}

All that said, it's a pretty specific and non-standard case. You might rethink your use case, maybe you actually want a delegate pattern, a default singleton like [NSNotificationCenter defaultCenter], or something else. Or you actually have a good use case for this. Nice.

In any case, there you have it.