Announcements
Known issues updated. Learn more
Highlighted

Capturing serverModified into a variable in Swift

Level 6

I'm new to Swift, and this is my first iOS application.  In fact, I'm fairly new to OO programming.  My background is mainframe z/OS.  So...

I needed to capture serverModified date in a field.  After much googling and trial and error, I finally made the code to parse it out of the MetaData.  It works, but is there a slicker way to do this? 

Thanks!

Here is the code I used to do it:

let dispatchGroup = DispatchGroup()
.
.
.
        
        // Check to see if Nvelopes.csv exists in Dropbox and get the servermodified date.

        dispatchGroup.enter()
        client!.files.getMetadata(path: "/Nvelopes.csv")
            .response {response, error in
                if let (metadataNvelopes) = response
                {
                    let nsstringMetadataNvelopes = NSString(string: metadataNvelopes.description)
                    var serverModifiedSeparator = "\"server_modified\" = \""
                    let serverModifiedArray = nsstringMetadataNvelopes.components(separatedBy: serverModifiedSeparator)
                    
                    if serverModifiedArray.count > 0 {
                        print("serverModifiedArray begin")
                        print(serverModifiedArray[0])
                        print(serverModifiedArray[1])
                        print("serverModifiedArray end")
                        serverModifiedSeparator = "\";"
                        let ServerModifiedArray = serverModifiedArray[1].components(separatedBy: serverModifiedSeparator)
                        let serverModified = ServerModifiedArray[0]
                        print("serverModified:" , serverModified)
                        
                    }

                    dispatchGroup.leave()

                } else if let error = error {
                    print("Did not find Nvelopes.csv in Dropbox:")
                    print(error.description)
                    dispatchGroup.leave()
                }
1 Accepted solution

Accepted Solutions

Re: Capturing serverModified into a variable in Swift

Level 6

Thanks, Greg!  So much cleaner!

Reply
Loading...
2 Replies

Re: Capturing serverModified into a variable in Swift

Dropboxer

Here's the recommended way to get the serverModified out of a Metadata object given by filesGetMetadata:

 

client!.files.getMetadata(path: path)
    .response {response, error in
        if let (metadata) = response {
            switch metadata {
                case let fileMetadata as Files.FileMetadata:
                    print("File metadata: \(fileMetadata)")
                    let serverModified = fileMetadata.serverModified
                    print("File serverModified: \(serverModified)")
                case let folderMetadata as Files.FolderMetadata:
                    print("Folder metadata: \(folderMetadata)")
                    // folders don't have serverModified
                case let deletedMetadata as Files.DeletedMetadata:
                    print("Deleted entity's metadata: \(deletedMetadata)")
                    // deleted entries don't have serverModified
                default:
                    print("default")
            }
        }
        else if let error = error {
            print(error)
        }
}

This method can return metadata for files, folders, or deleted items, so you need to check the type first, done here using the switch, since the latter two don't have serverModified.

Hope this helps! 

Reply
Loading...

Re: Capturing serverModified into a variable in Swift

Level 6

Thanks, Greg!  So much cleaner!

Reply
Loading...
API Support

Have a question? Our Dropbox Community is here to help you find an answer!
You won’t have to wait too long for a reply, but why not learn more about the Community while you’re here.

Not the right discussion for you? Find out what else our Community has to say about API Support.

Or, search our Help Center for even more.

Who's talking
Top contributors to this post