cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Whether you are transferring a single drive, a team or an entire organization, Movebot's cloud migration tool has been built to make your Dropbox migration simple - learn all about it here.

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

Capturing serverModified into a variable in Swift

RJKinsman
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

Re: Capturing serverModified into a variable in Swift

RJKinsman
Helpful | Level 6

Thanks, Greg!  So much cleaner!

View solution in original post

2 Replies 2

Re: Capturing serverModified into a variable in Swift

Greg-DB
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! 

Re: Capturing serverModified into a variable in Swift

RJKinsman
Helpful | Level 6

Thanks, Greg!  So much cleaner!

Who's talking

Top contributors to this post

  • User avatar
    RJKinsman Helpful | Level 6
  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?