cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Tell us what you want to see on the Community here!
Close

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Capturing serverModified into a variable in Swift

Highlighted

Capturing serverModified into a variable in Swift

Helpful | 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
Highlighted

Re: Capturing serverModified into a variable in Swift

Helpful | Level 6

Thanks, Greg!  So much cleaner!

View solution in original post

2 Replies 2
Highlighted

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! 

Highlighted

Re: Capturing serverModified into a variable in Swift

Helpful | Level 6

Thanks, Greg!  So much cleaner!

View solution in original post

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?