Connect a SQLite Database in Swift - swift

I have a problem when I try to create the path to my database in my iOS Application. I am a beginner in SQLite so I prefer ask here to have an answer.
I use this gitHub source of SQLite 3 for make and use a database in Swift, a GitHub source: https://github.com/stephencelis/SQLite.swift and I have an error which appears: "Missing argument label 'coder:' in call".
Then I fix this error by putting coder: before my path and another error appears: "Cannot convert value of type 'String' to expected argument 'NSCoder'.
static let path = NSSearchPathForDirectoriesInDomains( .documentDirectory, .userDomainMask, true).first!
static let db = try Connection(coder: "\(path)/data.sqlite3")

The problem was solve with delete and put sqlite again.
Thanks for the help.

Related

Definition conflicts with previous value - StorageGetDownloadURLTask.swift

I am getting an error in Xcode when trying to build an app.
In Firebase Storage-StorageGetDownloadURLTask.swift.
Firebase Storage: 11.0.7
Flutter: 3.0.5
Xcode 12
In a few lines it presents the information:
"Definition conflicts with previous value.
Value of type 'Any?' has no member 'count'.
Value of type 'Any?' has no member 'components'"
I would like to understand why I am getting this error and how to fix it.
I already updated Firebase Storage version from 11.0.1 to 11.0.7.
I removed the podfile.lock.
I removed the Pods.
Flutter clean command, then flutter pub get.
Pod install, pod deintegrate, pod update.
And I wasn't successful.
I'm trying to update the version of my app from flutter 2.2.3 to flutter 3.0.5, I managed to run it on Android, but in Xcode it's showing this error.
The line downloadTokens = dictionary["downloadTokens"] is declaring a constant named downloadTokens with a type of Any?.
The line guard let downloadTokens = downloadTokens as? String is trying to declare a constant named downloadTokens with a type of String.
So you are trying to create two separate constants with the same name but different types, hence the error message of "Definition conflicts with previous value".
Change your code to:
guard let downloadTokens = dictionary["downloadTokens"] as? String,
downloadTokens.count > 0 else {
This combines your two lines into one creating a single constant named downloadTokens with a type of String.
This fix will resolve the other two errors since downloadTokens is not correctly of type String instead of Any?.

Find latest modified file in Folder

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.

How to get original path when alias fails to resolve

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.

Value of type 'MSTable?' has no member 'pullWithQuery'

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)

What is the appropriateFor parameter for in FileManager.url(for:in:appropriateFor:create:)?

Apple's instructions for creating a temporary URL are to use FileManager.url(for:in:appropriateFor:create:).
The example they give is (rewritten in Swift 3):
let desktopURL = URL(fileURLWithPath: "/Users/Noah/Desktop/")
do {
let temporaryDirectoryURL = try FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: desktopURL, create: true)
} catch {
// handle error
}
The docs say that the appropriateFor parameter "determines the volume of the returned URL", but I don't understand what that means. What is this parameter for and how should I determine the URL to pass in for it?
The URL that you pass in is used to determine on which Volume (on which mounted disk) the temporary directory will be created. I suspect you should pass a URL to a file or folder that would reside on the same volume.
I Did an experiment to understand it.
let document = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
try? FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: document, create: true)
A directory appears in sandbox of my app:
So I think appropriateFor is used to determine where a temporary directory will be created
I came across this today, years later, but the docs have are clear on this now. I don't remember that they were before, but here's your answer direct from the documentation:
The file URL used to determine the location of the returned URL.
Only the volume of this parameter is used. This parameter is ignored
unless the directory parameter contains the value
FileManager.SearchPathDirectory.itemReplacementDirectory and the
domain parameter contains the value userDomainMask.