如何将char类型转换为id类型

时间:2013-08-16 12:26:24

标签: objective-c

实际上我必须在NSArray中存储三个字符

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

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

显然在我在NSarray中存储三个字符之前,我将这些字符转换为id。   所以如何将char转换为id.or引导我到其他更好或更有效的技术,所以保存三个字符。

2 个答案:

答案 0 :(得分:2)

使用NSNumber包裹字符,例如@(plus)并将其添加到运算符NSArray。 使用[(NSNumber *)operator[index] charValue]

将其恢复为char

答案 1 :(得分: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];