<分区>
<分区>
我正在创建一个项目,其中包含选项卡栏应用程序、数据库和导航控件中的分段控件(以编程方式)以显示来自数据库的信息,并且我正在获取 SIGABRT 和 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因: '*** -[__NSArrayM insertObject:atIndex:]: 对象不能为 nil'
在控制台窗口中..
TableViewAppDelegate。
#import "SegmentsController.h"
#import "TableViewAppDelegate.h"
#import "RootViewController.h"
#import "AtoZHomePageViewController.h"
#import "CollectionRecipe.h"
#import "NSArray+PerformSelector.h"
@interface TableViewAppDelegate()
- (NSArray *)segmentViewControllers;
- (void)firstUserExperience;
@end
@implementation TableViewAppDelegate
@synthesize window;
@synthesize navigationController;
@synthesize tabbarController;
@synthesize recipes;
@synthesize segmentsController, segmentedControl;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSArray * viewControllers = [self segmentViewControllers];
// UINavigationController * navigationController = [[[UINavigationController alloc] init] autorelease];
self.segmentsController = [[SegmentsController alloc] initWithNavigationController:navigationController viewControllers:viewControllers];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[viewControllers arrayByPerformingSelector:@selector(title)]];
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[self.segmentedControl addTarget:self.segmentsController
action:@selector(indexDidChangeForSegmentedControl:)
forControlEvents:UIControlEventValueChanged];
databaseName = @"RecipeDatabase.sql";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
// Execute the "checkAndCreateDatabase" function
[self checkAndCreateDatabase];
// Query the database for all animal records and construct the "animals" array
[self readRecipesFromDatabase];
// Configure and show the window
[self firstUserExperience];
[window addSubview:[tabbarController view]];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
return YES;
}
#pragma mark -
#pragma mark Segment Content
- (NSArray *)segmentViewControllers {
UIViewController * AtoZRecipe = [[AtoZHomePage alloc] initWithNibName:@"AtoZRecipeController" bundle:nil];
UIViewController * RecipesCollection = [[CollectionRecipe alloc] initWithNibName:@"RecipeCollection" bundle:nil];
NSArray * viewControllers = [NSArray arrayWithObjects:AtoZRecipe, RecipesCollection, nil];
[AtoZRecipe release]; [RecipesCollection release];
return viewControllers;
}
- (void)firstUserExperience {
self.segmentedControl.selectedSegmentIndex = 0;
[self.segmentsController indexDidChangeForSegmentedControl:self.segmentedControl];
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}
-(void) checkAndCreateDatabase{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:databasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
-(void) readRecipesFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the animals Array
recipes = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select * from recipe order by name";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aAuthor=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,2)];
NSString *aThumbnail=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,3)];
NSString *aPre_time=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,5)];
NSString *aBake_time=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,6)];
NSString *aTota_ltime=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)];
NSString *alarge_image=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,8)];
NSString *asmall_image=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)];
NSString *asummary=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 10)];
NSString *aServe_size=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,11)];
// Create a new recipe object with the data from the database
AtoZHomePage *recipe=[[AtoZHomePage alloc] initWithName:aName author:aAuthor img_thumbnail:aThumbnail pre_Time:aPre_time bake_Time:aBake_time total_time:aTota_ltime large_Img:alarge_image small_Img:asmall_image summary:asummary serve_size:aServe_size];
// Add the recipe object to the recipes Array
[recipes addObject:recipe];
[recipe release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
self.segmentedControl = nil;
self.segmentsController = nil;
[recipes release];
[tabbarController release];
[navigationController release];
[window release];
[super dealloc];
}
@end
这一行是不明确的 NSArray * viewControllers = [NSArray arrayWithObjects:AtoZRecipe, RecipesCollection, nil];
因为我看到了大部分相关的帖子,如果我删除 nil 它将丢失 sentinel 并且根据我已经调用了适当的 View Controller 。
请帮我摆脱这个错误...提前致谢:):)
最佳答案
实际上您没有将内存分配给数组,这就是您获得 NSInvalidArgumentException 的原因
试试这个
NSArray * viewControllers = [NSArray arrayWithArray:[self segmentViewControllers]];
关于ios - NS无效参数异常 : [__NSArrayM insertObject:atIndex:]: object cannot be nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9614204/