“Expression Result Unused” in UIDocumentInteractio

2019-08-09 03:17发布

Be gentle! I only have a vague understanding of what I am doing.

I'm trying to set the Name property of UIDocumentInteractionController with hopes that it will change the file name before it is sent to another application. I'm using the following to accomplish this:

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];

I am getting an "Expression Result Unused" error on this line:

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

I'm losing my mind trying to figure this one out. Any assistance is appreciated.

1条回答
干净又极端
2楼-- · 2019-08-09 03:29

Unfortunately you can't create a string like this:

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

@"%@" is a literal NSString, but the compiler won't do any formatting/replacement for you. You must explicitly make a call to one of the string constructor methods:

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

In this case, though, since soundFileName is itself an NSString, all you have to do is assign:

documentController.name = soundFileName;

The warning you're getting is the compiler saying to you that the bit after the comma (where you refer to soundFileName) is being evaluated and then discarded, and is that really what you meant to do?

In C, and therefore ObjC, the comma is an operator that can separate statements; each is evaluated separately. So this line where you're getting the warning could be re-written:

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

As you can see, the second line does nothing at all.

查看更多
登录 后发表回答