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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:29:03+00:00 2026-06-14T09:29:03+00:00

Since Objective-C 2.0, we’ve had @properties and autogenerated accessor methods. So today, what is

  • 0

Since Objective-C 2.0, we’ve had @properties and autogenerated accessor methods. So today, what is the point of key-value coding? Under what circumstances is it preferable to write

[myObject setValue:foo forKey:@"bar"];

instead of writing

[myObject setBar:foo];

or even

myObject.bar = foo;

I keep seeing articles and documentation making use of KVC, but always in a way where it seems like simply using properties would be better. So why would I ever use KVC? Thanks for any and all insight.

  • 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-14T09:29:04+00:00Added an answer on June 14, 2026 at 9:29 am

    It’s almost never preferable to write out [myObject setValue:foo forKey:@"bar"] by hand, with a literal @"bar". We usually use KVC to access a property when we don’t know which property we want to access until runtime.

    One example is an outlet in a xib. When you connect a text field’s delegate outlet to the File’s Owner in the xib, the xib records the connection as an object with three fields:

    • a reference to the text field (the object that has the outlet)
    • a reference to the file’s owner placeholder (the destination of the connection)
    • the name of the outlet as a string, @"delegate"

    At runtime, the xib loader (part of the UIKit framework) deserializes the text field. Then it deserializes the connection object and uses it to establish the connection that you wired up in the xib. The xib loader has to set a property of the text field (the delegate property), but it doesn’t know which property until it loads the xib at runtime, long after both your app and the UIKit framework were compiled.

    Another example of not knowing which property to access until runtime is the (little-known) ability of Core Animation to animate a custom property of your CALayer subclass. Say you create a subclass of CALayer called PolygonLayer, with a property named sides. You can animate the sides property using a standard CABasicAnimation:

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"sides"];
    animation.fromValue = @3;
    animation.toValue = @9;
    animation.autoreverses = YES;
    animation.duration = 1;
    [myPolygonLayer addAnimation:animation forKey:@"sides"];
    

    Presto, Core Animation will animate your layer’s sides property from 3 to 9 and back. Yet the source code of Core Animation doesn’t know anything about your sides property. (Check out this question for more details.)

    There are times we use KVC even though we know the property at compile-time. One example is when we want to take advantage of extra work KVC will do for us. For example, if you have an NSArray full of Person objects, and you want to get an array of every person’s first name, you could write this:

    NSMutableArray *firstNames = [NSMutableArray array];
    for (Person *person in people) {
        [firstNames addObject:person.firstName];
    }
    

    But this is a case where KVC has a feature that makes it simpler. If you access a property of an array using KVC, KVC will actually access that property of every element in the array for you:

    NSArray *firstNames = [people valueForKey:@"firstName"];
    

    Another example where we might use KVC even though we know the property at compile-time is when the property is not statically declared as part of the class. For example, each NSManagedObject (part of Core Data) dynamically gives itself properties based on whatever entity that instance of NSManagedObject is representing. You can access those properties using KVC, although generally we prefer to declare them in a subclass of NSManagedObject or in a category of NSManagedObject.

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

Sidebar

Related Questions

My problem is since an enum in objective-c essentially is an int value, I
how does addObserver: works under the hood ? Since Objective C can't overload operators...
I'm seeing an odd behavior with trying to get seconds since epoch in objective
I'm writing a program that calculates the Jacobi algorithm. It's written in Objective-C since
Objective: My script will download a remote file upon form submission, since the file
since today im facing a strange problem. When i start my app in my
Since Objective-C is basically an extension of C, Does the code get converted to
I'm rather confused about properties and instance variables in Objective-C. I'm about half-way through
After several months of coding in Objective-C, I completely understand when I need an
Objective-C properties default to atomic , which ensures that accessors are atomic but doesn't

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.