2010-01-08 5 views

Répondre

5

Lors de la création d'un nouveau projet d'application iPhone dans Xcode, la plupart des modèles autorisent l'option «Utiliser les données de base pour le stockage». Lorsque cette option est cochée, le délégué de l'application inclut un code de base de données standard comprenant ce bloc qui ouvre et/ou crée le magasin persistant:

/** 
Returns the persistent store coordinator for the application. 
If the coordinator doesn't already exist, it is created and the application's store added to it. 
*/ 
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"App.sqlite"]]; 

    NSError *error = nil; 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { 
     /* 
     Replace this implementation with code to handle the error appropriately. 

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

     Typical reasons for an error here include: 
     * The persistent store is not accessible 
     * The schema for the persistent store is incompatible with current managed object model 
     Check the error message to determine what the actual problem was. 
     */ 
     //NSLog(@"Error with persistent store %@, %@", error, [error userInfo]); 
     NSLog(@"Error with persistent store (did the data model change?)"); 
     abort(); 
    } 

    return persistentStoreCoordinator; 
} 
+0

Vous voulez dire qu'il créera automatiquement sqlite comme stockage persistant. pls réponse bientôt. – uttam

+1

Oui, ce code le fait. Veuillez ACCEPTER les réponses à vos questions si les réponses vous ont aidé. – gerry3

+0

Si je veux ajouter des données, comment puis-je ajouter? – uttam

Questions connexes