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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:45:38+00:00 2026-05-28T00:45:38+00:00

I have searched and can’t find the assert to this. I know it must

  • 0

I have searched and can’t find the assert to this. I know it must be a fundamental thing I’m missing here.

I have an enum:

typedef enum  {
    NINETYBEND, NINETYBEND_FAST, NINETYBEND_SLOW, STRAIGHT
} BlockTypeEnum;

I am trying to use these values in objects I am creating like this:

BlockTypeEnum blockType = STRAIGHT;
XMLLevelPiece* piece = [[XMLLevelPiece alloc] init];
[piece initPiece:blockType];

My problem occurs when I try to use the same variable twice. If I create one object with an enum, change the enum and then create a second object using it, the enum in my first object changes to the second enum value. This is not what I want. Example below:

BlockTypeEnum blockType = STRAIGHT;

XMLLevelPiece* piece = [[XMLLevelPiece alloc] init];
[piece initPiece:blockType];

blockType = NINETYBEND_FAST;

XMLLevelPiece* piece2 = [[XMLLevelPiece alloc] init];
[piece2 initPiece:blockType];

NSLog([NSString stringWithFormat:@"%d", [piece getBlockType]]);
NSLog([NSString stringWithFormat:@"%d", [piece2 getBlockType]]);

//BOTH BLOCK TYPES ARE NOW NINETYBEND_FAST, NOT WHAT I WANTED!!

As far as I understood, an enum is just a glorified int, not a pointer, and I am reassigning the variable after adding to the first object. Please can someone tell me what I’m missing! Thanks very much, Simon.

Here is my code for XMLPiece, thanks!

#import "XMLLevelPiece.h"
#import "BlockType.h"
#import "GridCord.h"
#import "BlockColor.h"

@implementation XMLLevelPiece

BlockTypeEnum mBlockType;
BlockColorEnum mBlockColor;
int mRotation;
GridCord* mGridCords;
BlockColorEnum mLeftColor;
BlockColorEnum mTopColor;
BlockColorEnum mRightColor;
Boolean mRotatable;
Boolean mMoveable;
int mGroupID;

-(id) init
{
    if( (self=[super init])) {

    }
    return self;
}

-(void)initPiece:(BlockTypeEnum)pBlockType pBlockColor:(BlockColorEnum)pBlockColor pRotation:(int)pRotation pGridCords:(GridCord*)pGridCords pLeftColor:(BlockColorEnum) pLeftColor pTopColor:(BlockColorEnum) pTopColor pRightColor:(BlockColorEnum) pRightColor pRotatable:(Boolean) pRotatable pMoveable:(Boolean) pMoveable pGroupID:(int) pGroupID
{
    mBlockType = pBlockType;
    mBlockColor = pBlockColor;
    mRotation = pRotation;
    mGridCords = pGridCords;
    mLeftColor = pLeftColor;
    mTopColor = pTopColor;
    mRightColor = pRightColor;
    mRotatable = pRotatable;
    mMoveable = pMoveable;
    mGroupID = pGroupID;
}

-(void)initPiece2
{
    NSLog(@"poo");
}

-(Boolean)getRotatable
{
    return mRotatable;
}

-(Boolean)getMoveable
{
    return mMoveable;
}

-(int) getGroupID
{
    return mGroupID;
}

-(BlockColorEnum) getLeftColor
{
    return mLeftColor;
}

-(BlockColorEnum) getTopColor
{
    return mTopColor;
}

-(BlockColorEnum) getRightColor
{
    return mRightColor;
}

-(BlockTypeEnum) getBlockType
{
    return mBlockType;
}

-(BlockColorEnum) getBlockColor
{
    return mBlockColor;
}

-(int) getRotation
{
    return mRotation;
}

-(id) getGridCords
{
    return mGridCords;
}

-(void) setRotatable:(Boolean) pRotatable 
{
    mRotatable = pRotatable;
}

-(void) setMoveable:(Boolean) pMoveable 
{
    mMoveable = pMoveable;
}

@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-05-28T00:45:39+00:00Added an answer on May 28, 2026 at 12:45 am

    TL;DR

    The answer is – that is not how you define ivars in Objective-C. I’m not even sure how that is supposed to behave but I can reproduce the error if I code it the same as you have.

    I’d be interested for someone with more knowledge to explain what the behaviour/scope of those variable should be when they are defined like you have.


    There’s a lot of flaws in that code

    You have not actually shown an initPiece:.

    The long init... with all the arguments is likely a bad idea. Generally only add things to an init as either a convenience or if the object simply cannot function without it.

    The use of get is not really correct in Objective-C

    The class should potentially be defined more like

    XMLLevelPiece.h

    // You will need to import the header with the BlockTypeEnum defined
    
    @interface XMLLevelPiece : NSObject
    
    @property (nonatomic, assign) BlockTypeEnum blockType;
    // .. Other properties
    
    - (id)initWithPiece:(BlockTypeEnum)blockType; // I'm not so sure you need this
    
    @end
    

    XMLLevelPiece.m

    #import "XMLLevelPiece.h"
    #import "BlockType.h"
    #import "GridCord.h"
    #import "BlockColor.h"
    
    @implementation XMLLevelPiece
    
    @synthesize blockType = mBlockType;
    
    - (id)initWithPiece:(BlockTypeEnum)blockType;
    {
        self = [super init];
        if (self) {
            mBlockType = blockType;
        }
        return self;
    }
    
    @end
    

    Then you can use it like

    BlockTypeEnum blockType = STRAIGHT;
    
    XMLLevelPiece *p1 = [[XMLLevelPiece alloc] initWithPiece:blockType];
    
    blockType = NINETYBEND_FAST;
    
    XMLLevelPiece *p2 = [[XMLLevelPiece alloc] initWithPiece:blockType];
    
    NSLog(@"%d", p1.blockType);
    NSLog(@"%d", p2.blockType);
    

    Which for me results in:

    2012-01-08 15:29:31.782 Untitled[1297:707] 3
    2012-01-08 15:29:31.791 Untitled[1297:707] 1
    

    Optional observations

    If you can do away with the dedicated initializer, the usage would look more like:

    BlockTypeEnum blockType = STRAIGHT;
    
    XMLLevelPiece *p1 = [[XMLLevelPiece alloc] init];
    p1.blockType = blockType;
    // all other assignments
    
    blockType = NINETYBEND_FAST;
    
    XMLLevelPiece *p2 = [[XMLLevelPiece alloc] init];
    p2.blockType = blockType;
    // all other assignments
    
    NSLog(@"%d", p1.blockType);
    NSLog(@"%d", p2.blockType);
    

    To remove a couple of superflous lines you could remove the local blockType variable and assign the value straight to the object:

    XMLLevelPiece *p1 = [[XMLLevelPiece alloc] init];
    p1.blockType = STRAIGHT;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched but can't find why this simple code will fail in Drools
I have searched elsewhere and can-not find this information anywhere. I have installed Django
I have searched quite thoroughly on this but can't seem to find an answer.
I have searched for this, and I can't find any documentation about doing these
I have searched about this but can't seem to find exactly how to do
I have searched this site and also googled but can't seem to find the
I have searched around for an answer to this but can't find one. When
I have searched this online, but I can't find the answer I am looking
I have searched everywhere and can't find anything about this: I have a checkbox
I have searched for this and can't believe I can't find it. Perhaps I've

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.