13

Related: “sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers” warning

Sometimes, I think it's useful though. I may need to pass an a table and want to make sure that the content of the table does not change.

However, in most Objective-C program samples, I never see const. My question is why?

21

Due to the way that Objective-C objects work, const gives up being an enforcement and starts being a notation for the programmer. Consider this program:

int f(const int x) {
    return ++x;
}

int main(int argc, char *argv[]) {
    @autoreleasepool {
        int x = 3;
        NSLog(@"%d", f(x));
    }
    return 0;
}

That actually won't compile here (I'm using clang): the compiler can detect the attempt to modify the primitive C type and emits an error. But now compare it with this program:

NSMutableString *f2(const NSMutableString * const x) {
    [x appendString: @" world!"];
    return x;
}

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSMutableString *x = [@"Hello" mutableCopy];
        NSLog(@"%@", f2(x));
    }
    return 0;
}

Even though the function is passed a constant pointer to a constant object, it's still possible to mutate the object.

In object-oriented programming, the best way to enforce the constant nature of an object is to make that object immutable - i.e. don't provide any methods that can change its state. Imagine that the function above took an NSString argument instead of NSMutableString, and that I'd passed the literal @"Hello" instead of a mutable copy. There is now, reasonably speaking, no chance of mutating the passed-in object[*]. Objective-C doesn't have any way of enforcing that though, unlike const or final object references in other OO languages.

For comparison, const works entirely differently in C++. If I get a const reference to a C++ object, I'm only allowed to call const member functions on that object. These functions preserve the const-ness of the object, either by not making any changes or by only modifying member variables that have explicitly been marked mutable by the class designer. So imagine that I had some type MutableString in C++ that's equivalent to NSMutableString in Objective-C. The equivalent of my example above would look something like:

MutableString& f3(const MutableString& x) {
  x.appendString(" world!");
  return x;
}

This definitely won't compile: in addition to appendString() not being a const operation, the function removes the const qualifier from the type reference which requires a const_cast.

[*]I expect there is some contorted way of doing it, but now we're into the realms of one programmer trying to sabotage another by doing "clever" things.

| improve this answer | |
  • Thanks. So there is no way to prevent modification through const modifier. – user4951 Jun 4 '12 at 10:26
  • Right. The compiler still treats const decoration as meaningful so you get contortions like aliases or casts to suppress warnings, but there's no change to the program's behaviour as a result of these contortions. – user4051 Jun 4 '12 at 10:30
  • 5
    I believe const could still be useful in Objective C to state that the pointer to an object is never assigned to point to a different object. I.e. with NSString * const x you know that x will always be the same string and not a different string. – tboyce12 Jan 26 '16 at 18:38
3

As far as Objective C goes - I don't program in it, so can't answer the question. The question I can answer though is "Should people use const a lot when programming in Objective C?" - that one is a definite "Yes".

Constness is essentially a software security feature that is used to make programs more reliable and maintainable. Many programmers grow up without really appreciating the value of consts, until one day something bad happens and they have an "Ah ha" moment - "Oh, that's what const is used for". It's entirely possible to write a program with no constants, is virtually impossible to make such a program reliable.

Many samples of code are not really programs, just snippets that are designed to show a specific feature. They don't use const as it distracts from the point the sample is trying to make. The other type of code you see is code written by inexperienced programmers who base what they do on the examples they have learned from, or those that know about constants, and are too lazy to use them.

| improve this answer | |
0

I haven't used objective-c for a very long time.

However, I wonder why no body mention this aspect.

Objective-c already have something else related to constant. Mutability

For example, Nstring * will point to a constant string. If you want a string you can modify, you use NMutableString or something like that. That NMutableString will provide additional methods that allow people to modify the string.

Now, what about if you want the pointer itself to be a constant. I never have to worry about that while coding a very large project.

So I think that's why.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.