【发布时间】:2016-12-08 20:26:49
【问题描述】:
我有一个 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;
这是在.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,但没有发现任何解决此特定问题的方法。非常感谢您的帮助!
【问题讨论】:
-
显示
dictionaryOfUserIndexes在你的单例类中是如何声明的。请格式化您的代码。您发布的问题太多,无法发布格式错误的代码。 -
re: 格式化我的代码更好吗?如果不是,我不明白你指的是什么...请详细说明... SD
-
SingletonDictionary继承自什么? -
mapUserFields 中的第一行...现在我看了一下,也不确定这是否正确...这是我几年前写的,现在回头看,可以'不记得我为什么做这些事情......当时只是在学习单身......
标签: objective-c singleton ios9 xcode8