11

That's it. Why would anyone want (at least as a public API) a method such as that? Is there any practical use for it?

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
Matoe
  • 2,742
  • 6
  • 33
  • 52
  • 1
    The question was "Why would anyone want..." or "What is the purpose...", not "Why is it there?" – Matoe Dec 15 '12 at 20:21
  • 1
    This seems like a valid question. Why the downvotes? – Nikolai Ruhe Dec 15 '12 at 20:23
  • It's very open ended...there's no real straight answer. Like @H2CO3 asked, why would anyone _not_ want it? – John Dec 15 '12 at 20:35
  • There is no real reason. It's there, perhaps it could be used. But there is no reason to not use it. – Josiah Dec 15 '12 at 20:37
  • 1
    @H2CO3 and @John - Have you ever had a practical reason to use the `self` method? I'm curious too when this would be useful. Why would I ever need to call `[self self]` or `[someObject self]`? Before this question, I hadn't even noticed that method existed. – rmaddy Dec 15 '12 at 21:14
  • 1
    I use it in cycript purely through laziness. view = [array[1] view]; then I drop the index to index 2 and it IS a view, so doesn't have a view method. Rather then have to go back and delete lines I tend to rewrite it to [array[2] self], especially when I'll have to change again for the 3'rd index. – Kyle Howells Dec 15 '12 at 21:34

3 Answers3

12

The self method is useful for Key-Value Coding (KVC).

With KVC, you can treat an object somewhat like a dictionary. You can access a property of the object using a string containing the name of the property, like this: [view valueForKey:@"superview"]. You walk down a chain of properties using a string containing a key path, like this: [view valueForKeyPath:@"superview.superview.center"].

Since NSObject has a self method, you can use self as the key or key path: [view valueForKey:@"self"]. So if you're constructing your key paths programmatically, or reading them from a file, using "self" as a key may allow you to avoid writing a special case.

You can also use self in predicates, like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self beginswith \"foo\""];
NSArray *filteredArray = [arrayOfStrings filteredArrayWithPredicate:predicate];

I don't know whether NSPredicate actually uses the self method (perhaps via KVC) in this case. It's certainly possible.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    "SELF" is documented as a special constant in NSPredicates, like "nil", "NULL" and a bunch of others. It could be mapped to the actual "self" method, but to me the documentation implies it's special. – Wade Tregaskis Dec 15 '12 at 23:01
2

I'm not sure why "self" was added originally, but one thing it did come in handy for was protecting interior pointers to objects. Apple's official recommendation was to insert a [foo self] call after you're done with the interior pointer; the method call does nothing functionally but ensures the compiler keeps foo around until then.

Wade Tregaskis
  • 1,996
  • 11
  • 15
-1

I think it's to do with the ObjC runtime.

objc_msgSend(autoreleasePool, sel_registerName("drain"));
BOOL AppDel_didFinishLaunching(struct AppDel *self, SEL _cmd, void *application, void *options)

The first argument is self. I think it has something to do with that. In all honesty though as it would end up as:

id self(struct id *self, SEL _cmd) {
    return self;
}

....It made more sense before I started writing this response.

Kyle Howells
  • 3,008
  • 1
  • 25
  • 35