-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

eugui
  • 461
  • 1
  • 5
  • 13
  • 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? –  Dec 17 '13 at 19:11
  • so how can I access this variable "linhaPod" in class "_tracks"? Is possible or no? – eugui Dec 18 '13 at 10:23
  • @userXXX No, you can't. –  Dec 18 '13 at 10:23

2 Answers2

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).

Chuck
  • 234,037
  • 30
  • 302
  • 389
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.

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76
  • Right, except an Objective-C class method is not the same thing as a static method in other languages. C functions are the Objective-C equivalent of static methods. – jlehr Dec 17 '13 at 19:30
  • 1
    @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. – TotoroTotoro Dec 17 '13 at 19:33
  • 1
    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. – jlehr Dec 17 '13 at 19:43
  • @jlehr: I don't think `super` is visible in ObjC static methods. `self` is visible, but that is a Class object. So it's quite different than `self` in an instance method. – TotoroTotoro Dec 17 '13 at 19:51
  • @BlackRider: The `super` keyword is equivalent to `self`, but starts the method lookup in the superclass rather than the current class. It works in any method, including class methods. (Why wouldn't it? There isn't really a difference between class and instance methods except that one is on the class and the other is on an instance of the class.) – Chuck Dec 17 '13 at 20:13
  • I get an "undeclared identifier" error when I try to reference `super` in a class method. – TotoroTotoro Dec 17 '13 at 20:29
  • @BlackRider: Are you using it as the receiver of a message? Because that's the only context in which the keyword is valid. – Chuck Dec 17 '13 at 20:32
  • @Chuck: I tried to do `NSLog(@"super: %@", super);`. I think it's the same as calling `[super description]`. – TotoroTotoro Dec 17 '13 at 20:34
  • 1
    @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. – Chuck Dec 17 '13 at 20:50
  • @Chuck: amazing. `[super description]` does indeed work. You learn something every day! – TotoroTotoro Dec 17 '13 at 21:28
  • @jlehr That's not what "static" means in other OO languages (such as C++ and Java). You are confusing the two meanings of the `static` keyword. –  Dec 18 '13 at 10:34
  • @H2CO3 Where am I going wrong? Here's Wikipedia on static methods: "In statically typed languages such as Java, static methods are called "static" because they are resolved statically (i.e. at compile time) based on the class they are called on and not dynamically as in the case with instance methods which are resolved polymorphically based on the runtime type of the object. Therefore, static methods cannot be overridden." **source: http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods** – jlehr Dec 18 '13 at 16:05
  • 1
    @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). –  Dec 18 '13 at 17:21
  • @H2CO3 Heh, "almost a coincidence"…nice turn of phrase. ;-) Again, my original comment was simply that an Objective-C class method isn't the same thing as a static method in other languages. I'm not sure if you're suggesting that was incorrect, or that you think my follow-up explanation was somehow inadequate, but anyway, it was a tangential point. – jlehr Dec 18 '13 at 19:32
  • 1
    @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 –  Dec 18 '13 at 19:40