Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm a beginner with iOS but I have done some example with CoreData to store users info.</p> <p>First, you need to create your model with your entity (I suppose you have already done). In my example, my entity is called "User".</p> <p>First, add a property similar to this</p> <pre><code>NSManagedObjectContext *context; </code></pre> <p>to your ViewController class.</p> <p>Second, in your viewDidLoad method, add this two lines:</p> <pre><code>AppDelegate *appdelegate = [[UIApplication sharedApplication]delegate]; context = [appdelegate managedObjectContext]; </code></pre> <p>And third, store your info:</p> <pre><code>NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context]; NSManagedObject *newUser = [[NSManagedObject alloc]initWithEntity:entitydesc insertIntoManagedObjectContext:context]; [newUser setValue:(NSString *)[dictionary objectForKey:@"name"] forKey:@"name"]; [newUser setValue:(NSString *)[dictionary objectForKey:@"surname"] forKey:@"surname"]; ... NSError *error; [context save:&amp;error]; </code></pre> <p>(I take my properties from a NSDictionary called dictionary)</p> <p>To read your info:</p> <pre><code>AppDelegate *appdelegate = [[UIApplication sharedApplication]delegate]; context = [appdelegate managedObjectContext]; NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc]init]; [request setEntity:entitydesc]; //NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NULL"]; [request setPredicate:nil]; NSError *error; NSArray *matchingData = [context executeFetchRequest:request error:&amp;error]; //NSArray *matchingData = [context executeFetchRequest:nil error:&amp;error]; // If the user is not logged in previously if (matchingData.count &lt;=0 ){ //self.displaylabel.text = @"No person find"; } else { // If the user is already logged in for (NSManagedObject *obj in matchingData) { AppDataModel *appDataModel=[AppDataModel getInstance]; appDataModel.appUserInfo = [User alloc]; appDataModel.appUserInfo.name = [obj valueForKey:@"name"]; appDataModel.appUserInfo.surname = [obj valueForKey:@"surname"]; } } </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.
    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