3 Replies
      Latest reply on Jun 13, 2018 1:27 PM by dave256
      dave256 Level 1 Level 1 (0 points)

        My apologies if there is a separate forum for Xcode 10, but I didn't see it.

         

        I have the following code for a UIDocument subclass to generate a thumbail image for the UIDocumentBrowswerViewController to dispay.

         

            public override func fileAttributesToWrite(to url: URL, for saveOperation: UIDocument.SaveOperation) throws -> [AnyHashable : Any] {
        
                let slide = self.slideAtIndex(0)!
                let image = slide.image(self.imageSize, document: self)
        
                return [
                    URLResourceKey.hasHiddenExtensionKey: true,
                    URLResourceKey.thumbnailDictionaryKey: [
                        URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey: image
                    ]
                ]
            }

        Xcode10/Swift 4.2 has changed the signature to:

         

        publicoverridefunc fileAttributesToWrite(to url: URL, for saveOperation: UIDocument.SaveOperation) throws -> [FileAttributeKey : Any]

        It does not compile because the return dictionary key types do not match. There does not seem to be a FileAttributeKey for saving a thumbnail. Is this an oversight or is there a way around this? For now I've filed rdar://40808963

         

        Thanks,

        Dave

        • Re: UIDocument.fileAttributesToWrite signature changed in Xcode 10/Swift 4.2
          Claude31 Level 7 Level 7 (3,475 points)

          Here are all the thumbnail keys :

           

          Thumbnail Keys

           

          static let thumbnailKey: URLResourceKey

               All thumbnails as a single NSImage (read-write).

           

          static let thumbnailDictionaryKey: URLResourceKey

               A dictionary of NSImage/UIImage objects keyed by size (read-write).

               See URLThumbnailDictionaryItem for a list of possible keys.

                    static let NSThumbnail1024x1024SizeKey: URLThumbnailDictionaryItem

           

          struct URLThumbnailDictionaryItem

               Possible keys for the thumbnailDictionaryKey dictionary.

           

          So, the key exists.

           

          Did you try to covert to FileAttributeKey

          FileAttributeKey(URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey.rawValue)

           

          URLResourceKey.thumbnailDictionaryKey: [
                          FileAttributeKey(URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey.rawValue): image
                      ]

           

          Direct cast as FileAttributeKey does not work.

          • Re: UIDocument.fileAttributesToWrite signature changed in Xcode 10/Swift 4.2
            OOPer Level 7 Level 7 (3,880 points)

            The answer of Claud31 is very suggesting, but you may need something like this:

            return [
               FileAttributeKey(URLResourceKey.hasHiddenExtensionKey.rawValue): true,
               FileAttributeKey(URLResourceKey.thumbnailDictionaryKey.rawValue): [
                   URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey: image
                ]
            ]
            

            The outermost keys for the returned dictionary need to be of type `FileAttributeKey`.

            The type `FileAttributeKey` is a thin wrapper for `NSString` and you can initialize it with a String value.