从Singleton获取NSMutableDictionary?

时间:2013-11-27 15:23:51

标签: objective-c nsmutablearray singleton

我创建了一个单例类,以便在程序中共享一个对象。这是代码:

SelectedRow.h

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

@interface SelectedRow : NSObject {
    TableEntry *rowValue;
}

@property (nonatomic, retain) TableEntry *rowValue;

+ (id)sharedManager;
- (void)setVariable:(TableEntry*)value;

@end

和SelectedRow.m

#import "SelectedRow.h"
#import "TableEntry.h"

@implementation SelectedRow

@synthesize rowValue;

+ (id)sharedManager {
    static SelectedRow *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

- (id)init {
    if (self = [super init]) {
        rowValue = [[TableEntry alloc] init];
    }
    return self;
}

- (void)setVariable:(TableEntry*)value {
    rowValue = value;
}

@end

而TableEntry.h

#import <Foundation/Foundation.h>

@interface TableEntry : NSObject {
@private
    NSString *videoId;
    NSString *videoCategory;
    NSString *videoTitle;
    NSString *videoDescription;
    NSDate *videoDate;
    NSMutableArray *videoRelatedVideos;
    NSDictionary *videoAdditionalInformation;
    NSString *videoAccessControl;
    NSArray *videoFields;
    NSMutableDictionary *days;
    NSMutableDictionary *views;
    NSMutableDictionary *watchtime;
    NSMutableDictionary *subscribers;
    NSMutableDictionary *shares;
}

@property (copy) NSString *videoId;
@property (copy) NSString *videoCategory;
@property (copy) NSString *videoTitle;
@property (copy) NSString *videoDescription;
@property (copy) NSMutableArray *videoRelatedVideos;
@property (copy) NSDictionary *videoAdditionalInformation;
@property (copy) NSArray *videoFields;
@property (copy) NSString *videoAccessControl;
@property (copy) NSDate *videoDate;
@property (copy) NSMutableDictionary *days;
@property (copy) NSMutableDictionary *views;
@property (copy) NSMutableDictionary *subscribers;
@property (copy) NSMutableDictionary *shares;
@property (copy) NSMutableDictionary *watchtime;

- (id)setId:(NSString*)Id setCategory:(NSString*)Category setDate:(NSDate*)date setTitle:(NSString*)title setDescription:(NSString*)description setRelatedVideos:(NSMutableArray*)relatedVideos setAdditionalInformation:(NSDictionary*)additionalInformation setAccessControl:(NSString*)accessControl setFields:(NSArray*)fields setDays:(NSMutableDictionary*)days setViews:(NSMutableDictionary*)views setSubscribers:(NSMutableDictionary*)subscribers setShares:(NSMutableDictionary*)shares setWatchtime:(NSMutableDictionary*)watchtime;

- (NSString*)extractId;
- (NSString*)extractCategory;
- (NSString*)extractTitle;
- (NSString*)extractDescription;
- (NSMutableArray*)extractRelatedVideos;
- (NSDictionary*)extractAdditionalInformationVideos;
- (NSDictionary*)extractAccessControlVideos;
- (NSArray*)extractFields;
- (NSMutableDictionary*)extractDays;
- (NSMutableDictionary*)extractViews;
- (NSMutableDictionary*)extractSubscribers;
- (NSMutableDictionary*)extractShares;
- (NSMutableDictionary*)extractWatchtime;

@end

和TableEntry.m

- (id)init {

    self = [super init];
    if (self) {
        videoId = @"9bZkp7q19f0";
        videoCategory = @"Music";
        videoTitle = @"Demo Title";
        videoDescription = @"Demo description";
        videoDate = [NSDate date];
        videoAdditionalInformation = [NSDictionary alloc];
        videoRelatedVideos = [NSMutableArray alloc];
        videoAccessControl = @"demo accesControl";
        videoFields = [NSArray alloc];
        days = [NSMutableDictionary alloc];
        views = [NSMutableDictionary alloc];
        shares = [NSMutableDictionary alloc];
        subscribers = [NSMutableDictionary alloc];
        watchtime = [NSMutableDictionary alloc];
    }
    return self;
}

- (id)setId:(NSString*)Id setCategory:(NSString*)Category setDate:(NSDate*)date setTitle:(NSString*)title setDescription:(NSString*)description setRelatedVideos:(NSMutableArray*)relatedVideos setAdditionalInformation:(NSDictionary*)additionalInformation setAccessControl:(NSString*)accessControl setFields:(NSArray*)fields  setDays:(NSMutableDictionary*)Days setViews:(NSMutableDictionary*)Views setSubscribers:(NSMutableDictionary*)Subscribers setShares:(NSMutableDictionary*)Shares setWatchtime:(NSMutableDictionary*)Watchtime {
    videoId = Id;
    videoCategory = Category;
    videoDate = date;
    videoTitle = title;
    videoDescription = description;
    videoRelatedVideos = relatedVideos;
    videoAccessControl = accessControl;
    videoAdditionalInformation = additionalInformation;
    videoFields = fields;
    days = Days;
    views = Views;
    subscribers = Subscribers;
    watchtime = Watchtime;
    shares = Shares;

    return self;
}

- (NSString*)extractId {
    return self.videoId;
}

- (NSString*)extractCategory{
    return self.videoCategory;
}

- (NSString*)extractTitle{
    return self.videoTitle;
}

- (NSString*)extractDescription{
    return self.videoDescription;
}

- (NSMutableArray*)extractRelatedVideos{
    return self.videoRelatedVideos;
}

- (NSString*)extractAccessControlVideos{
    return self.videoAccessControl;
}

- (NSDictionary*)extractAdditionalInformationVideos{
    return self.videoAdditionalInformation;
}

- (NSArray*)extractFields{
    return self.videoFields;
}

- (NSMutableDictionary*)extractDays{
    return self.days;
}

- (NSMutableDictionary*)extractSubscribers{
    return self.subscribers;
}

- (NSMutableDictionary*)extractWatchtime{
    return self.watchtime;
}

- (NSMutableDictionary*)extractShares{
    return self.shares;
}

- (NSMutableDictionary*)extractViews{
    return self.views;
}

@end

我可以使用以下方法从单例中提取任何值:

SelectedRow *selectedRow = [SelectedRow sharedManager];
NSString *videoID = [selectedRow.rowValue extractId];

任何NSMutableDictionary都会出现问题。如果我尝试:

SelectedRow *selectedRow = [SelectedRow sharedManager];
NSMutableDictionary *days = [selectedRow.rowValue extractDays];

或与任何其他NSMutableDictionary我收到此错误:

[NSMutableDictionary count]: method sent to an uninitialized mutable dictionary object

我做错了什么?感谢

1 个答案:

答案 0 :(得分:5)

[NSMutableDictionary alloc]调用会为NSMutableDictionary分配空间,但不会对其进行初始化。

将其替换为[NSMutableDictionary dictionary]以解决问题。您的NSArrayNSMutableArray个对象也是如此(将其替换为[NSMutable array][NSMutableArray array])。

videoAdditionalInformation类型的NSDictionary应该初始化为nil,因为NSDictionary对象是不可变的。如果您打算稍后将其设置为某个字典,那么您可以将它nil保留在初始化状态。

此外,您应该重新考虑使用copy:它对NSString个对象有意义,但对NSMutableDictionary个对象几乎没有意义。