![](/img/trans.png)
[英]Objective-C/Xcode 6: Best way to have a Search Bar populate a table view?
[英]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.