Objective-C:接口属性上的NSMutabledicionary问题

时间:2017-01-07 15:56:46

标签: objective-c nsmutabledictionary

我已经实现了这个AdressCard类。这是界面:

#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

现在我想创建一个“AdressBookDictionary”类,使用NSMutableDictionary访问我的adressCard,所以我可以插入,删除等它的值。我知道我需要键和值但是在编写它时会出现错误dict属性:它表示“类型参数太少”,如下所示:

#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

有人能帮助我吗?我在Objective-c

上很新

1 个答案:

答案 0 :(得分:1)

您只使用字典指定单一类型:

NSMutableDictionary <AddressCard*> *cards;

字典有两个关联类型 - 一个用于键,一个用于值。看起来您想使用NSString作为键,因此您需要像这样定义字典:

NSMutableDictionary <NSString*, AddressCard*> *cards;

这篇博文有一个很好的简短概述: http://useyourloaf.com/blog/using-objective-c-lightweight-generics/