出于某种原因,以下 NSData 行导致我的应用程序崩溃。这是控制台向我抛出的错误:
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“NSConcreteAttributedString initWithString::nil value” *
有谁知道这可能是为什么?我正在使用 XCode 5,在 XCode 4 中构建时似乎没有这个问题。是否有替代的代码字符串可以解决这个问题?如果我删除这些行,则不会发生崩溃。但是,我需要保存我的数据,哈哈。
NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:strainsfinal];
视图控制器.m
-(void)arrayUpdated
{
Strains = appdelegate.strainsfinal;
[StrainTableView reloadData];
NSLog(@"notification received");
}
- (int)numberOfSectionsInTableView: (UITableView *)tableview
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [Strains count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strainTableIdentifier = @"StrainTableCell";
StrainTableCell *cell = (StrainTableCell *)[tableView dequeueReusableCellWithIdentifier:strainTableIdentifier];
if (cell == nil)
cell = [[StrainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strainTableIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StrainTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(@"Using the search results");
cell.titleLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
cell.descriptionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Description"];
cell.ratingLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Rating"];
cell.ailmentLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Ailment"];
cell.actionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Action"];
cell.ingestLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Ingestion"];
NSLog(@"%@", searchResults);
} else {
NSLog(@"Using the FULL LIST!!");
cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
cell.descriptionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Description"];
cell.ratingLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Rating"];
cell.ailmentLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Ailment"];
cell.actionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Action"];
cell.ingestLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Ingestion"];
}
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
strainsfinal = [[NSMutableArray alloc] init];
NSData *dataSave = [[NSUserDefaults standardUserDefaults] objectForKey:@"strains"];
if (dataSave != Nil) {
Strains = [NSKeyedUnarchiver unarchiveObjectWithData:dataSave];
// strainsfinal = [Strains copy];
}
// Override point for customization after application launch.
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:@"http://urlhere.com/file.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
bool firstlaunch = [[NSUserDefaults standardUserDefaults] boolForKey:@"firstlaunch"];
if (firstlaunch != YES) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"arthritisswitch"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"cancerswitch"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hivswitch"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"insomniaswitch"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"migrainesswitch"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstlaunch"];
}
// Do any additional setup after loading the view, typically from a nib.
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
[data appendData:theData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSData *dataSave = [[NSUserDefaults standardUserDefaults] objectForKey:@"strains"];
NSMutableArray *oldStrains = [NSKeyedUnarchiver unarchiveObjectWithData:dataSave];
NSMutableSet *blankArray = [[NSMutableSet alloc] init];
// Strains = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
Strains = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:nil];
if (oldStrains.count < Strains.count) {
for (int x = oldStrains.count - 1; x < Strains.count; x++) {
[oldStrains addObject:[Strains objectAtIndex:x]];
}
}
if (Strains.count > 0) {
blankArray = [Strains copy];
NSSortDescriptor *sortDescriptor = nil;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Title"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
Strains = [[blankArray allObjects] sortedArrayUsingDescriptors:sortDescriptors];
int count = 0;
for (NSDictionary *item in Strains) {
[item setValue:[NSNumber numberWithInt:count] forKey:@"position"];
if ([item valueForKey:@"checked"] == nil) {
bool checked = NO;
[item setValue:[NSNumber numberWithBool:checked] forKey:@"checked"];
}
count++;
}
dataSave = [NSKeyedArchiver archivedDataWithRootObject:Strains];
[[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:@"strains"];
[strainsfinal addObjectsFromArray:Strains];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure that you're connected to 3G or Wi-Fi." delegate:nil
cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
-(void)updateStrains:(NSDictionary *)item
{
/*
NSLog(@"updating items");
NSNumber *position = [NSNumber numberWithInt:[[item objectForKey:@"position"] intValue]];
NSIndexPath *itemIndex = [NSIndexPath indexPathForRow:[position intValue] inSection:0];
NSLog(@"%@",itemIndex);
[strainsfinal replaceObjectAtIndex:itemIndex.row withObject:item];
*/
NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:strainsfinal];
[[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:@"strains"];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"arrayupdated" object:self userInfo:nil];
}