繁体   English   中英

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

[英]Best way to populate an array of Strings in objective C

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

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

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

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

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

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

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

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

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

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

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

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

如果你正在使用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);

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

正是阿图尔,你做对了。

转到XML文件,解析它,它将更容易处理。

编辑:

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

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

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

我会创建带有字典数组的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"]];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM