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 6946777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:36:32+00:00 2026-05-27T13:36:32+00:00

I’m working with Core Data and got two entities (Account, Contact) where one account

  • 0

I’m working with Core Data and got two entities (Account, Contact) where one account can have multiple contacts (one-to-many-relationship).

When inserting a new object I want to check if the phone number of the new contact already belongs to an existing contact. If the number exists, there should be a notification for the user.

In “ContactAddViewController.m” the following method is called where contactData is a NSMutableDictionary with the values and keys for the new contact (entered in textfields).

[ContactInfo findOrCreateContact:contactData inManagedObjectContext:context];

In “AccountInfo.m” the request for the phone with the predicate and the value of my textfield is set. If the phone number doesn’t exist, the contact will be added to the entity “ContactInfo” – if it exists, the method returns 0.

+ (ContactInfo *)findOrCreateContact:(NSDictionary *)data inManagedObjectContext:(NSManagedObjectContext *)context
{
    ContactInfo *contactInfo = nil;

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"ContactInfo" inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"phone == %@", [data objectForKey:@"phone"]];

    NSError *error = nil;
    contactInfo = [[context executeFetchRequest:request error:&error] lastObject];

    if (!error && !contactInfo) 
    {
        // if phone number can't be found, create new contact
        contactInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ContactInfo" inManagedObjectContext:context];
        contactInfo.firstName = [data objectForKey:@"firstName"];
        contactInfo.lastName = [data objectForKey:@"lastName"];
        contactInfo.phone = [data objectForKey:@"phone"];
        contactInfo.updatedDate = [NSDate date];
        contactInfo.cinfoTOainfo = [AccountInfo findOrCreateAccount:data inManagedObjectContext:context];
    }

    // Return object
    return contactInfo;
}

__

Question

How can I set the return value of 0 to a NSString (or sth like that), so that I can build up an if-clause like the following?

if ( ... ) // What should I put in here?
{
    // if phone number doesn't exist, do that.
}
else
{
    // if phone number exist, do that.
}
  • 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-05-27T13:36:33+00:00Added an answer on May 27, 2026 at 1:36 pm

    You mean the return value from that method?

    + (ContactInfo *)findOrCreateContact:(NSDictionary *)data inManagedObjectContext:(NSManagedObjectContext *)context
    

    First the naming of the function state find OR create so with a name like that you should return an object in both case, the one you have find or the one just created. That would avoid possible confusion.

    Second, Your return type is an object, so you should return nil or an object.

    So at this point you have to decide if you want to comply with your naming or not, but let say you want.
    You could have a method like this :

    + (ContactInfo *)findOrCreateContact:(NSDictionary *)data 
                  inManagedObjectContext:(NSManagedObjectContext *)context
                       contactWasCreated:(BOOL *)isNew
    

    By passing a pointer to a BOOL you can get set it to YES if the ContactInfo was created and NO if you are returning an one that already existed, and you can still return nil if you wasn’t able to have a ContactInfo to return. Passing a BOOL as a pointer is inspired by - (BOOL)save:(NSError **)error where you are passing a pointer to an object an can retreive the NSError if needed.
    So in your if you could say

    if (contanctInfo && wasCreated)
    {
        // I've got a new one
    }
    else if (contactInfo) 
    {
         // I just receive an old one
    }
    else
    {
         // something very bad just happened...
    }
    

    You could also decide to return a NSDictionary with the ContactInfo as one entry and a NSNumber for the BOOL as an other entry.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.