0

Attempting to get an enumerated value by an index from a NSMutableArray property that is filled with enumerated values.

all I wanted is to set a dynamic property and get set back within class instance.

thus, I coded a clear example that expose of problem.

Problem is this. WriteByType method in example never gets correct value. it should log "You Get Right Arrow" but it doesn't

//----------------------------------------------------------
//  CCMyClass.h
#import <Foundation/Foundation.h>

typedef enum {
    aLeft,
    aRight
} Arrow;

@interface CCMyClass : NSObject
{
    NSMutableArray *_arrowsPkg;
}
@property(nonatomic,copy) NSMutableArray *arrowsPkg;
@property (nonatomic)int nextPkgIndex;
-(void)GetItemOne;
@end
//-------------------------------------------------------------
//  CCMyClass.m
#import "CCMyClass.h"

@implementation CCMyClass
@synthesize arrowsPkg=_arrowsPkg;

-(void)WriteByType:(Arrow)AArrow
{
switch (AArrow) {
    case aLeft:
        NSLog(@"You Get Left Arrow");
        break;
    case aRight:
        NSLog(@"You Get Right Arrow");
        break;
    }
}

-(void)GetItemOne
{
    Arrow a =(Arrow)[[self arrowsPkg]objectAtIndex:1];
    [self WriteByType:a];

}
@end
//---------------USAGE EXAMPLE----------------------------------------------
//  CCAppDelegate.m
- (IBAction)btn1Clicked:(id)sender {

    CCMyClass *c1=[[CCMyClass alloc]init];
    NSNumber *v1=[NSNumber numberWithInt:aLeft];
    NSNumber *v2=[NSNumber numberWithInt:aRight];

    NSMutableArray *arr1=[NSMutableArray arrayWithObjects:v1,v2, nil];
    [c1 setArrowsPkg:arr1];
    [c1 GetItemOne];
}
Matthew
  • 9,851
  • 4
  • 46
  • 77
Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47

2 Answers2

0

[[self arrowsPkg]objectAtIndex:1] is an NSNumber, not the member of the Arrow enum. Just like during the initialization of the NSNumber objects, you should convert betweem the object and its enclosed value by calling intValue on it.

(And really, re-read a C and Objective-C tutorial. This is trivial.)

  • thanks for quick response. wantto get the value in instance objects array property by an index. ie 0 meant the instance object's ArrowsPkg[0]. so it is aLeft.and it should log "You Get Right Arrow" but it doesn't. – Zen Of Kursat Feb 02 '13 at 23:10
  • @N.Ramos And what prvents you from doing that? –  Feb 02 '13 at 23:10
  • wantto get the value in instance object's array property by an index. ie 0 meant the instance object's ArrowsPkg[0]. so it is aLeft.and it should log "You Get Left Arrow" but it doesn't. – Zen Of Kursat Feb 02 '13 at 23:16
  • @N.Ramos After trying my suggestion, it will. –  Feb 02 '13 at 23:19
  • so, how can I fill with enumerated values instead of pure integer values? would you mind suggesting me a source or book with a page that explains "how to fill NSMutableArray With enumerated values instead of pure integers or NSIntegers" ? all I know is assigned enum values are just integers in real. – Zen Of Kursat Feb 02 '13 at 23:24
  • @N.Ramos Since "enum values are just integers in real", you can treat them as every other `int`. `[NSNumber numberWithInt:aRight]` is just fine. –  Feb 02 '13 at 23:31
0

Replace

Arrow a = (Arrow)[[self arrowsPkg] objectAtIndex:1];

with

Arrow a = (Arrow)[[[self arrowsPkg] objectAtIndex:1] intValue];

or, more clearly written

NSNumber *arrowNumber = [[self arrowsPkg] objectAtIndex:1];
Arrow a = [arrowNumber intValue];

You are storing your Arrow enum types in NSNumber in btn1Clicked:, so you need to unbox these values in GetItemOne from NSNumber * types to the earlier int values.

mopsled
  • 8,445
  • 1
  • 38
  • 40
  • thanks ! may be I need to change the Question. how can I store enum values ? ie.like this: NSMutableArray *arr1=[NSMutableArray arrayWithObjects:aLeft,aLeft,aLeft,aRight,aLeft,aRight, nil]; Some says: "the right way to do it is not to make the mutable array a property" what do you think about this title ?[link](http://stackoverflow.com/questions/816720/whats-the-best-way-to-use-obj-c-2-0-properties-with-mutable-objects-such-as-ns) – Zen Of Kursat Feb 02 '13 at 23:49
  • If you're using a recent version of Xcode (and LLVM), you should be able to use the new Objective-C NSNumber boxing syntax, which can be used like this: `[NSMutableArray arrayWithObjects:@(aLeft),@(aRight),nil];` – mopsled Feb 02 '13 at 23:53
  • finally, I failed to use this style. WriteByType method's AArrow parameter value always gets weird values. I wont use a NSMutableArray as a Property especially with enum values :) I now prefer as ivar and some getset methods for it manually. – Zen Of Kursat Feb 03 '13 at 00:25
  • Here is a solid solution Reporting here. this may save someone's a day in life. -(void)GetItemOne { Arrow a =[[[self arrowsPkg]objectAtIndex:1] intValue]; [self WriteByType:a]; } intValue was the keyword.thanks. – Zen Of Kursat Feb 03 '13 at 00:47