V
V
Votetoda2014-12-24 00:06:18
Objective-C
Votetoda, 2014-12-24 00:06:18

How to organize core data without tableview?

Good afternoon! There was a need to organize the saving and output of a variable using Core data, there are many examples on the Internet. But they all use tableview , you need a simple action to write a variable and output a variable. Thank you all.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
An, 2014-12-24
@Votetoda

You see, the CoreData Framework is designed to store and manage an object graph. That is why UITableView is used everywhere. It allows you to display a hierarchical structure without any problems, there would be a standard control for drawing graphs, they would use it. Well, Core Data is not designed to store variables - it's like riding a plane on a rural road. I strongly recommend reading at least the first two chapters https://developer.apple.com/library/mac/documentat... To save some variables, it's best to use NSUserDefault or NSKeyedArchiver
Just in case you still want to write something to CoreData , then look at the Creating and Deleting Managed Objects and Fetching Managed Objects chapters. Well, before writing a "variable", it will need to be represented as an object ...

M
Maxim Prigozhenkov, 2014-12-26
@Waka_Waka

It's very easy to do.
The first step is to connect Core Data to the class in which you are going to implement something using Core Data.
It is done like this:

-(NSManagedObjectContext *)managedObjectContext{
    NSManagedObjectContext *context = nil;
    id delegate = ;
    if (newHuman != nil) {
        newHuman.firstName = self.firstNameField.text;
        newHuman.lasteName = self.lastNameField.text;
        newHuman.phoneNamber = self.phoneNumberField.text;
}

NSError *savingError = nil;

if (![context save:&savingError]) {
NSLog(@"Eroor");
}

Getting data from CoreData will look like this:
NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Humans" inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity];
    
    NSMutableArray *array = [context executeFetchRequest:fetchRequest error:nil]; //Массив можно определить и в другом месте

NSManagedObject *newCell = [self.array objectAtIndex:0]; //Тут надо указать какой-либо индекс (обычно в tableview используется indexPatch.row). Можно так же быстрым перебором получить все.

//И записываем в переменные
NSString *name = [newCell valueForKey:@"firstName"];
NSString *lastName = [newCell valueForKey:@"lasteName"];
NSString *phoneNum = [newCell valueForKey:@"phoneNamber"];

It's so simple))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question