Thursday, September 26, 2013

Common Objective C coding errors

I am going to introduce the common coding errors made while programming in Objecive C language.

1. Declaring the const char * strings. Yes this the common coding error while programming, You almost never want to declare a const char * string, e.g. see the below line of code.

self.selectedLabel.text = [NSString stringWithFormat:@"Selected : %d", self.selectedCount];

new the method stringWithFormat is declared like this 

+ (instancetype)stringWithFormat:(NSString *)format,, ...

and it expects and NSString as a parameter.

If you do not specify @ symbol in front of the Format string it will be treated as a const char * string not NSString class.

2. Removing the method from the .m file and not removing the Action from the UI result in the error.

3. Write repeated @synthetize statement, rather all the properties can be declared in one statement with , separated

4. Trying to use == sign to compare the strings. However this will compare the references not the actual contents. Use isStringEquals and other equivalent methods.

5. Initializing a variable inside a case statement of Switch/Case. Which is not allowed it, compiler will raise an error. To avoid this declare a variable before the switch statement, or you can write a NSLog statement as the first statement in the case. :)

No comments:

Post a Comment