Shairjeel ahmed
Shairjeel ahmed

Reputation: 51

how to convert type char to type id

actually I have to store three character in NSArray

` char plus = '+'; char minus = '-'; char multiply = '*';

NSArray *operator = [NSArray arraywithobjects : plus , minus ,multiply,nil];`

obviously before I store three characters in NSarray I have these character to convert to id. so how to convert char to id.or guide me to other better or efficient technique so save three characters.

Upvotes: 1

Views: 1605

Answers (2)

        // As we can not add primitive to NSArray ,
        // First we should convert it to NSString and then add it to NSArray

        //'c' character string
        char plus = '+';

        char minus = '-';

        char multiply = '*';


        //Converted to NSString object which was inherited from NSObject
        NSString *plusObject = [NSString stringWithFormat:@"%c", plus];


        NSString *minusObject = [NSString stringWithFormat:@"%c", minus];


        NSString *multiplyObject = [NSString stringWithFormat:@"%c", multiply];


        //Add collection of NSString objects to NSArray
        NSArray *operator = [NSArray 
                 arrayWithObjects:plusObject , minusObject ,multiplyObject,nil];

Upvotes: 2

Kirsteins
Kirsteins

Reputation: 27345

Wrap characters using NSNumber, for example, @(plus) and add it to operator NSArray. Get it back to char with [(NSNumber *)operator[index] charValue]

Upvotes: 2

Related Questions