-3

I have this code:

.h

@interface DetalhesPod : UIViewController { NSString *linhaPod; } @property (nonatomic, strong) NSString *linhaPod; 

.m

+ (NSArray *)_tracks { NSArray *arrTexto = [self.linhaPod componentsSeparatedByString:@"#"]; } 

Why I have problem with "+" in "self.linhaPod" ? If I put "-" I don't have problem:

- (NSArray *)_tracks { } 

Error message: instance variable "linhaPod" accessed in class method...

Thanks

3
  • 2
    Not an Xcode question. --- Now just think about it. It doesn't make sense to call an instance method on self from within a class method. How would the class method know which particular object (instance) to call the instance method on? Commented Dec 17, 2013 at 19:11
  • so how can I access this variable "linhaPod" in class "_tracks"? Is possible or no? Commented Dec 18, 2013 at 10:23
  • @userXXX No, you can't. Commented Dec 18, 2013 at 10:23

2 Answers 2

4

What you're seeing here is the difference between a class and an instance of that class. Each instance of the class has its own linhaPod instance variable — in one instance, it might point to the string @"bob" and in another it might be @"andy". The class is an entity of its own. self in a class method refers to the class itself, not to any instance. So what would it mean to access this variable from the class itself? The instance variable only exists in instances (hence why it is called an instance variable).

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

Comments

0

You can't reference a property from a static, or class method (in your case, _tracks). This is because class methods don't operate on an object, and the notion of an object property value makes no sense if you don't have an object. Class methods can only use other class methods and static variables from the same class. Check out the Wikipedia article on static methods (this concept is common to many programming languages, including Objective-C). It's a fundamental concept in programming and really worth learning about.

15 Comments

@jlehr Could you explain that a bit more? For example, what is the difference between an ObjC class method and a Java static method? I was specifically talking about static member functions, BTW.
Class methods in Objective-C are dispatched using exactly the same mechanism as instance methods -- in other words they're looked up dynamically, so they're not static. As a consequence, they're inherited the same way instance methods are, and can be overridden in subclasses. Note that self and super are visible in Objective-C class methods, which is not the case for static methods in Java.
@BlackRider: No, that isn't valid. It wouldn't work in an instance method, either. super isn't an identifier, but a keyword. It has to appear in the receiver position of a message send (i.e. [super something]. If you had done NSLog(@"super: %@", [super description]), that would have worked.
@jlehr It's almost only a coincidence that "static" happens to mean this as well (although I can see the relation). The primary question here is whether or not a method is called on an instance of a class or on the class itself. This is not related to a method being polymorphic (however, you're right in that Objective-C class methods are polymorphic, but that isn't what OP asked).
@jlehr No, that wasn't incorrect, since it is right that Objective-C class methods are polymorphic. All I was trying to say is this is not why they are different. But yeah, probably this is getting overdiscussed :P
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.