2

在 Objective-C 中,我正在寻找一种更简洁的方式来填充字符串数组。

我想填充一个数组,最简单的解决方案似乎是硬编码

NSArray *arr =  [[NSArray alloc] initWithObjects:@"First",@"Second",@"Third",nil];

我正在将此数组中的对象(字符串)用于我能够做到的 UIPicker。现在,一旦用户从选择器中选择了一个选项(第一个/第二个/第三个),我需要为每个选项设置一个相应的值。

就像在 html 中一样,我们拥有<option value="1">First</option>非常明确的名称值对之间的直接对应关系。

因此,一种选择是创建另一个数组,当用户在选择器中选择一个项目时,从相应位置的第二个数组中获取值并使用它。

我想知道我们是如何做到的,或者有更好的方法。在 xml(或其他东西)中有这样的静态数据并能够轻松地从那里读取,这样代码就不会被弄乱会更好吗?

4

7 回答 7

4

拥有多个并行访问的数组通常是一个坏主意。简单的替代方法是拥有一个字典数组,其中每个字典都有一个名称键和一个值键。如果它变得更复杂,您可以创建一个自定义对象而不是字典并拥有一个数组。

于 2012-11-26T15:35:00.713 回答
3

您可以使用 NSDictionary,并且语法可以很容易地声明:

NSDictionary* dict= @{ @"First" : @1  , @"Second" : @2 , @"Third" : @3 };
于 2012-11-26T15:38:48.650 回答
2

最好的方法是使用 NSDictionary(如果内容会改变,可能是 NSMutableDictionary)。您可以将键设为字符串值,并将值设为您想要的任何值。像这样的东西:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:firstValue, "firstString", secondValue, "secondString", etc, "etc...", nil];
于 2012-11-26T15:35:27.310 回答
1

如果您使用 aUIPickerView可以定义您的数组,就像您拥有的一样,或者使用数组文字语法:

NSArray *array = @[@"First", @"Second", @"Third"];

当您检查用户选择的内容时,您可能只是在获取UIPickerView方法 selectedRowInComponent ,它返回一个从零开始的索引。如果需要,您可以使用它来查找索引中的字符串

NSInteger index = [self.pickerView selectedRowInComponent:0];
NSString *selectedString = array[index];

如果您想进行反向查找,您可以使用indexOfObject检索从零开始的索引:

NSInteger index = [array indexOfObject:@"Third"];
NSLog(@"%d", index);

但我个人将数组用于表格视图、选择器视图等。

于 2012-11-26T17:44:26.957 回答
0

正是阿图尔,你说得对。

去找一个 XML 文件,解析它,它会更容易处理。

编辑:

解析后的 xml 内容可以存储在字典中以便更好地访问。

于 2012-11-26T15:35:10.273 回答
0

这是创建数组的简单方法。

NSString* string = @"first/second/third/fourth";
NSArray* arr = [[string componentsSeparatedByString:@"/"] retain];

但是为了更好地回答您的问题,我会使用一系列字典,因为您可以使用简单的 NSPredicate 轻松过滤它以提取值。这是一个例子。首先通过为每个字符串/值对制作一个字典并将它们放入一个数组中来创建你的字典数组......

NSDictionary* d1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"first", [NSNumber numberWithInteger:1], nil] forKeys:[NSArray arrayWithObjects:@"string", @"value", nil]];
NSDictionary* d2 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"second", [NSNumber numberWithInteger:2], nil] forKeys:[NSArray arrayWithObjects:@"string", @"value", nil]];
NSDictionary* d3 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"third", [NSNumber numberWithInteger:3], nil] forKeys:[NSArray arrayWithObjects:@"string", @"value", nil]];
NSArray* stringsAndValues = [[NSArray alloc] initWithObjects:d1, d2, d3, nil];

在您的 UIPicker 的某个时刻,您将返回字符串值。以下是 NSPredicate 如何轻松获得价值。在这种情况下,我会假设“第二”是选择......

NSString* stringValue = @"second";
NSPredicate* thePred = [NSPredicate predicateWithFormat:@"string == %@", stringValue];
NSArray* a = [stringsAndValues filteredArrayUsingPredicate:thePred];
NSInteger value = [[[a objectAtIndex:0] valueForKey:@"value"] integerValue];
于 2012-11-26T15:42:21.053 回答
0

我会用字典数组创建 plist 文件:

<plist version="1.0">
<array>
    <dict>
        <key>title</key>
        <string>first</string>
        <key>value</key>
        <string>first_value</string>
    </dict>
    <dict>
        <key>title</key>
        <string>second</string>
        <key>value</key>
        <string>second_value</string>
    </dict>
</array>
</plist>

加载它:

NSString *filename = @"pickerData.plist";
NSString *pathToFile = [[NSBundle mainBundle].bundlePath stringByAppendingPathComponent:filename];
self.items = [NSArray arrayWithContentsOfFile:pathToFile];

然后通过选择器轻松使用数据:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [self.items count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [[self.items objectAtIndex:row] objectForKey:@"title"];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [self doSomethingWithValue:[[self.items objectAtIndex:row] objectForKey:@"value"]];
}
于 2012-11-26T16:45:03.907 回答