PDA

View Full Version : Finding invalid method names?




robvas
Mar 9, 2012, 10:20 PM
Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

Is there a compiler option that will turn on stricter checking of method names? just spent 25 minutes tracking a bug down and it ended up being a simple case of using numberofObjects instead of numberOfObjects with an NSArray.



xStep
Mar 10, 2012, 01:50 AM
According to a documentation search, the numberOfObjects method is associated with the NSFetchedResultsSectionInfo Protocol. NSArray uses the count method.

In both Xcode 3 and 4, a warning is issued on the line where I misspelled the method name. Perhaps I've turn a warning on, or you turned a warning off.

gnasher729
Mar 10, 2012, 02:30 AM
Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

Is there a compiler option that will turn on stricter checking of method names? just spent 25 minutes tracking a bug down and it ended up being a simple case of using numberofObjects instead of numberOfObjects with an NSArray.

Go to project settings, look at the setting "compiler warnings", turn on each warning in turn and keep everything that looks useful. Most important "treat warnings as errors".

Also there is no checking when sending a message to an object with declared type "id", so sometimes you have to assign to the right type first.

Oligarch
Mar 10, 2012, 12:05 PM
Also, use code completion. It is tremendously faster than manually spelling out long method names and pretty much eliminates typos like this one. Works for anything: class names, method names, named constants, variables, functions. The built-in heuristics are quite good. You don't even have to respect capitalization. Just type the first few letters in lower case and hit escape, then return or the next character, like a space, semicolon, etc. Nine times out of ten, it should give you what you want, no need to pause and select, guaranteed typo-free. If it does get it wrong, Cmd-Z and add another couple of letters, or select from the list.