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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:54:27+00:00 2026-05-25T05:54:27+00:00

I have two classes with the same set of properties, declared using the @property

  • 0

I have two classes with the same set of properties, declared using the @property directive in a protocol, they both implement. Now I was wondering if it is possible to automatically populate an instance of the first class with the values from an instance of the second class (and vice-versa).
I would like this approach to be robust, so that if I change the of properties declared in the protocol there will be no need to add extra code in the copying methods.

  • 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-25T05:54:28+00:00Added an answer on May 25, 2026 at 5:54 am

    Yes, given the exact context there could be various approaches to this problem.

    One I can think of at the moment is to first get all the properties of source object then use setValue:value forKey:key to set the values on the target object.

    Code to retrieve all custom properties:

    -(NSSet *)propertyNames {
      NSMutableSet *propNames = [NSMutableSet set];
      unsigned int outCount, i;
      objc_property_t *properties = class_copyPropertyList([self class], &outCount);
      for (i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        NSString *propertyName = [[[NSString alloc] 
          initWithCString:property_getName(property)] autorelease];
        [propNames addObject:propertyName];
      }
      free(properties);
    
      return propNames;
    }
    

    You may want to checkout the Key-Value Coding Programming Guide for more information.

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

Sidebar

Related Questions

I have two classes generated by LINQ2SQL both from the same table so they
I have two Service classes which implement the same interface ServiceClass1 @Service public class
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have two classes: Action and MyAction . The latter is declared as: class
I have two classes: Account and Operator. Account contains a list of Operators. Now,
I have two related classes which share a common interface and are both stored
If I have those two classes that have two different properties but with the
I have two objects that contain some properties that are exactly the same (same
I'm using the Unity dependency injection framework. I have two classes, that each take

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.