cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements
Highlighted
Level 6

Capturing serverModified into a variable in Swift

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 best answer

Accepted Solutions
Level 6

Re: Capturing serverModified into a variable in Swift

Thanks, Greg!  So much cleaner!

Reply
Loading...
2 Replies
Dropboxer

Re: Capturing serverModified into a variable in Swift

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...
Level 6

Re: Capturing serverModified into a variable in Swift

Thanks, Greg!  So much cleaner!

Reply
Loading...