1

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

` 字符加 = '+'; 字符减 = '-'; 字符乘法 = '*';

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

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

4

2 回答 2

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];
于 2013-08-16T12:37:19.377 回答
2

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

于 2013-08-16T12:36:17.347 回答