0

我正在尝试根据开关创建 NSData,但在将选项添加到方法时遇到问题appendBytes。添加 NSStringtest给我一个错误。

例子:

- (void)selectCenterJustification:(int)option
{
    NSMutableData *data;
    NSString *test;
    switch (option)
    {
        case 0:
            test = @"\x0";
            break;
        case 1:
            test = @"\x1";
            break;
        case 2:
            test = @"\x2";
            break;
    }
    // does not work because of "test"
    [data appendBytes:"\x1b" "a", test length:3];

    // working
    [data appendBytes:"\x1b" "a" "\x1" length:3];
}

知道我该怎么做吗?

4

1 回答 1

3
NSMutableData *data = [NSmutableData data];
NSString *test = nil;

switch (option) {
    case 0:
        test = @"\x0";
        break;
    case 1:
        test = @"\x1";
        break;
    case 2:
        test = @"\x2";
        break;
    default:
        NSLog(@"[justification]: unknown option");
        break;
}
if (test) {
    [data appendBytes:"\x1b" "a" length:2];
    [data appendBytes:[test cStringUsingEncoding:NSASCIIStringEncoding] length:1];
}

更新

看来您需要C字符串。

于 2013-08-14T12:54:45.783 回答