Search code examples
objective-cnsmutabledictionary

Objective-C: issue with NSMutabledicionary on interface property


I have this AdressCard class implemented. Here's the interface:

#import <Foundation/Foundation.h>

@interface AddressCard : NSObject

@property NSString *name;
@property NSString *email;
@property NSString *phone;

- (instancetype) initWithName:(NSString*)name
                        email:(NSString*)email
                        phone:(NSString*)phone;

+ (instancetype) cardWithName:(NSString*)name
                        email:(NSString*)email
                        phone:(NSString*)phone;


@end

Now I want to create a class "AdressBookDictionary" acessing my adressCard with a NSMutableDictionary, so I can insert, delete, etc, it's values. I know I need the keys and values but i get an error when writing it's the dict property: it says "too few type arguments", like this:

#import <Foundation/Foundation.h>
#import "AddressCard.h"

@interface AdressBookDictionary : NSObject

@property NSString *name;
@property NSMutableDictionary <AddressCard*> *cards;

- (instancetype)initWithName:(NSString*)name;
+ (instancetype)bookWithName:(NSString*)name;

- (void)addCard:(AddressCard*)card;
- (void)removeCard:(AddressCard*)card;
- (AddressCard*)lookup:(NSString*)name;

- (NSUInteger)entries;
- (void)list;



@end

Can someone help me? I'm quite new at Objective-c


Solution

  • You're only specifying a single type with your dictionary:

    NSMutableDictionary <AddressCard*> *cards;
    

    A dictionary has two associated types - one for keys, and one for values. It looks like you want to use NSStrings as your keys, so you'd need to define the dictionary like this:

    NSMutableDictionary <NSString*, AddressCard*> *cards;
    

    This blog post has a nice, short overview: http://useyourloaf.com/blog/using-objective-c-lightweight-generics/