Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing NSCache as data source for pickerview may crash
    primarykey
    data
    text
    <p>I have some data that was originally stored in sqlite and will be loaded to NSCache when first used. I copy the data from NSCache to a pickerview object and use the data to generate pickerview("retain" in the object).</p> <p>However, a small amount of users encounter crashes because the data in pickerview object is not as expected.</p> <p>Datasource.m:</p> <pre><code>- (NSDictionary *)getAllCurrenciesForCache { NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CurrencyList" ofType:@"json"]]; NSDictionary *dict = [data objectFromJSONData]; return dict; } - (NSArray *)getCurrencyList { NSDictionary *currencies = [cache objectForKey:@"key"]; if(!currencies){ NSDictionary *currencies = [self getAllCurrenciesForCache]; [cache setObject:currencies forKey:@"key"]; } NSArray *keys = [currencies allKeys]; if(keys){ // do some sorting return [keys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; } // shouldn't be here return nil; } </code></pre> <p>ViewController.m:</p> <pre><code> NSArray *data = [datasource getCurrencyList]; NSMutableArray *currencies = [[NSMutableArray alloc] init]; for (NSString *abbr in data) { [currencies addObject:[[MyClass alloc] initWithAbbr:abbr]]; } MyPicker *picker = [[MyPicker alloc] initWithData:[NSArray arrayWithArray:currencies]]; </code></pre> <p>MyPicker.h:</p> <pre><code>@property (nonatomic, retain) NSArray *currencies; </code></pre> <p>MyPicker.m:</p> <pre><code>- (id)initWithData:(NSArray *)data { self = [super init]; if (self) { self.currencies = data; self.datasource = self; self.delegate = self; } return self; } #pragma mark - UIPickerViewDataSource - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return self.currencies.count; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ // sometimes crashes MyClass *currency = self.currencies[row]; return [currency showString]; } #pragma mark - UIPickerViewDelegate - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ [self notifySelection]; } - (void)notifySelection { NSInteger index = [self selectedRowInComponent:0]; // here may crash because of index out of bound MyClass *currency = self.currencies[index]; // send the selected currency to view controller } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload