0

I'm trying to learn objective-c and have encountered a warning in this example:

#import <Foundation/Foundation.h>

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

{
    @autoreleasepool {

        NSMutableDictionary *booklisting = [NSMutableDictionary dictionary];

        int count; // < Am getting 'unused variable' warning here 

        [booklisting setObject:@"Wind in the Willows" forKey:@"100-432112"];
        [booklisting setObject:@"Tale of Two Cities" forKey:@"200-532874"];
        [booklisting setObject:@"Sense and Sensibility" forKey:@"200-546549"];

        [booklisting setObject:@"Shutter Island" forKey:@"104-109834"];


       NSLog(@"Number of books in dictionary = %lu", [booklisting count]);

would anyone know why?.. Would appreciate help..thanks

monkeyboy
  • 1,961
  • 2
  • 13
  • 7

5 Answers5

2

you are not using any where in your code.that's why warning comes like this.

int count;// count is an variable  and
[booklisting count]//here count is a property of NSArray reference class 

remove int count; and check it.

  • + 1, but `[booklisting count]` is in this case a method of `NSDictionary` ;) – HAS Jun 10 '13 at 09:59
  • yes Exactly for both aray and dictinary classes having the property count. –  Jun 10 '13 at 10:00
  • Yeah, but monkeyboy is using an `NSMutableDictionary` and not an `NSArray` and so your comment `//here count is a property of NSArray reference class` is wrong in this case ;) – HAS Jun 10 '13 at 10:03
  • NSMutableArray is subclass to NSArray na. –  Jun 10 '13 at 10:04
  • Of course it is, but we are not talking about `NSArray` at all, but **`NSDictionary`** (and **`NSMutableDictionary`**). – HAS Jun 10 '13 at 10:07
  • 1
    i agree with you regarding this question. –  Jun 10 '13 at 10:09
1

you arent using count... [booklisting count] is accessing a method of booklisting which is a NSMutableDictionary which has a method called count which returns how many entries are in the dictionary.

its coincidence that they have the same name.

Fonix
  • 11,447
  • 3
  • 45
  • 74
0

put this line

count =[booklisting count];

before NSLog

or remove this int count; line

count is getter method for array.

Ishu
  • 12,797
  • 5
  • 35
  • 51
  • Thanks.. I commented out the int count line and it worked. I'm a bit confused though. The code that I used is exactly the same as instructed from an ebook called "Objective-C 2.0 Essentials" by Neil Smyth. I got it from techtopia.com – how could he be wrong? Or have I done something wrong? – monkeyboy Jun 10 '13 at 11:42
0

Just remove int count; since you have not used variable count.

Ayush
  • 3,989
  • 1
  • 26
  • 34
0
int count; // < Am getting 'unused variable' warning here 

You declare a variable count.But it is never used.Hence the error is shown

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101