“Expression Result Unused” in UIDocumentInteractionController

半城伤御伤魂 提交于 2019-12-14 02:45:03

问题


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:


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.



来源:https://stackoverflow.com/questions/9623683/expression-result-unused-in-uidocumentinteractioncontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!