I'm trying to find the latest text file in a folder so I can open it. So far I have:
let logFolder = URL(fileURLWithPath: "/Users/me/Library/Logs" )
let fm = FileManager.default
var files = try fm.contentsOfDirectory(at: logFolder, includingPropertiesForKeys: [.creationDateKey], options: [])
let txtFilePaths = files.filter{$0.pathExtension == "txt"}
But then I get stuck. I know I can get the date for a file with txtFilePath[x].creationDate
Seems like there should be a simple way of doing this but I'm a newbie and struggling to find any web resources for Swift 5.
Cheers
Such information about files is stored in something that is usually referred to as metadata.
I believe you are looking for something like NSMetadataItem.
By following the steps presented in this response, you should be able to have access to a field called kMDItemContentModificationDate. This should help you achieve your goal.
Related
I am developing an audio player using flutter.
In this project, I am using on_audio_query package to list all audio files from my mobile, then select one to play it.
I want to know, is there anyway to rename a file or delete it?
So the user can select one file and rename it, or select one or multiple files and delete them.
Thanks in advance
Try this example:
Future changeFileNameOnly(File file, String newFileName) {
var path = file.path;
var lastSeparator = path.lastIndexOf(Platform.pathSeparator);
var newPath = path.substring(0, lastSeparator + 1) + newFileName;
return file.rename(newPath);
}
Use permission_handler to get access to manage_external_storage before..
Don't forget to add it in manifest
You can also use the shared storage to get access..
By using i think saf package
I'm working on an app that uses Google Sheets as a database, but I can't figure out how to get Swift to read from google sheets. I've looked through the API website and a few questions on here but I need some help just getting started. So far I have;
private let scopes = [kGTLRAuthScopeSheetsSpreadsheets]
private let service = GTLRSheetsService()
var range = "Form Responses 1!A1:D3"
let spreadsheetId = "p;;;;;;p;;;;;;;;1LqXa6v75JE8RQQDOI4Z_g8mUT8x0DhsEDwRIaxDN-DU" // Portfolio
let query = GTLRSheetsQuery_SpreadsheetsValuesGet.query(withSpreadsheetId: spreadsheetId, range:range)
service.executeQuery(query, delegate: self, didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)))
Which I got from another question on here...
But 'displayResultWithTicket' is an unresolved identifier. Currently it gets to the last line, and debugging I'm not even sure what to do with the 'query' value. I'm really not even sure if it's reading the correct spreadsheet. How can I tell?
Long story short, I'm lost.
I recently made an app that uses Google Sheets. But I used the CSV file for the database. This might help. If you want, I can share the details.
First you need to export the Google Sheets file in CSV format. I used Realm to read CSV. Using Realm Studio, you need to create the columns as in the CSV file. Then you can import the CSV file into Realm Studio. Your database is ready.
In didFinishLaunchingWithOptions to find the file created after installing Realm on your project
print(Realm.Configuration.defaultConfiguration.fileURL!.path)
Now we will use the database with Swift.
For this, we must first add the Realm file that we prepared to the XCode project directory. Then we use it in ViewController this way to get data.
var realm : Realm!
For get the data in default.realm file
let realmPath = Bundle.main.url(forResource: "default", withExtension: "realm")!
let realmConfiguration = Realm.Configuration(fileURL: realmPath, readOnly: true)
do{
realm = try Realm(configuration: realmConfiguration)
}
catch {
print("error \(error.localizedDescription)")
}
You can export the result to an array. I hope it helps.
How can the original target path be programatically retrieved when the alias fails to resolve?
do {
let resolutionOptions: URL.BookmarkResolutionOptions = [
.withoutUI, .withoutMounting
]
let _ = try URL(resolvingAliasFileAt: fileURL, options: resolutionOptions)
}
catch _ {
// since non-resolvable, then retrieve & print original target string
}
The existing StackOverflow question "Getting alias path of file in swift" does not cover original target path retrieval for the situation of a non-resolvable alias.
The information would seem to be available somehow because the Finder GUI Get Info will still show the Original: /Some/Path even if the original is not found or available.
Also, mdls metadata listing did not provide the original target path.
I think you can load the bookmark data using URL.bookmarkData(withContentsOf:), then use resourceValues(forKeys:fromBookmarkData:) with [.pathKey] as the keys. Then, query the path of the returned URLResourcesKey object.
I tried to change client page size in Azure server
it's default is 50 and I want to make it bigger
so i use Microsoft tutorial in this link
https://learn.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-ios-how-to-use-client-library#querying
var client : MSClient?
let client = MSClient(applicationURLString: "AppUrl")
let table = client.tableWithName("TodoItem")
let query = table.query()
let pullSettings = MSPullSettings(pageSize: 3000)
but when I write
table.pullWithQuery(query, queryId:nil, settings: pullSettings) { (error) in
if let err = error {
print("ERROR ", err)
}
}
there are error "Value of type 'MSTable?' has no member 'pullWithQuery'"
what is the problem ?
is the function name changed ?
Two problems:
The documentation has not been updated for current versions of Swift
(an update request has been filed). The correct function name in modern Swift is pull rather than pullWithQuery.
The pullWithQuery function is on MSSyncTable, not MSTable. Pull is part of the offline sync system. The MSTable analog is read.
More details:
The SDK itself defines the function as MSSyncTable.pullWithQuery, but one of the features of Swift 3.0 is that it renames Objective C methods when it projects them into Swift to remove redundant arguments from the name, so verbWithX(X) becomes just verb(with:x) and pullWithQuery (MSQuery) becomes pull(with:MSQuery).
For more information on Swift 3 changes please see https://swift.org/blog/swift-3-0-released/ . I believe this particular change is SE-0005: Better Translation of Objective-C APIs Into Swift
If you download the Swift quickstart from your Azure Portal then you’ll get the correct modern pattern there:
self.table!.pull(with: self.table?.query(), queryId: "AllRecords")
or with your arguments:
self.table!.pull(with: self.table?.query(), queryId: nil, settings: pullSettings)
I am trying to write a unit tests that uses a file in my project. In my application, I use FileManager.default.currentDirectoryPath to get the path to my file. However, the test path is different when I use the same in tesing. After reading up the documentation I understood that XCode uses a different path for unit testing.
I was wondering how I can get the correct Bundle path for File.txt in my tests
I have tried this but it returns null
let testBundle = Bundle(for: type(of: self))
let fileURL = testBundle.url(forResource: "File", withExtension: "txt")
The following is my directory structure. ShopTests is my unit test file and File.txt is the file for which I am trying to retrieve the path url for(both highlighted in picture)
Here is how I am doing it in a test that works:
let dataURL = Bundle(for: TripBuilderTest.self).url(forResource: fileName, withExtension: "json")
I think I remember having had the same problem you are reporting. Try using classname.self as per above.