2

I have the following:

@implementation DataSource
+ (NSArray *)someData
{
  static NSArray *data = nil;
  if (!data) {
    data = [[NSArray arrayWithObjects:..., nil] retain];
  }
  return data;
}
@end

Is there a way to access the class method from the class it self?

5 Answers 5

2
NSArray *array = [DataSource someData];
Sign up to request clarification or add additional context in comments.

Comments

0

Yes. Inside a class method like your someData you can call another class method like

[self anotherClassMethod]. 

Here self refers to class.

Comments

0
NSArray *accessor = [DataSource someData];

The + refers to class level access.

Comments

0

In other class methods, you can call it as [self someData]. From instances of the class, you can call it as [[self class] someData] (this has the nice property that subclasses can override it and their implementations will automatically be used as appropriate). From outside the class, you can call it as [DataSource someData].

BTW, if this is actually meant to be the data source for some Cocoa or Cocoa Touch class such as NS/UITableView, you should probably implement the class as a singleton instead of making the class itself the data source, because using classes as data sources is not well-tested and the lack of instance variables will probably become a pain as your program grows.

Comments

0

a static method can be called with name of class. Above static method return type NSArray so we can use anywhere

NSArray  *Arr=[DataSource someData];

Comments

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.