In Java, I can declare local "variables" as final, e.g.
void myMethod() {
final String foo = "Hello World!";
foo = "Bye-bye..."; // compile error!!
return;
}
When I try to change its value, a get an error from the compiler. I want to declare some of my local "variables" final to avoid changing their value by accident.
Is this possible in Objective-C?
Usability of using final local variables: Most importantly, We can use local variable as final in an anonymous inner class, we have to declare the local variable of anonymous inner class as final. This is to do with the individual accessor methods that get generated to implement the anonymous inner class.
Variable Declaration in Objective-C You will use extern keyword to declare a variable at any place. Though you can declare a variable multiple times in your Objective-C program but it can be defined only once in a file, a function or a block of code.
Like in C/C++, you can use the const
keyword.
For more info, this article seems right up your alley: Java Developer’s Guide to String Constants in Objective-C
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With