我想在Swift中打印。我觀看了2014年WWDC採用AirPrint視頻,這在Obj-C中非常簡單,但我真的很想在Swift中完成這項工作。該文檔表明您必須使用字典創建UIPrintInfo對象。沒關係,但你如何創建UIPrintInfo需要的字典?具體來說,你使用什麼鍵?我已經看過文檔,但我不清楚哪些鍵需要。字典在Swift中創建UIPrintInfo對象需要什麼鍵?
1
A
回答
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)
}
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值。最後兩個鍵具有您自己選擇的字符串值。
希望這可以幫助別人。
相關問題
- 1. 在swift中創建對象和鍵數組字典
- 2. Azure WebJobs SDK - 在什麼情況下需要創建JobHost對象?
- 3. 爲什麼元素創建需要DOM中的文檔對象?
- 4. Make對象的字典只創建指定鍵的對象
- 5. 爲什麼要創建模擬對象?
- 6. 在Swift中創建循環字典
- 7. 在JavaScript中創建新對象時需要括號和新關鍵字嗎?
- 8. 爲什麼「case class」不需要「new」來創建新對象
- 9. 爲什麼我需要'新'來創建一個對象?
- 10. 爲什麼我們需要創建模擬對象?
- 11. Swift創建字典Int:Class_Type
- 12. 使用Firebase異步字典創建對象數組下載(Swift)
- 13. 爲什麼Math對象在javascript中不需要新的關鍵字
- 14. 爲什麼Generic.xaml中的合併字典需要絕對uri?
- 15. 爲什麼在VB6中創建新的Crystal Report對象需要很長時間?
- 16. Rails用param創建對象 - 不需要
- 17. C#對象創建需要時間
- 18. 在需要字符串的循環外部創建新對象
- 19. 當創建一個JavaScript全局對象,爲什麼我需要指定此關鍵字
- 20. 迭代字典對象時所通過的詞典:需要的建議
- 21. 爲什麼我們需要在創建對象時額外添加'get'和'set'?
- 22. Swift創建NSError對象
- 23. Swift - 創建參數對象
- 24. Swift:動態對象創建
- 25. 在Titanium + iPhone中創建字典(鍵值對)
- 26. 如何在Swift中排序字典鍵?
- 27. 爲什麼直接從用戶輸入創建字典時鍵/值對反轉
- 28. Symfony - 創建插件需要什麼?
- 29. MVC模式:需要先創建什麼?
- 30. 如何創建鍵,值字典在python
謝謝,Nate。我很感激幫助。在你回答時,我也在回答我自己的問題。我們以相同的觀點,以一種不同的方式。我沒有想過要把字典傳給零。 – docwelch 2014-11-04 01:13:11
是的,奇怪的是,你不能用'UIPrintInfo()'初始化。 – 2014-11-04 01:16:27