1

要温柔!我对自己在做什么只有一个模糊的理解。

我正在尝试设置 UIDocumentInteractionController 的 Name 属性,希望它能在将文件名发送到另一个应用程序之前更改文件名。我正在使用以下内容来完成此操作:

UIDocumentInteractionController *documentController;
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSURL *soundFileURL = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:
                                                  [NSString stringWithFormat: @"%@/%@", kDocumentNotesDirectory, currentNote.soundFile]]];  

    NSString *suffixName = @"";
    if (self.mediaGroup.title.length > 10) {
        suffixName = [self.mediaGroup.title substringToIndex:10];
    }
    else {
        suffixName = self.mediaGroup.title;
    }
    NSString *soundFileName = [NSString stringWithFormat:@"%@-%@", suffixName, currentNote.soundFile];

    documentController = [UIDocumentInteractionController interactionControllerWithURL:(soundFileURL)];
    documentController.delegate = self;
    [documentController retain];
    documentController.UTI = @"com.microsoft.waveform-​audio";
    documentController.name = @"%@", soundFileName; //Expression Result Unused error here
    [documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

我在这一行收到“未使用的表达式结果”错误:

documentController.name = @"%@", soundFileName;

我正在失去理智试图弄清楚这一点。任何帮助表示赞赏。

4

1 回答 1

1

不幸的是,您不能创建这样的字符串:

documentController.name = @"%@", soundFileName;

@"%@"是文字NSString,但编译器不会为您进行任何格式化/替换。您必须显式调用字符串构造方法之一:

documentController.name = [NSString stringWithFormat:@"%@", soundFileName];

但是,在这种情况下,因为soundFileNameis 本身是 a NSString,所以您所要做的就是分配:

documentController.name = soundFileName;

你得到的警告是编译器告诉你逗号后面的位(你指的是soundFileName)正在被评估然后被丢弃,这真的是你的意思吗?

在 C 和 ObjC 中,逗号是可以分隔语句的运算符;每个都单独评估。因此,您收到警告的这一行可以重写:

documentController.name = @"%@";
soundFileName;

如您所见,第二行根本什么都不做。

于 2012-03-08T20:15:47.507 回答