Objective-C:接口 属性 上的 NSMutabledicionary 问题
Objective-C: issue with NSMutabledicionary on interface property
我已经实现了这个 AdressCard class。这是界面:
#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
现在我想创建一个 class "AdressBookDictionary" 使用 NSMutableDictionary 访问我的地址卡,这样我就可以插入、删除等等,它的值。我知道我需要键和值,但是在写字典 属性 时出现错误:它说 "too few type arguments",像这样:
#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
的新手
您只在字典中指定了一种类型:
NSMutableDictionary <AddressCard*> *cards;
字典有两种 关联类型 - 一种用于键,一种用于值。看起来你想使用 NSString
s 作为你的键,所以你需要像这样定义字典:
NSMutableDictionary <NSString*, AddressCard*> *cards;
此博客 post 有一个很好的简短概述:
http://useyourloaf.com/blog/using-objective-c-lightweight-generics/
我已经实现了这个 AdressCard class。这是界面:
#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
现在我想创建一个 class "AdressBookDictionary" 使用 NSMutableDictionary 访问我的地址卡,这样我就可以插入、删除等等,它的值。我知道我需要键和值,但是在写字典 属性 时出现错误:它说 "too few type arguments",像这样:
#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
的新手您只在字典中指定了一种类型:
NSMutableDictionary <AddressCard*> *cards;
字典有两种 关联类型 - 一种用于键,一种用于值。看起来你想使用 NSString
s 作为你的键,所以你需要像这样定义字典:
NSMutableDictionary <NSString*, AddressCard*> *cards;
此博客 post 有一个很好的简短概述: http://useyourloaf.com/blog/using-objective-c-lightweight-generics/