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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:07:43+00:00 2026-06-06T23:07:43+00:00

I have a bunch of simple NSManagedObject s I create in a unit test.

  • 0

I have a bunch of simple NSManagedObjects I create in a unit test. They just have a single name attribute of type NSString *. I always give my NSManagedObject the same entityName and Class name.

I want to avoid having to write the following code 30 times to set up a unit test:

@interface FooTest : GHTestCase {
Foo *foo;
}
@end
@implementation FooTest

- (void) setUp {
  [super setUp];

  foo = [NSEntityDescription insertNewObjectForEntityForName:@"Foo"
                                      inManagedObjectContext:managedObjectContext];
  foo.name = @"foo";
}
@end

Since foo is an ivar, I would think I should be able to write a macro to grab the type of foo (Foo), and use to create my Foo:

#define InsertManagedObjectByVariable(variable) \
do { \
variable = [NSEntityDescription insertNewObjectForEntityName:NSStringFromClass([typeof(variable) class])]; \
variable.name = (NSString *) CFSTR(#variable);
} while(0)

However, this causes the following warning in clang:

variable = [NSEntityDescription insertNewObjectForEntityName:NSStringFromClass([typeof(variable) class])];
                                                                               ^
                                                             Expected expression

I also thought I could try to determine the type using the objective-c runtime IVar from Ivar class_getInstanceVariable(Class cls, const char* name), but the only IVar type information available from the type encoding from ivar_getTypeEncoding is id, which isn’t enough.

Can someone think of a way to obtain the type information of an IVar either at compile time or runtime?

  • 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-06T23:07:45+00:00Added an answer on June 6, 2026 at 11:07 pm

    I haven’t tried obtaining class information from an ivar, but I know that @property declarations do encode information about the class. For instance, this property declaration:

    @property (copy) NSString *normalString;
    

    results in this attribute string (retrieved using property_getAttributes()) at runtime:

    T@"NSString",C,VnormalString
    

    I’ve written some open source parsing code for this information.

    Once you have the class name, you can convert it into an actual Class object using NSClassFromString(), and message the result from there.

    Disclaimer: This probably shouldn’t be depended upon for production applications, as it is undocumented.

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

Sidebar

Related Questions

Does this control have a name? Or is it just a bunch of simple
Im sure there is a very simple solution for this. I have a bunch
I have a simple autocomplete field that spits out a bunch of suggested words
I have a simple SQL query in PostgreSQL 8.3 that grabs a bunch of
I have a bunch human reading simple sentence (hello world) as a wav file,
i have A bunch of fabric patterns (simple jpg files) An image for every
Simple question. Say I have a bunch of NIL's in my list q .
I have a bunch of very simple functions. Each function has one input and
we have a bunch of mod_rewrite rules. these are simple URI + GeoIP Continent
I have a bunch of simple entity instances that I have serialized to a

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.