Objectivist-C

I’m Bavarious, (sporadically) writing about Objective-C and Apple frameworks.

Feb 17

Name spaces in Objective-C

One thing that might not be immediately clear for newcomers to Objective-C is the name spaces for the varying Objective-C constructs. For instance, is it possible for a class to have the same name as a protocol?

Let’s start by enumerating some of the elements that can be named:

  • C types
  • C variables
  • C functions
  • C structure members
  • Objective-C classes
  • Objective-C class methods
  • Objective-C instance methods
  • Objective-C instance variables
  • Objective-C declared properties
  • Objective-C protocols

C elements

Since Objective-C is a strict superset of C, the standard rules for C name spaces apply. Types, variables, and functions share the same name space if they are in the same syntactic context (e.g. global vs. block). For instance, it is not possible to have a global function with the same name as a globally defined type. However, it is possible to have a global function with the same name as a type declared inside that function since they are in different contexts.

The following code is valid C:

int main()
{
    typedef int main;
    {
        main main = 42;
        printf("%d\n", main);
    }
    return 0;
}

whereas the following code isn’t:

int main()
{
    typedef int main;
    main main = 42;
    printf("%d\n", main);
    return 0;
}

because the type main is in the same syntactic context as the local variable main.

Each C structure (or union) has its own name space.

Objective-C Elements

An Objective-C class is a C type, hence the same rules for C types apply. The following code isn’t valid:

typedef char *Name;

@interface Name : NSObject
@end

since both the type and the class are defined in the same syntactic context, whereas the following code is valid:

@interface Name : NSObject
@end

int main ()
{
    typedef char *Name;
    Name name = "objc";
    printf("%s\n", name);
    return 0;
}

Note that Name can refer to both the global Objective-C class and the C typedef char *, and the compiler uses whatever is closest to the syntactic context where the identifier is used.

Other Objective-C elements are not C types. Protocols and categories have their own name space and, for a given class, instance variables, declared properties, class methods, and instance methods have each their own name space. This means that it is possible to have a protocol, a category, a class method, an instance method, a declared property, and an instance variable with the same name. This also means that each of these elements can have the same name as an Objective-C class or C elements.

The following code — where a protocol, a class, an instance variable, a declared property, a class method, and an instance method share the same name — is valid:

@protocol Name
@end

@interface Name <Name> {
    Name *Name;
}
@property (nonatomic, assign) Name *Name;
- (Name *)Name;
+ (Name *)Name;
@end

Props to David Smith for pointing out that class methods and instance methods are in different name spaces, and to Daniel DeCovnick for pointing out that categories are also in a different name space.


  1. objectivistc posted this