Reputation: 176
I should preface this with saying I come from a Java/Android background which has colored my understanding of the word "member". That said, I'm starting to learn Objective-C and I ran across the isMemberOfClass method and the name confuses me.
I understand that isMemberOfClass returns a Boolean value that indicates whether the receiver is an instance of a given class. However, I don't understand why it is called isMEMBER when it checks if it is an INSTANCE.
Is there something about the language protocol that I don't know that would make sense to name it this? Does member mean something different in Objective-C than it does in Java?
The way I understand the definition of member, it is something a class HAS (method or data), rather than something a class IS (a type).
Can anyone clear this seemingly odd naming convention up for me? Thanks for helping a newbie!
Upvotes: 4
Views: 199
Reputation: 18253
I believe the term "member" used to denote either an instance variable or a method (sorry, member function) goes back to C++. I personally have never heard it used with Java, but my hearing may not be the best.
Objective-C is pretty much everything C++ is not - and vice versa. Objective-C is historically based on smalltalk and inherits most of that environment's way of doing things. Alan Kay, one of the "inventors" of Smalltalk is quoted as saying "I invented Object-Oriented Programming, and C++ is not what I had in mind", just to illustrate the feelings between the two camps.
So to answer your question: Yes, "member" has a different meaning in Objective-C and C++, and don't be too surprised if you find other words with different meanings as you get deeper into Objective-C.
Upvotes: 2
Reputation: 299345
The key note here is that Cocoa (and SmallTalk before it), does not use the word "member" to mean "instance variable" or "function of" or any of the other ways that the word "member" is used in Java or C++. There's a useful paper on SmallTalk from Harry H. Porter III that gives some context:
As mentioned earlier, each object contains zero or more fields. The term "field" is preferable, but other languages use the term "data member" for the same concept. Smalltalk uses the term "instance variable" to mean a field, so we say, "An object contains several instance variables" to mean the same thing as "An object contains several fields" or "An object contains several data members." We will use the terms "field" and "instance variable" interchangeably.
...
In Java or C++, the programmer may add "static data members" and "static member functions" to any class. Smalltalk has a similar ability, although static data members are called "class variables" and static member functions are called "class methods". (Recall that Smalltalk calls normal fields "instance variables" and it calls normal member functions "instance methods".)
In Cocoa, the term "member" is typically used in the context of a collection (see [NSSet member:]
). The question being asked by isMemberOfClass:
is "is this object a member of the set of all instances of this specific class."
That's not to say isInstanceOfClass:
would have been an un-Cocoa-like name. It's just that "member" doesn't have the same meaning here as in some other languages.
Upvotes: 6
Reputation: 2529
The concept of a member as a component of a class (method or data) does not exist in the iOS framework. The framework is also built around verbose and often lengthly method or variable names to promote "added readability" (in quotes because it isn't always necessarily the result).
It easily could be named isInstanceOfClass
, but that may have caused some confusion with subclasses. isMemberOfClass
just happens to be a simple method name that doesn't collide with any principles of the iOS framework and is quite self explanatory. I don't think the logic extends beyond that.
Upvotes: 2