单身汉词典是空的,我不明白为什么

问题描述:

我有一个iOS应用程序匹配传入的文本字段标准字段用于导入记录。我的问题是,使用这些字段的NSMutableDictionary是空的!这里是保存的映射的​​代码:单身汉词典是空的,我不明白为什么

-(void)mapUserFields: (id) sender { // move contents of each textField when user has finished entering it 

    SingletonDictionary *sd = [SingletonDictionary sharedDictionary]; 

    UITextField *tf = (UITextField *)sender; // textfield contains the pointer to user's half of the equation 
    int tagValue = (int)tf.tag; // get the tag value 

    [sd.dictionaryOfUserIndexes setObject:tf.text forKey:[NSString stringWithFormat:@"%d", tagValue]]; // value found in textField id'd by tag 

    NSLog(@"\nfield.text: %@ tagValue: %d nsd.count: %d\n",tf.text, tagValue, sd.dictionaryOfUserIndexes.count); 

} 

这是NSLog的结果:

field.text:1 tagValue:38 nsd.count:0

这是.h文件中单例的定义:

@property (nonatomic, retain) NSMutableDictionary *dictionaryOfUserIndexes; 

这是初始化唱歌的代码leton在.m文件:

//-- SingletonDictionaryOfUserIDs -- 
+ (id) sharedDictionary { 

    static dispatch_once_t dispatchOncePredicate = 0; 
    __strong static id _sharedObject = nil; 
    dispatch_once(&dispatchOncePredicate, ^{ 
     _sharedObject = [[self alloc] init]; 
    }); 

    return _sharedObject; 
} 

-(id) init { 
    self = [super init]; 
    if (self) { 
     dictionaryOfUserIndexes = [NSMutableDictionary new]; 
    } 
    return self; 
} 

@end 

我相信我的问题是,因为sd.dictionaryOfUserIndexes尚未被初始化,但我不知道这是不是真的,如果是的话,如何对其进行初始化(我试过几个不同的变体,所有这些都造成了构建错误)。我看着SO和Google,但没有发现解决这个问题的东西。帮助将不胜感激!

+0

显示如何在单例类中声明'dictionaryOfUserIndexes'。并请格式化您的代码。您发布的问题太多,无法发布格式不正确的代码。 – rmaddy

+0

更好的是:格式化我的代码?如果不是,我不明白你指的是什么......请详细说明...... SD – SpokaneDude

+0

'SingletonDictionary'继承自什么? –

有一些事情是我们可以改善这个代码,但唯一它是在init方法的参考dictionaryOfUserIndexes。该代码张贴不会编译,除非:(a)你有这样一行:

@synthesize dictionaryOfUserIndexes = dictionaryOfUserIndexes; 

使后盾变量而没有默认_前缀命名,或(b)你指的是与伊娃缺省前缀,如:

_dictionaryOfUserIndexes = [NSMutableDictionary new]; 

的另一种方式 - 在除了一个init方法中最每个上下文优选 - 是使用合成的设定器,如:

self.dictionaryOfUserIndexes = [NSMutableDictionary new]; 

但是机智h单独更改(所以它会编译)您的代码运行正常,向字典添加一个值并记录递增计数。

+0

非常感谢你...我真的很感激... – SpokaneDude