Objective-C Object as a C Structure

Okay, so figuring out how to unwind an Objective-C object into its base representation goes against all that is object-oriented programming, however, its interesting none-the-less.

In Objective-C there is a directive, @defs(), that outputs (at compile time) the list of instance variables for a class. We can use this list to create a C structure with the variables of a specified class.

For instance, if we have a class named TestClass, here is how one might create a structure using the @defs directive:

struct testClassStructure

  @defsTestClass;
 *testClassAsStruct;

Before we look at how to use this structure to access the variables of TestClass, lets look at a simple interface and implementation of TestClass:

TestClass Interface

//  TestClass.h
//
#import <UIKit/UIKit.h>
 
@interface TestClass : NSObject 

  NSString *testString;
  int      testInteger;
  BOOL     testBoolean;

 
@property nonatomic, retain NSString *testString;
@property readonly int testInteger;
@property BOOL testBoolean;
 
@end

TestClass Implementation

//  TestClass.m
//  
#import "TestClass.h"
 
/*-------------------------------------
* TestClass implementation
*-------------------------------------*/
@implementation TestClass
 
@synthesize testString;
@synthesize testInteger;
@synthesize testBoolean;
 
- idinit 

  if self = super init 
  
    testString = NSString alloc initWithString:@"Fubar";
    testInteger = 99;
    testBoolean = YES;
  
  return self;

 
- voiddealloc 

  testString release;
  super dealloc;

@end

Pretty simple stuff. Our interface defines three instance variables, a string, an integer and a boolean. The implementation file, upon initialization of a TestClass object sets defaults values for each instance variable.

So, lets look at how to use a C structure to read/write the instance variables of TestClass:

AppDelegate Interface

//  UntitledAppDelegate.h
 
#import <UIKit/UIKit.h>
 
@interface UntitledAppDelegate : NSObject <UIApplicationDelegate> 

    UIWindow *window;

 
@property nonatomic, retain IBOutlet UIWindow *window;
 
@end

AppDelegate Implementation

//  UntitledAppDelegate.m
 
#import "UntitledAppDelegate.h"
#import "testClass.h"
 
@implementation UntitledAppDelegate
 
@synthesize window;
 
struct testClassStructure

  @defsTestClass;
 *testClassAsStruct;
 
- voidapplicationDidFinishLaunching:UIApplication *application 
 
 
  TestClass *tmp;
 
  tmp = TestClass alloc init;
 
  testClassAsStruct = struct testClassStructure * tmp;
 
  NSLog@"testString: %@", testClassAsStruct->testString;
  NSLog@"testInteger: %d", testClassAsStruct->testInteger;
  NSLog@"testBoolean: %s", testClassAsStruct->testBoolean == YES ? "Yes" : "No";
 
  testClassAsStruct->testString = @"A new string";
  testClassAsStruct->testInteger = 0;
  testClassAsStruct->testBoolean = NO;
 
  NSLog@"testString: %@", testClassAsStruct->testString;
  NSLog@"testInteger: %d", testClassAsStruct->testInteger;
  NSLog@"testBoolean: %s", testClassAsStruct->testBoolean == YES ? "Yes" : "No";
 
  tmp release;

 
- voiddealloc 

  window release;
  super dealloc;

 
@end

And of course, the output:

There you have it. An inside look at how to work with an Objective-C object using a C structure.

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *