Why is this not working?


#1

For some reason when I run this program it fails because “string” is considered an undeclared identifier. Here is my code I’ve checked it and I can’t see how it differs from the book.

NNString+VowelCounting.h:

#import <Foundation/Foundation.h>

@interface NSString (VowelCounting)

  • (int)vowelCount;

@end

NSString+VowelCounting.m

#import “NSString+VowelCounting.h”

@implementation NSString (VowelCounting)

  • (int)vowelCount
    {
    NSCharacterSet *charSet =
    [NSCharacterSet characterSetWithCharactersInString:@“aeiouyAEIOUY”];

    NSUInteger count = [self length];
    int sum = 0;
    for (int i = 0; i < count; i++) {
    unichar c = [self characterAtIndex:i];
    if ([charSet characterIsMember:c]) {
    sum++;
    }
    }
    return sum;
    }

@end

main.m

#import <Foundation/Foundation.h>
#import “NSString+VowelCounting.h”

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    // insert code here...
    NSLog(@"Hello, World!");
    NSLog(@"%@ has %d vowels", string, [string vowelCount]);
    
}
return 0;

#2

I think you meant

[code]int main(int argc, const char * argv[])
{
@autoreleasepool {

// insert code here...
NSString *string = "Hello, World!";
NSLog(@"%@ has %d vowels", string, [string vowelCount]);

}
return 0;
}
[/code]


#3

Thank you for the quick reply! I have a talent for not seeing what is right in front of my face! The next time I have a problem hopefully it will be a real one. Thanks again.


#4

When I try to build this script I get two errors in the “NSObject+VowelCounting.m”-file .

On the line of: NSUInteger count = [self length]; it reads
No visible @interface for “NSObject” declares the selector “length”.

And on the line of unichar c = [self characterAtIndex:i]; it reads:
No visible @interface for “NSObject” declares the selector “characterAtIndex”.

What am I missing?


#5

What is the type of self? Can you post your code?


#6

Hi

Here’s my code:

[code]- (int)vowelCount
{
NSCharacterSet *charSet =
[NSCharacterSet characterSetWithCharactersInString:@“aeiouyAEIOUY”];

NSUInteger count = [self length];
int sum = 0;
for (int i = 0; i < count; i++) {
    unichar c = [self characterAtIndex:i];
    if ([charSet characterIsMember:c]) {
        sum++;
    }
}
return sum;

}
@end
[/code]


#7

This is only a fraction of the code, thus not very useful. Please post all your *.h and *.m files.


#8

I found my mistake and excuse the incomplete code inclusion.