2012-03-08 81 views
1

請溫柔!我只對我正在做的事情有一個模糊的理解。UIDocumentInteractionController中的「表達結果未使用」

我試圖設置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; 

我失去了我的腦海裏想了明白這一個。任何援助表示讚賞。

+1

刪除@「%@」, – 2012-03-08 19:42:44

回答

1

可惜你不能這樣創建一個字符串:

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

@"%@"是文字NSString,但是編譯器不會爲你做任何格式/更換。你必須明確地撥打電話到的字符串構造方法之一:

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

在這種情況下,雖然,因爲soundFileName本身就是一個NSString,所有你需要做的就是分配:

documentController.name = soundFileName; 

的你得到的警告是編譯器告訴你,逗號後面的位(你指的是soundFileName)正在被評估並被丟棄,這真的是你的意思嗎?

在C中,因此在ObjC中,逗號是一個可以分隔語句的運算符;每個都分開評估。因此,您得到警告的這條線路可能會被重寫:

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

正如您所看到的,第二行完全不起作用。

+0

感謝您提供豐富的答案!不幸的是,被髮送到其他應用程序的文件的名稱沒有被更改,但是。除非有明顯的事情讓你感到震驚,否則我會在做一些四處搜尋之後另存一個問題。 – user1257826 2012-03-08 22:25:27

+0

檢查'soundFileName'和其他使用'NSLog'創建的變量:'NSLog(@「%@,%@,%@」,suffixName,currentNote.soundFile,soundFileName);'確保它們是你期望他們是什麼。 – 2012-03-09 06:56:40

+0

我試圖做同樣的 - 名稱不會改變。我可以改變UTI而不是名字;( – slott 2012-09-04 07:09:25

相關問題