tags:

views:

92

answers:

5

I have a class that has a method:

-(NSInteger) getCityCountForState:(NSString *)state CityArray:(NSMutableArray *)cityArray {
NSInteger count = 0;
City *city = [[City alloc] init];

for(city in cityArray)
{
 if (city.state == state)
 {
  count++;
 }
}

return count;
}

That method is called from elsewhere in the same class:

count = [getCityCountForState:state CityArray:appDelegate.cities];

This gives the error "'getCityCountForState' undeclared (first use in this function)"

I have also tried:

count = [self.getCityCountForState:state CityArray:appDelegate.cities];

This gives the error "request for member 'getCityCountForState' in something not a structure or union"

I'm obviously missing something obvious...but I can't figure out what it is.

+12  A: 
count = [self getCityCountForState:state CityArray:appDelegate.cities];

Usual Objective-C naming conventions would argue for renaming that method a little, but that should get the call working, at least.

Edit: Here's a good place to start reading: Coding Guidelines for Cocoa. Personally, I'd argue for calling it:

- (NSInteger)countCities:(NSArray*)cities forState:(NSString*)state;
Sixten Otto
Specifically, each part of a method name should start with a lowercase letter (unless it's an initialism like URL), so that selector should be `getCityCountForState:cityArray:`.
Chuck
jdelStrother
Thank you very much. I renamed my method to countCities like you suggested. I like that name better anyway.:)
Pselus
+4  A: 

You should also remove the line

City *city = [[City alloc] init];

And put City * in the for statement

for (City *city in cityArray)

This will make it a little short and remove a memory leak

epatel
I am still learning about pointers and memory management. Thanks for that advice. I took it.
Pselus
A: 

Is -getCityCountForState:CityArray: declared in your header, too? The definition must preceed the usages if not.

swegi
it was declared in the header, yes.
Pselus
+4  A: 

Sixten answered correctly, but i thought i'd point out that your code has a memory leak; you don't need the [[City alloc] init] since you are reading them out of the array and not creating new ones in your logic.

If you're new to obj-c and cocoa i recommend reading the Memory Management Guide.

Kevlar
+3  A: 

Objective-C method call syntax is:

   [receiver message]

When you wrote:

[getCityCountForState:state CityArray:appDelegate.cities]

You attempted to send a CityArray:appDelegate.cities message to getCityCountForState:state.

You should review Objective-C syntax. Read Apple's introduction to the language.

NSResponder