Domanda

I am a beginner. And I was trying to transform arabic numbers to ordinal numbers by having the following class.

num is NSInteger, while, during the calculation, the warning pop out "local declaration of '' hides instance"

#import "ordinalNumberFormatter.h"

@implementation ordinalNumberFormatter

- (NSString*)ordinalNumberFormatter:(NSInteger)num
{
        NSString *ending;
        int ones = num % 10; //Warning came out
        int tens = floor(num / 10); //Warning came out
        tens = tens % 10;
        if(tens == 1){
            ending = @"th";
        }else {
            switch (ones) {
                case 1:
                    ending = @"st";
                    break;
                case 2:
                    ending = @"nd";
                    break;
                case 3:
                    ending = @"rd";
                    break;
                default:
                    ending = @"th";
                    break;
            }
        }
        return [NSString stringWithFormat:@"%d%@", (int)num, ending]; //Warning came out
    }
@end
È stato utile?

Soluzione

Do you have a class variable called "num" as well? The warning is just saying that you're using the local variable "num" defined in

- (NSString*)ordinalNumberFormatter:(NSInteger)num

And you might instead want to use the "num" defined as the class variable. Maybe change the above "num" to another name and use that name in the method. That'll clear it all up

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top