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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:09:40+00:00 2026-06-08T03:09:40+00:00

Consider the following code: #import <Foundation/Foundation.h> #import <objc/runtime.h> typedef NSString* MyStringRef; typedef NSString MyString;

  • 0

Consider the following code:

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

typedef NSString* MyStringRef;
typedef NSString MyString;

@interface ClassA : NSObject
@property (nonatomic, copy) MyStringRef stringA;
@property (nonatomic, copy) MyString *stringB;
@end

@implementation ClassA
@synthesize stringA = _stringA;
@synthesize stringB = _stringB;
@end

int main() {
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([ClassA class], &count);
    for (unsigned int i = 0; i < count; i++) {
        Ivar thisIvar = ivars[i];
        NSLog(@"thisIvar = %s, %s", ivar_getName(thisIvar), ivar_getTypeEncoding(thisIvar));
    }

    ClassA *a = [[ClassA alloc] init];
    NSLog(@"Out: %@", [a valueForKey:@"stringA"]);
    NSLog(@"Out: %@", [a valueForKey:@"stringB"]);
}

This is the output:

$ clang --version
Apple clang version 3.1 (tags/Apple/clang-318.0.58) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.0
Thread model: posix

$ clang -o typedef -fobjc-arc -framework Foundation typedef.m && ./typedef
2012-06-06 20:14:15.881 typedef[37282:707] thisIvar = _stringA, @"NSString"
2012-06-06 20:14:15.884 typedef[37282:707] thisIvar = _stringB, ^{NSString=#}
2012-06-06 20:14:15.885 typedef[37282:707] Out: (null)
2012-06-06 20:14:15.888 typedef[37282:707] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ClassA 0x7fabe0501480> valueForUndefinedKey:]: this class is not key value coding-compliant for the key stringB.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff835fef56 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff878e5d5e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff836891b9 -[NSException raise] + 9
    3   Foundation                          0x00007fff83e77703 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 240
    4   Foundation                          0x00007fff83dae38e _NSGetUsingKeyValueGetter + 108
    5   Foundation                          0x00007fff83dae315 -[NSObject(NSKeyValueCoding) valueForKey:] + 392
    6   typedef                             0x000000010e84bc6d main + 317
    7   typedef                             0x000000010e84b9c4 start + 52
)

The question I have here is what is it about Objective-C that causes the typedef NSString MyString to effectively create a struct that contains one variable of type Class and then uses that where I use MyString. e.g. the struct looks like this (after consulting this):

struct NSString {
    Class a;
};

It sort of makes sense but causes the valueForKey: to fail, presumably because it’s a struct now, so it can’t return that in the same way as objects. Or more precisely, it falls into the “throw exception” part of the search order described in the docs.

I’d just like to understand what it is about the language that causes this to happen and why it can’t treat my 2 typedefs in the same way.

  • 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-08T03:09:41+00:00Added an answer on June 8, 2026 at 3:09 am

    I got an answer to this at WWDC 2012. It turns out that this behaviour is expected as is for binary compatibility with GCC.

    Seems odd to me that they’d effectively introduce what I would consider a bug to be binary compatible with old GCCs. I hope that this sort of thing will eventually be phased out in favour of a correct compiler.

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

Sidebar

Related Questions

Consider the following code: A.java: import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @interface A{} C.java: import
Consider the following code: Here is my price calculator controller header file. #import <Foundation/Foundation.h>
Consider the following code: public interface A { public A another(); } public interface
consider the following python code: import gtk class MainWindow(): def __init__(self): self.window = gtk.Window()
Consider the following code. public interface IFoo { } public class Bar { public
Consider following code: main.cpp: #include <iostream> typedef void ( * fncptr)(void); extern void externalfunc(void);
Consider the following Python code: import os print os.getcwd() I use os.getcwd() to get
Consider the following code snippet import qualified Foreign.Concurrent import Foreign.Ptr (nullPtr) main :: IO
Consider the following code: module ftwr; import std.regex; import std.stdio; import std.conv; import std.traits;
Consider the following code: from numpy import log2 import matplotlib.pyplot as plt xdata =

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.