将嵌套 plist 加载到选择器时遇到问题
我对 Objective C 还很陌生,并且在将 plist 中的值读取到一组 NSArray 中时遇到问题。我尝试引用这个问题但没有运气。
我有 3 个数组正在尝试加载到我的选择器中。我想要加载的 3 个数组是数组转换、typesFrom 和 typesTo。 typesFrom 和 typesTo 包含相同的值,但根据转换类型而变化。
目前,选择器将加载 3 列,但没有行。我觉得可能有一个简单的解决方案,但我已经尝试了几个小时,但似乎找不到它。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Grab information from the plist
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle
pathForResource:@"conversions"
ofType:@"plist"
];
NSDictionary *dictionary = [[NSDictionary alloc]
initWithContentsOfFile:plistPath
];
// Store conversions
for(NSDictionary *dict in [dictionary valueForKey:@"Conversions"]) {
[self.conversions arrayByAddingObject:[dict valueForKey:@"name"]];
[self.categories arrayByAddingObject:[dict valueForKey:@"conversions"]];
}
[dictionary release];
[self loadIntoPicker:0];
}
- (void)loadIntoPicker:(NSInteger)index {
// Set the current category
NSArray *category = [self.categories objectAtIndex:index];
for (NSDictionary *conversion in category) {
[self.typesFrom arrayByAddingObject:[conversion valueForKey:@"type"]];
[self.typesTo arrayByAddingObject:[conversion valueForKey:@"type"]];
[self.descriptions arrayByAddingObject:[conversion valueForKey:@"description"]];
[self.multipliers arrayByAddingObject:[conversion valueForKey:@"multiplier"]];
}
[picker selectRow:0 inComponent:1 animated:YES];
[picker selectRow:0 inComponent:2 animated:YES];
[picker reloadComponent:1];
[picker reloadComponent:2];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if ( component == kCategoryComponent )
return [self.conversions count];
else
return [self.typesFrom count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if ( component == kCategoryComponent )
return [self.conversions objectAtIndex:row];
else if ( component == kConvertFromComponent )
return [self.typesFrom objectAtIndex:row];
else
return [self.typesTo objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if ( component == kCategoryComponent ) {
if ( row >= 0 )
[self loadIntoPicker:row];
}
}
我正在使用的plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Conversions</key>
<array>
<dict>
<key>name</key>
<string>Speed</string>
<key>conversions</key>
<array>
<dict>
<key>type</key>
<string>mph</string>
<key>description</key>
<string>mile/hour</string>
<key>multiplier</key>
<integer>1</integer>
</dict>
<dict>
<key>type</key>
<string>ft/s</string>
<key>description</key>
<string>foot/second</string>
<key>multiplier</key>
<real>1.466667</real>
</dict>
<dict>
<key>type</key>
<string>km/h</string>
<key>description</key>
<string>kilometer/hour</string>
<key>multiplier</key>
<real>1.609344</real>
</dict>
<dict>
<key>type</key>
<string>m/s</string>
<key>description</key>
<string>meter/second</string>
<key>multiplier</key>
<real>0.44704</real>
</dict>
</array>
</dict>
<dict>
<key>name</key>
<string>Distance</string>
<key>conversions</key>
<array>
<dict>
<key>type</key>
<string>ft</string>
<key>description</key>
<string>feet</string>
<key>multiplier</key>
<integer>1</integer>
</dict>
<dict>
<key>type</key>
<string>yd</string>
<key>description</key>
<string>yard</string>
<key>multiplier</key>
<real>0.3333333</real>
</dict>
<dict>
<key>type</key>
<string>mi</string>
<key>description</key>
<string>mile</string>
<key>multiplier</key>
<real>0.0001894</real>
</dict>
<dict>
<key>type</key>
<string>in</string>
<key>description</key>
<string>inch</string>
<key>multiplier</key>
<integer>12</integer>
</dict>
<dict>
<key>type</key>
<string>m</string>
<key>description</key>
<string>meter</string>
<key>multiplier</key>
<real>0.3048</real>
</dict>
<dict>
<key>type</key>
<string>km</string>
<key>description</key>
<string>kilometer</string>
<key>multiplier</key>
<real>0.0003048</real>
</dict>
<dict>
<key>type</key>
<string>cm</string>
<key>description</key>
<string>centimeter</string>
<key>multiplier</key>
<real>30.48</real>
</dict>
</array>
</dict>
<dict>
<key>name</key>
<string>Volume</string>
<key>conversions</key>
<array>
<dict>
<key>type</key>
<string>ft^3</string>
<key>description</key>
<string>cubic feet</string>
<key>multiplier</key>
<integer>1</integer>
</dict>
<dict>
<key>type</key>
<string>liter</string>
<key>description</key>
<string>liter</string>
<key>multiplier</key>
<real>28.31685</real>
</dict>
<dict>
<key>type</key>
<string>acre ft</string>
<key>description</key>
<string>acre foot</string>
<key>multiplier</key>
<real>2.3e-05</real>
</dict>
<dict>
<key>type</key>
<string>barrel</string>
<key>description</key>
<string>barrel [US, liquid]</string>
<key>multiplier</key>
<real>0.2374768</real>
</dict>
<dict>
<key>type</key>
<string>gallon</string>
<key>description</key>
<string>gallon [US, liquid]</string>
<key>multiplier</key>
<real>29.92208</real>
</dict>
<dict>
<key>type</key>
<string>quart</string>
<key>description</key>
<string>quart [US, liquid]</string>
<key>multiplier</key>
<real>29.92208</real>
</dict>
</array>
</dict>
</array>
</dict>
</plist>
I'm pretty new to Objective C, and I'm having trouble reading values from a plist into a set of NSArrays. I tried referencing this question but had no luck.
I have 3 arrays that I am trying to load into my picker. The 3 arrays I want to load in are the arrays conversions, typesFrom, and typesTo. typesFrom and typesTo contain the same values, but change depending on the type of conversion.
Currently, the picker will load with 3 columns but no rows. I feel like there's probably a simple solution, but I've been trying for hours and can't seem to find it.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Grab information from the plist
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle
pathForResource:@"conversions"
ofType:@"plist"
];
NSDictionary *dictionary = [[NSDictionary alloc]
initWithContentsOfFile:plistPath
];
// Store conversions
for(NSDictionary *dict in [dictionary valueForKey:@"Conversions"]) {
[self.conversions arrayByAddingObject:[dict valueForKey:@"name"]];
[self.categories arrayByAddingObject:[dict valueForKey:@"conversions"]];
}
[dictionary release];
[self loadIntoPicker:0];
}
- (void)loadIntoPicker:(NSInteger)index {
// Set the current category
NSArray *category = [self.categories objectAtIndex:index];
for (NSDictionary *conversion in category) {
[self.typesFrom arrayByAddingObject:[conversion valueForKey:@"type"]];
[self.typesTo arrayByAddingObject:[conversion valueForKey:@"type"]];
[self.descriptions arrayByAddingObject:[conversion valueForKey:@"description"]];
[self.multipliers arrayByAddingObject:[conversion valueForKey:@"multiplier"]];
}
[picker selectRow:0 inComponent:1 animated:YES];
[picker selectRow:0 inComponent:2 animated:YES];
[picker reloadComponent:1];
[picker reloadComponent:2];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if ( component == kCategoryComponent )
return [self.conversions count];
else
return [self.typesFrom count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if ( component == kCategoryComponent )
return [self.conversions objectAtIndex:row];
else if ( component == kConvertFromComponent )
return [self.typesFrom objectAtIndex:row];
else
return [self.typesTo objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if ( component == kCategoryComponent ) {
if ( row >= 0 )
[self loadIntoPicker:row];
}
}
The plist I'm using:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Conversions</key>
<array>
<dict>
<key>name</key>
<string>Speed</string>
<key>conversions</key>
<array>
<dict>
<key>type</key>
<string>mph</string>
<key>description</key>
<string>mile/hour</string>
<key>multiplier</key>
<integer>1</integer>
</dict>
<dict>
<key>type</key>
<string>ft/s</string>
<key>description</key>
<string>foot/second</string>
<key>multiplier</key>
<real>1.466667</real>
</dict>
<dict>
<key>type</key>
<string>km/h</string>
<key>description</key>
<string>kilometer/hour</string>
<key>multiplier</key>
<real>1.609344</real>
</dict>
<dict>
<key>type</key>
<string>m/s</string>
<key>description</key>
<string>meter/second</string>
<key>multiplier</key>
<real>0.44704</real>
</dict>
</array>
</dict>
<dict>
<key>name</key>
<string>Distance</string>
<key>conversions</key>
<array>
<dict>
<key>type</key>
<string>ft</string>
<key>description</key>
<string>feet</string>
<key>multiplier</key>
<integer>1</integer>
</dict>
<dict>
<key>type</key>
<string>yd</string>
<key>description</key>
<string>yard</string>
<key>multiplier</key>
<real>0.3333333</real>
</dict>
<dict>
<key>type</key>
<string>mi</string>
<key>description</key>
<string>mile</string>
<key>multiplier</key>
<real>0.0001894</real>
</dict>
<dict>
<key>type</key>
<string>in</string>
<key>description</key>
<string>inch</string>
<key>multiplier</key>
<integer>12</integer>
</dict>
<dict>
<key>type</key>
<string>m</string>
<key>description</key>
<string>meter</string>
<key>multiplier</key>
<real>0.3048</real>
</dict>
<dict>
<key>type</key>
<string>km</string>
<key>description</key>
<string>kilometer</string>
<key>multiplier</key>
<real>0.0003048</real>
</dict>
<dict>
<key>type</key>
<string>cm</string>
<key>description</key>
<string>centimeter</string>
<key>multiplier</key>
<real>30.48</real>
</dict>
</array>
</dict>
<dict>
<key>name</key>
<string>Volume</string>
<key>conversions</key>
<array>
<dict>
<key>type</key>
<string>ft^3</string>
<key>description</key>
<string>cubic feet</string>
<key>multiplier</key>
<integer>1</integer>
</dict>
<dict>
<key>type</key>
<string>liter</string>
<key>description</key>
<string>liter</string>
<key>multiplier</key>
<real>28.31685</real>
</dict>
<dict>
<key>type</key>
<string>acre ft</string>
<key>description</key>
<string>acre foot</string>
<key>multiplier</key>
<real>2.3e-05</real>
</dict>
<dict>
<key>type</key>
<string>barrel</string>
<key>description</key>
<string>barrel [US, liquid]</string>
<key>multiplier</key>
<real>0.2374768</real>
</dict>
<dict>
<key>type</key>
<string>gallon</string>
<key>description</key>
<string>gallon [US, liquid]</string>
<key>multiplier</key>
<real>29.92208</real>
</dict>
<dict>
<key>type</key>
<string>quart</string>
<key>description</key>
<string>quart [US, liquid]</string>
<key>multiplier</key>
<real>29.92208</real>
</dict>
</array>
</dict>
</array>
</dict>
</plist>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的数组未正确初始化或填充。
arrayByAddingObject: 返回包含添加对象的新数组,但您没有使用这些语句的返回值。
如果您的数组是
NSMutableArray
属性,请尝试以下代码:如果它们是
NSArray
类型,您可以像这样修改代码:It looks like your arrays are not being initialised or filled properly.
arrayByAddingObject:
returns the new array containing the added object, but you are not using the return value of those statements.If your arrays are
NSMutableArray
properties, try this code:If they are of
NSArray
type, you can modify the code like so: