希望有人可以帮助我。我对iOS应用程序开发非常陌生,我实际上是想在当地学校举办一些职业日活动。
背景是我希望能够测试学生准确处理和输入数据的能力。
为了测试这个,我想要一个我将部署到iPad上的应用程序供他们在白天尝试。非常简单的东西(我想!)
到目前为止,我知道如何创建我的应用程序,将输入框放入并且所有链接起来并且已经计算出如何测试框中的输入是否正确对照以下代码中的硬编码值:
self.DealID = self.DealIDEntry.text;
NSString *Check1 = @"No";
NSString *DealIDString = self.DealID;
if ([DealIDString length] == 0) {
Check1 = @"No Deal ID Entered";
}
if([self.DealID isEqual: @"12345678"]) {
Check1 = @"Yes";
}
self.DealIDCheck.text = Check1;
我想要的是,有一个可能的DealID表,每个都有5-6位相关数据(客户,货币等),然后当学生输入交易ID以及希望是正确的客户时,货币等应用程序应通过对plist中的DealID进行“查找”并检查客户价值对于该DealID是否正确来进行检查。
我试过通过字典来做这件事:
NSBundle *bundle = [NSBundle mainBundle];
NSString *pListpath = [bundle pathForResource:@"TestTable" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:pListpath];
self.CustomerName = self.CustomerNameEntry.text;
NSString *Check2 = @"No";
NSDictionary *DealID = [dict valueForKey:self.DealID];
NSString *CustomerNameString = self.CustomerName;
if ([CustomerNameString length] == 0) {
Check2 = @"No Customer Name Entered";
}
if([self.CustomerName isEqual: [DealID objectForKey:@"CustomerName"]]) {
Check2 = @"Yes";
}
self.CustomerNameCheck.text = Check2;
可悲的是它不起作用,我怀疑要么我不知道如何正确加载plist(或测试它是否已经加载)或者我做错了!
编辑2 - 感谢Matt在下面的帮助,我现在有了这个代码来加载我的plist作为目录目录,并可以查询它为给定子目录的相关对象。再次感谢马特!
//This loads the array of data
NSBundle* bundle = [NSBundle mainBundle];
NSString* plistPath = [bundle pathForResource:@"TestTable" ofType:@"plist"];
//Make large Dictionary
NSDictionary *TradeTable = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
NSLog(@"The count: %i", [TradeTable count]);
//Create relevant sub directory
NSDictionary *query = [TradeTable valueForKey:@"12345678"];
NSLog(@"The count: %i", [query count]);
//Query that subdirectory for the object in question
NSString *name = [query objectForKey:@"CustomerName"];
NSLog(name);
对于任何有兴趣的人,我的plist现在看起来像这样:
<dict>
<key>12345678</key>
<dict>
<key>CustomerID</key>
<integer>98765</integer>
<key>CustomerName</key>
<string>BigBank</string>
<key>CustomerBank</key>
<string>LittleBank</string>
<key>AmountinCCY</key>
<integer>75000</integer>
<key>CCY</key>
<string>GBP</string>
<key>AmountinUSD</key>
<integer>115000</integer>
<key>Date</key>
<integer>1</integer>
<key>Points</key>
<integer>5</integer>
</dict>
<key>12345679</key>
<dict>
<key>CustomerID</key>
<integer>98754</integer>
<key>CustomerName</key>
<string>CarShop</string>
<key>CustomerBank</key>
<string>BigBank</string>
<key>AmountinCCY</key>
<integer>25123</integer>
<key>CCY</key>
<string>EUR</string>
<key>AmountinUSD</key>
<integer>27000</integer>
<key>Date</key>
<integer>0</integer>
<key>Points</key>
<integer>4</integer>
</dict>
</dict>
</plist>
很抱歉罗嗦,只是想确保我尽可能多地提供信息。
非常感谢任何人的帮助。 菲尔
答案 0 :(得分:0)
您显示的plist将加载为NSDictionary对象的NSArray。所以用NSArray arrayWithContentsOfFile:
加载它;然后你走NSArray直到找到你想要的NSDictionary。
您使用的是[[NSDictionary alloc] initWithContentsOfFile:pListpath]
,但结果(您的dict
)为零,因为不是字典,它是一个数组。 initWithContentsOfFile:
上的文档准确地预测了这一点:
返回值:初始化字典... 或nil if ...文件内容是字典的无效表示。
(我的斜体)
嗯,这是你的问题。该文件的内容是字典的无效表示,因为它们实际上是数组的有效表示。
现在,我并不是说一系列字典是存储数据的最佳方式。我认为字典词典会好得多(因为那时你可以通过一些主键立即获取正确的子字典,比如DealID)。但是,无论出于何种原因,这都不是您存储数据的方式。您将其存储为数组,现在必须将其作为数组检索。
编辑:我建议您使用交易ID的值作为子管理的关键。像这样:
Dictionary
key: 12345678
value: a whole nother dictionary with everything *else* about that answer
---
key: 12345679
value: a whole nother dictionary with everything *else* about that answer
---
etc.
所以你从你的大字典词典(d
)开始,现在d[someDealId]
是正确的字典(subd
),现在你可以获得subd[@"customerName"]
或任何字典键的其他值。在我看来,这正是你所描述的那种“查找”。