2014-11-04 127 views
1

我想在Swift中打印。我觀看了2014年WWDC採用AirPrint視頻,這在Obj-C中非常簡單,但我真的很想在Swift中完成這項工作。該文檔表明您必須使用字典創建UIPrintInfo對象。沒關係,但你如何創建UIPrintInfo需要的字典?具體來說,你使用什麼鍵?我已經看過文檔,但我不清楚哪些鍵需要。字典在Swift中創建UIPrintInfo對象需要什麼鍵?

回答

3

可以使用字典來創建UIPrintInfo實例,但常常會使這個很容易被忽略。更容易的是用nil字典創建一個對象,然後設置屬性。一旦完成,您可以檢索並檢查字典表示。

let printInfo = UIPrintInfo(dictionary: nil) 
printInfo.printerID = savedPrinterID 
printInfo.jobName = "My Print Job" 
printInfo.duplex = UIPrintInfoDuplex.ShortEdge 
printInfo.orientation = UIPrintInfoOrientation.Landscape 
printInfo.outputType = UIPrintInfoOutputType.Photo 

if let infoDict = printInfo.dictionaryRepresentation() { 
    println(infoDict) 
    // [UIPrintInfoOrientationKey: 1, UIPrintInfoJobNameKey: My Print Job, 
    // UIPrintInfoPrinterIDKey: ABC123, UIPrintInfoOutputTypeKey: 1, 
    // UIPrintInfoDuplexKey: 2] 
    let anotherPrintInfo = UIPrintInfo(dictionary: infoDict) 
} 
+0

謝謝,Nate。我很感激幫助。在你回答時,我也在回答我自己的問題。我們以相同的觀點,以一種不同的方式。我沒有想過要把字典傳給零。 – docwelch 2014-11-04 01:13:11

+0

是的,奇怪的是,你不能用'UIPrintInfo()'初始化。 – 2014-11-04 01:16:27

1

考慮這件事後,我找到了答案通過創建一個OBJ-C項目,創建UIPrintInfo對象作爲示例項目呢,然後UIPrintInfo對象上調用dictionaryRepresentation(返回我們需要的詞典) 。

UIPrintInfo *printInfo=[UIPrintInfo printInfo]; 
printInfo.outputType=UIPrintInfoOutputGrayscale; 
[email protected]"Print Job 1"; 
printInfo.orientation=UIPrintInfoOrientationLandscape; 
[email protected]"Printer ID Here"; 
NSDictionary *dict=[printInfo dictionaryRepresentation]; 

因此,這裏的關鍵是:

UIPrintInfoDuplexKey

UIPrintInfoOrientationKey

UIPrintInfoOutputTypeKey

UIPrintInfoPrinterIDKey

UIPrintInfoJobNameKey

前三個鍵具有來自文檔中列出的枚舉的int值。最後兩個鍵具有您自己選擇的字符串值。

希望這可以幫助別人。

相關問題