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

  • Home
  • SEARCH
  • 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 8483507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:07:25+00:00 2026-06-10T20:07:25+00:00

I’m implementing a objective C wrapper for Box2d (which is written in c++). The

  • 0

I’m implementing a objective C wrapper for Box2d (which is written in c++). The b2Body keeps a reference to its wrapper B2Body in its userData field. GetUserData returns a void*. I’m now implementing fast iteration for getting the B2Bodies out of the B2World.

I get an ‘Assigning to ‘id’ from incompatible type ‘B2Body *’ error at the line indicated below. Why?

#import "B2Body.h"
#import "B2World.h"
#import "Box2d.h"

@implementation B2World

-(id) initWithGravity:(struct B2Vec2) g
{
  if (self = [super init])
  {
    b2Vec2 *gPrim = (b2Vec2*)&g;
    _world = new b2World(*gPrim);
  }

  return self;
}

- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len;

{
  if(state->state == 0)
  {
    state->mutationsPtr = (unsigned long *)self;
    state->extra[0] = (long) ((b2World*)_world)->GetBodyList();
    state->state = 1;
  }

  // pull the box2d body out of extra[0]
  b2Body *b = (b2Body*)state->extra[0];

  // if it's nil then we're done enumerating, return 0 to end
  if(b == nil)
  {
    return nil;
  }

  // otherwise, point itemsPtr at the node's value
  state->itemsPtr = ((B2Body*)b->GetUserData()); // ERROR
  state->extra[0] = (long)b->GetNext();

  // we're returning exactly one item
  return 1;
}

`

B2Body.h looks like this:
#import

@interface B2Body : NSObject
{
  int f;
}

-(id) init;
@end
  • 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-10T20:07:27+00:00Added an answer on June 10, 2026 at 8:07 pm

    NSFastEnumerationState is a C structure, and the itemsPtr field is:

    id __unsafe_unretained  *itemsPtr;
    

    In earlier versions, the __unsafe_unretained specifier was obviously missing.

    Note, that the field itemsPtr is a pointer-to-id. Since id is essentially a pointer, itemsPtr is a pointer to an object pointer. Actually, this field is what holds the array of objects that allows the fast enumeration. Basically, it trolls through this array of object pointers.

    Since I know nothing about Box2d, that’s about all I can say. Assuming b->GetUserData() returns a pointer to an array of objects, you should be able to do this:

    state->itemsPtr = (__unsafe_unretained id *)b->GetUserData();
    

    While a bit dated, Mike Ash’s article is still a great source for implementing fast enumeration.

    EDIT

    Just noticed that you are returning a single object. So, I assume GetUserData just returns a single object pointer. Since you need to return a pointer to object pointers, you would need to do something like this:

    id object = (__bridge id)b->GetUserData();
    state->itemsPtr = &object;
    

    However, that stack object will be gone once you return from this method, which is why you are passed a stack buffer you can use. Thus, you should probably stuff that single pointer into the provided stack buffer:

    *buffer = (__bridge id)b->GetUserData()
    state->itemsPtr = buffer;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select an H1 element which is the second-child in its group
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I have an array which has BIG numbers and small numbers in it. I

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.