Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Singleton字典是空的,我不知道';我不明白为什么_Objective C_Singleton_Ios9_Xcode8 - Fatal编程技术网

Objective c Singleton字典是空的,我不知道';我不明白为什么

Objective c Singleton字典是空的,我不知道';我不明白为什么,objective-c,singleton,ios9,xcode8,Objective C,Singleton,Ios9,Xcode8,我有一个iOS应用程序,它将传入的文本字段与用于导入记录的标准字段相匹配。我的问题是使用这些字段的NSMutableDictionary是空的!以下是保存映射的代码: -(void)mapUserFields: (id) sender { // move contents of each textField when user has finished entering it SingletonDictionary *sd = [SingletonDictionary shar

我有一个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标记值:38 nsd。计数:0

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

@property (nonatomic, retain) NSMutableDictionary *dictionaryOfUserIndexes;
这是在.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.DictionaryFuserIndex没有初始化,但我不确定这是否正确,如果是,如何初始化它(我尝试了几个不同的变体,所有这些都产生了构建错误)。我查看了SO和Google,但没有找到解决这个问题的方法。非常感谢您的帮助

我们可以在这段代码中改进一些地方,但唯一的错误是在
init
方法中引用了
DictionaryFuserIndex
。发布的代码不会编译,除非:(a)您有如下行:

@synthesize dictionaryOfUserIndexes = dictionaryOfUserIndexes;
因此,命名支持变量时不带默认前缀,或者(b)使用默认前缀引用ivar,如中所示:

_dictionaryOfUserIndexes = [NSMutableDictionary new];
另一种方法——最好是在除init方法之外的大多数上下文中使用——是使用合成setter,如:

self.dictionaryOfUserIndexes = [NSMutableDictionary new];

但是,仅通过这一更改(因此它将编译),您的代码就可以正常运行,向字典添加一个值,并记录递增的计数。

显示如何在singleton类中声明
DictionaryFuserIndex
。请格式化你的代码。您发布的问题太多,无法发布格式不正确的代码。重新设置代码格式是否更好?如果不是,我不明白你指的是什么…请详细说明。。。SDSingletonDictionary继承自什么?mapUserFields中的第一行。。。现在我看了一下,也不确定这是否正确。。。几年前我写过这篇文章,现在回想起来,我不记得为什么我会做这些事情。。。那时我只是在学习单身汉。。。