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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:31:13+00:00 2026-06-01T23:31:13+00:00

Here is the compare operation I would like to do: // foobar is the

  • 0

Here is the compare operation I would like to do:

// foobar is the name of an ivar on some class
// i.e. NSDate *foobar;
const char *ivarType = [self getTypeForInstanceVariableWithName:@"foobar"];
const char *objType  = @encode(NSDate);

if (strcmp(ivarType, objType) == 0) {
    //set value 
} 

NSLog(@"comparing %s with %s", ivarType, objType);

Helper method:

- (const char*)getTypeForInstanceVariableWithName:(NSString *)name {
     return ivar_getTypeEncoding(class_getInstanceVariable([self class], [name cStringUsingEncoding:NSUTF8StringEncoding]));
}

NSLog result:

comparing @"NSDate" with {NSDate=#}

How come @encode returns a different type syntax than ivar_getTypeEncoding()? Is there a better way to accomplish this type determination? I must be missing something here…
Thanks!

  • 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-01T23:31:15+00:00Added an answer on June 1, 2026 at 11:31 pm

    When using ivar_getTypeEncoding() you have to pay attention to the first char. Let’s see an example:

    If you have a primitive type the first char will be all you get, for int it will be ‘i’, for char ‘c’, for unsigned long long ‘Q’, etc…

    For objects and classes you might get more, like on your example, but the first character is what you want, like @ for objects, # for classes, : for selectors.

    You can read about those types here. You can also use constants when comparing, like _C_ID, _C_CLASS, etc. Have a loot at runtime.h.

    C arrays seem to be more tricky, but I’m sure you can get around that.

    One problem in your code is that you’re getting the type encoding for the NSDate class, not for a NSDate object. So instead of comparing to @encode(NSDate) compare to @encode(NSDate *).

    To see this type of code in action have a look at Adium’s code for example.

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

Sidebar

Related Questions

With code like the following public class Task { string Name; public static bool
I am not sure if I am missing something here. I would like to
I'm looking to analyze and compare the following `signals': (Edit: better renderings here: oscillations
Here is some code I made :) @echo off set source=R:\Contracts\ set destination=R:\Contracts\Sites\ ROBOCOPY
Here is an example: I have a file 1.js, which has some functions. I
here is the scenario 1: user starts to type some word, autocomplete engine shows
How would you implement in Java the binary tree node class and the binary
I would like to do MySQL query like this: if((value1 != dbPrimaryValue1) OR (value2
From cplusplus.com: template < class Key, class Compare = less<Key>, class Allocator = allocator<Key>
Is there a name for the following type of set/list transposition operation? (Probably abusing

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.