Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9072481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:10:41+00:00 2026-06-16T18:10:41+00:00

I’m trying to save a string into a database every time a button is

  • 0

I’m trying to save a string into a database every time a button is pressed but when I run the project, I get that on my console: 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Info''.

Referring to the Data Model, I have created a .xcdatamodeld with an Entity named ‘Info’ and, inside it, an attribute named ‘path’ with a type of string.

I’ve created three functions. “enterdata” Checks if the name is avaliable or not by calling “findData”. If the name is avaliable, a new data is recorded throught “newData”, if not, it looks for a different name.

I’ve been looking for some similar questions and I’ve found out this. It says that de ManagedObjectContext has to be passed to the View Controller but I don’t understand what does it mean.

Here’s my .h code:

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

Here’s my .m code:

#import <CoreData/CoreData.h>

@synthesize managedObjectContext;

int  iSavedNum = 1;
bool bCanSave;

//Enter data
- (IBAction) enterdata:(id)sender {

    //Search if data is already registered
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [NSString stringWithFormat:@"%@/info%i.png",docDir, iSavedNum];
    [self findData:path :@"path"];

    //If data is already saved, save it with new name.
    if (bCanSave == NO) {
        for (iSavedNum = 1; bCanSave == YES; iSavedNum++) {
            [self findData:path :@"path"];
            if (bCanSave == YES) {
                [self newData:path :@"path"];
            }
        }
    } else {
        [self newData:path :@"path"];
    }

}

//Input new data
- (void) newData:(NSString *)value:(NSString *)key {

    //Create ManagedObjectContext and ManagedObjectModel
    __0AppDelegate *appDelegate = (__0AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSManagedObjectModel *newRecord;

    //Put the data to the Entity
    NSString *entityName = @"Info";
    newRecord = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
    [newRecord setValue:value forKey:key];

    //Errors management and cheking
    NSError *error;
    [context save:&error];
    NSLog(@"Info Saved. Value: %@ Key: %@", value, key);

}

//Find Data
- (void) findData:(NSString *)valor:(NSString *)key {

    //Create ManagedObjectContext
    __0AppDelegate *appDelegate = (__0AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    //Call the Entity and make a request
    NSString *entityName = @"Info";
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    //Create predicate to call specific info
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(%@ = %@)", key, valor];
    [request setPredicate:pred];

    //Errors management and creation of an array with found info
    NSError *error;
    NSArray *objects = [context executeFetchRequest:request error:&error];

    //Set if the name is avaliable or not
    if ([objects count] == 0) {
        bCanSave = YES;
    } else {
        bCanSave = NO;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:10:42+00:00Added an answer on June 16, 2026 at 6:10 pm

    It tells you exactly what the error is:

    nil is not a legal NSManagedObjectContext parameter

    That means that on this line:

    newRecord = [NSEntityDescription insertNewObjectForEntityForName:entityName
                                              inManagedObjectContext:context];
    

    The variable context is nil. This means that your managedObjectContext method isn’t working correctly. You don’t show this so there’s not much more we can add.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.