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

  • Home
  • SEARCH
  • 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 6233573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:19:29+00:00 2026-05-24T10:19:29+00:00

I have created an extension to NSObject to allow for object properties to be

  • 0

I have created an extension to NSObject to allow for object properties to be set from data contained in a PLIST or dictionary. I did use setValuesForKeysWithDictionary but this only works for keys not keyPaths. My extension does the same thing, except allows the PLIST to contain key paths as well as keys. For example, detailTextLabel.text @”detailed text” as the key value pair.

This works well, except it is possible to crash the app because of a typo in the PLIST. For instance, it will crash if the property name exists but is a different type that expected (a number instead of a string for instance). What is the best way to make this more robust and code defensively to avoid these types of errors?

I am already using - (void) setValue:(id)value forUndefinedKey:(NSString *)key {} in my objects to trap items in the PLIST that don’t correspond to actual keys.

#pragma mark -
#pragma mark Extensions to NSObject

@implementation NSObject (SCLibrary)

- (void) setValuesForKeyPathsWithDictionary:(NSDictionary *) keyPathValues {
    NSArray *keys;
    int i, count;
    id key, value;

    keys = [keyPathValues allKeys];
    count = [keys count];
    for (i = 0; i < count; i++)
    {
        key = [keys objectAtIndex: i];
        value = [keyPathValues objectForKey: key];

        [self setValue:value forKeyPath:key];
    }
}

Thanks in advance, Dave.

  • 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-24T10:19:31+00:00Added an answer on May 24, 2026 at 10:19 am

    Have added a general @try/@catch around the setValue:forKeyPath:

    @try {
        [self setValue:value forKeyPath:key];
    }
    @catch (NSException * e) {
        NSLog(@"%@",e);
    }
    

    Which does prevent the app from crashing, however I was hoping to find an approach that checked for the selector before trying to set its value.

    Will accept this answer if a better one is not forthcoming.

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

Sidebar

Related Questions

I have created a Generic Extension method for DataRow object. The method takes no
I have created a JavaScript extension for an array object as follows: Array.prototype.where =
I have created a Shell Extension using ATL COM Object . But during creation
i have installed SharePoint 2007 and VS2008 extension on my computer.i have created a
I have created an MS Access 2003 application, set up as a split front-end/back-end
I have created a Chrome extension that uses the hotkeys [Alt]+[0...9] only to discover
I need to associate a file extension I have created .rulog with notepad.exe as
I have a class that modifies data via some extension methods. In order to
I have created an extension for mediawiki that works in all major browsers other
I have created a namespace extension that is rooted under Desktop. The main purpose

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.