在目标C中填充字符串数组的最佳方法

时间:2012-11-26 15:30:57

标签: ios objective-c nsarray uipickerview

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

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

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

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

与html一样,我们有<option value="1">First</option>,它使名称对之间的直接对应非常明确。

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

我想知道我们是怎么做的,还是有更好的方法。将这样的静态数据放在xml(或其他东西)中并且能够从那里轻松读取以便代码不会搞砸是不是更好?

7 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

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

NSDictionary* dict= @{ @"First" : @1  , @"Second" : @2 , @"Third" : @3 };

答案 2 :(得分:2)

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

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:firstValue, "firstString", secondValue, "secondString", etc, "etc...", nil];

答案 3 :(得分:1)

如果你正在使用UIPickerView可以像你一样定义你的数组,或者使用数组文字语法:

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);

但我个人使用数组来查看表视图,选择器视图等等。

答案 4 :(得分:0)

正是阿图尔,你做对了。

转到XML文件,解析它,处理起来会更简单。

编辑:

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

答案 5 :(得分:0)

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

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

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

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];

答案 6 :(得分: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"]];
}
相关问题