I have a function that queries an SQLite database and returns names, dates and values. In SQLite the dates are in Julian format. I convert the dates in the SQLite query to a date time string which give me "2022-08-01 00:00:00". I want to display this date as the date string "Aug 1, 2022". The only way I have been able to achieve this is to convert the first date string to a date via a date formatter then convert this date to the desired date string via a second date formatter. Is there a more direct way to do this?
func AccessSQLiteDB(db: OpaquePointer?) {
let queryTradingDaysStatement = """
WITH
TempTable1
AS
(
SELECT
max(TimeStamp) - 365.25 as StartingDate
FROM
TradingDays
WHERE
FundName = 'Fund1'
),
TempTable2
AS
(
SELECT
main.FundName,
datetime(main.TimeStamp) as SQLDateString,
main.Close
FROM
TradingDays main, TempTable1 as temp
WHERE
main.FundName = 'Fund1'
AND
main.TimeStamp >= temp.StartingDate
)
SELECT
FundName,
SQLDateString,
Close
FROM
TempTable2
ORDER By
SQLDateString ASC
;
"""
let sqlDateStringFormatter = DateFormatter()
sqlDateStringFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
sqlDateStringFormatter.timeZone = .gmt
var queryTradingDaysCPtr: OpaquePointer?
if sqlite3_prepare_v2(db, queryTradingDaysStatement, -1, &queryTradingDaysCPtr, nil) == SQLITE_OK {
while (sqlite3_step(queryTradingDaysCPtr) == SQLITE_ROW) {
let fundName = sqlite3_column_text(queryTradingDaysCPtr, 0)
let timeStamp = sqlite3_column_text(queryTradingDaysCPtr, 1)
let close = sqlite3_column_double(queryTradingDaysCPtr, 2)
let fundNameAsString = String(cString: fundName!)
let timeStampAsString = String(cString: timeStamp!)
print(timeStampAsString) // returns this format 2022-08-01 00:00:00
let timeStampAsDate: Date = sqlDateStringFormatter.date(from: timeStampAsString)!
print("\(timeStampAsDate)") // returns this format 2022-08-01 00:00:00 +0000
let mediumDataFormatter = DateFormatter()
mediumDataFormatter.dateStyle = .medium
mediumDataFormatter.timeZone = .gmt
let dateString = mediumDataFormatter.string(from: timeStampAsDate)
print(dateString) // returns this format Aug 1, 2022
let closeAsString = String(format: "$%.2f", close)
print(fundNameAsString + " - " + dateString + " - " + closeAsString)
} // end while loop
} else {
let errorMessage = String(cString: sqlite3_errmsg(db))
print("\nQuery is not prepared \(errorMessage)")
}
sqlite3_finalize(queryTradingDaysCPtr)
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I have a program that creates a custom table view. In the delegate that populates the cells with data I create an instance of a custom NSTableCellView. It works. What I would like to be able to do is pass in a value when making an instance so that I can set the width of the frame (var rectWidth in the custom NSTableCellView). I am currently using two different custom NSTableCellViews, each with a different value for var rectWidth. Below is my delegate and custom NSTableCellView code.
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if (tableColumn?.identifier)!.rawValue == "fund" {
let dataCell = CustomTableCellView()
dataCell.textField?.stringValue = closingValues[row].fundName
return dataCell
} else if (tableColumn?.identifier)!.rawValue == "date" {
let dataCell = CustomTableCellViewDate()
dataCell.textField?.stringValue = SQLDateFormatter.string(from: closingValues[row].timeStamp)
return dataCell
} else {
let dataCell = CustomTableCellView()
dataCell.textField?.stringValue = String(format: "$%.2f", closingValues[row].close)
return dataCell
}
}
class CustomTableCellView: NSTableCellView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
let rectWidth: CGFloat = 100
self.autoresizingMask = .width
let nsRectangle = NSMakeRect(0, 0, rectWidth, 24)
let customTextField: NSTextField = CustomTextField(frame: nsRectangle)
self.textField = customTextField
self.addSubview(customTextField)
}
required init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I have a swift program that displays a chart using Chart. The code includes an X Axis Scale parameter but for some reason the last value (right most) on the x axis does not display. It should display Aug 2023. In checking the array used for the x axis labels I find that the last value is Aug 2023. I do not know how to overcome this obstacle. Any assistance will be appreciated. Below is a picture of the bottom of the chart and the code.
struct CustomChartView: View {
let vm: SQLDataVM = SQLDataVM.shared
let closingValues: [TradingDay]
let fundName: String
let numYears: Int
let maxCloseStruct: TradingDay
let maxClose: Double
let minCloseStruct: TradingDay
let minClose: Double
let yIncrment: Double
let yAxisValues: [Double]
let minTimeStampStruct: TradingDay
let minTimeStamp: Date
var dateComponent = DateComponents()
let maxTimeStampStruct: TradingDay
let maxTimeStamp: Date
var xAxisValues: [Date] = []
init (fundName: String, numYears: Int) {
self.fundName = fundName
self.numYears = numYears
closingValues = self.vm.QueryDatabase(fundName: fundName, numYears: numYears)
maxCloseStruct = self.closingValues.max(by: { (tradingDay1, tradingDay2) -> Bool in
return tradingDay1.close < tradingDay2.close
})!
maxClose = maxCloseStruct.close
minCloseStruct = closingValues.min(by: { (tradingDay1, tradingDay2) -> Bool in
return tradingDay1.close < tradingDay2.close
})!
minClose = minCloseStruct.close
yIncrment = (maxClose - minClose)/4
yAxisValues = [
minClose,
minClose + (1 * yIncrment),
minClose + (2 * yIncrment),
minClose + (3 * yIncrment),
maxClose
]
minTimeStampStruct = closingValues.min(by: { (tradingDay1, tradingDay2) -> Bool in
return tradingDay1.timeStamp < tradingDay2.timeStamp
})!
minTimeStamp = minTimeStampStruct.timeStamp
maxTimeStampStruct = closingValues.max(by: { (tradingDay1, tradingDay2) -> Bool in
return tradingDay1.timeStamp < tradingDay2.timeStamp
})!
maxTimeStamp = maxTimeStampStruct.timeStamp
xAxisValues.append(minTimeStamp)
for i in 1...11 {
dateComponent.month = i
let nextMonth = Calendar.current.date(byAdding: dateComponent, to: minTimeStamp)
xAxisValues.append(nextMonth!)
}
xAxisValues.append(maxTimeStamp)
print("\(xAxisValues[12])") // prints 2023-08-04 00:00:00 +0000
} // end init
var body: some View {
HStack(alignment: .center) {
VStack(alignment: .center) {
Chart(closingValues, id:\.id) {
LineMark(
x: .value("Date", $0.timeStamp, unit: .day),
y: .value("Closing", $0.close)
)
.foregroundStyle(.blue)
} // end chart
.frame(width: 1000, height: 700, alignment: .center)
.chartXAxisLabel(position: .bottom, alignment: .center, spacing: 15) {
Text("Date")
.font(.custom("Arial", size: 20))
}
.chartYAxisLabel(position: .leading, alignment: .center, spacing: 20) {
Text("Closing Value")
.font(.custom("Arial", size: 20))
}
.chartXAxis {
AxisMarks(values: xAxisValues) { value in
if let date = value.as(Date.self) {
AxisValueLabel(horizontalSpacing: -14, verticalSpacing: 10) {
VStack(alignment: .leading) {
Text(ChartMonthFormatter.string(from: date))
.font(.custom("Arial", size: 14))
Text(ChartYearFormatter.string(from: date))
.font(.custom("Arial", size: 14))
} // end v stack
} // end axis label
} // end if statement
AxisGridLine(centered: true, stroke: StrokeStyle(lineWidth: 1))
.foregroundStyle(Color.black)
AxisTick(centered: true, length: 0, stroke: .none)
}
} // end chart x axis
.chartXScale(domain: [xAxisValues[0], xAxisValues[12]])
.chartYAxis {
AxisMarks(position: .leading, values: yAxisValues) { value in
AxisGridLine(centered: true, stroke: StrokeStyle(lineWidth: 1))
.foregroundStyle(Color.black)
AxisTick(centered: true, length: 0, stroke: .none)
AxisValueLabel(horizontalSpacing: 10) {
if let yAxisValue = value.as(Double.self) {
let stringValue = String(format: "$%.02f", yAxisValue)
Text(stringValue)
.font(.custom("Arial", size: 14))
}
}
}
}
.chartYScale(domain: [minClose, maxClose])
.chartPlotStyle { plotArea in
plotArea.background(.white.opacity(0.9))
.border(.black, width: 1)
} // end chart plot style
} // end v stack
.frame(width: 1200, height: 900, alignment: .center)
} // end h stack
} // end some view
}
Hello,
I have a Swift program. It utilizes the SwiftUI.
I want to place my frame/container in the center of the computer screen. Any advise on how to do so would be appreciated.
Below is what I have so far.
Regards,
Chris
import SwiftUI
@main
struct Test1App: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.frame(width: 500, height: 350)
.background(Color .blue)
.padding()
}
}
I have a working program utilizing the MVVM architecture. There are 3 view models and 3 models. In the first model called I do some testing and would like to pass the result of that testing, a boolean, to the other two models. I have tried to create a class with a boolean, create an instance in the content view and pass it into one of the view models. I would then pass it into the corresponding model. Unfortunately I have been unable to get it to work i.e. pass an instance of the class into the view model. If you see a fix to this approach or have a better approach please let me know. The failure message I get is related to attempting to pass csvData to vooVM. The message is => Cannot use instance member 'csvData' within property initializer; property initializers run before 'self' is available.
My Class that contains the boolean that would be set in the first model and passed to the two other models via their view models:
class CSVData: ObservableObject {
var updated: Bool = false
}
My Struct Content View initial section:
struct ContentView: View {
var csvData: CSVData
// The following line gives the above mentioned error message
@StateObject private var vooVM = VOOVM(csvData: csvData)
@StateObject private var vfiaxVM = VFIAXVM()
@StateObject private var prinVM = PrincipalVM()
@State private var selectedItemID: Int?
let bottomPadding: CGFloat = 2
init() {
self.csvData = CSVData()
}
var body: some View {
My View Model initial section:
class VOOVM: ObservableObject {
var ContainerValues1: [CommonAttributes] = []
var ContainerValues5: [CommonAttributes] = []
var MinAndMaxClose1: [String:Double] = [:]
var MinAndMaxClose5: [String:Double] = [:]
private var vooModel: VOOModel = VOOModel()
var symbol: String
var shares: Double
var csvData: CSVData
init(csvData: CSVData) {
self.csvData = csvData
During the upgrade to Ventura it appears that Xcode was also upgraded to 14.0.1. In an attempt to use a new feature, Charts, I entered into a macOS app import Charts and I get an error message no such module Charts. If I use the same import Charts in an iOS app I do not get such an error. I am wondering if Charts in not available for macOS apps or did something go wrong during the Ventura upgrade. If it is the later, it makes sense to me to cleanly uninstall Xcode and reinstall. But it is unclear if just dragging Xcode from the application folder to the trash and emptying the trash completely uninstalls Xcode. As a side note, I do not ever remember Xcode being upgraded during an upgrade to macOS. Any input will be appreciated.
I have a program that allows me to programmatically bounce a ball around a window. Everything works as desired. What I would like to be able to do is work with the window itself to do such things as remove the title, change the background color and make it non resizable. I think I do this by creating a class of type NSWindosController which I have done. I create an instance of it in the ContentView but it does not work. Is my class wrong or am I not creating an instance in the right place or is there some other problem. Thank you.
import Cocoa
class WindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
window?.styleMask.remove(.titled)
window?.styleMask.remove(.resizable)
window?.backgroundColor = .red
}
}
import SwiftUI
struct ColorShading: Identifiable {
let value: GraphicsContext.Shading
let id = UUID()
}
struct ContentView: View {
@State private var isPaused: Bool = true
@State private var ballPosition = BallPosition()
@State private var winController = WindowController()
var body: some View {
let ballColors = [
ColorShading(value: GraphicsContext.Shading.color(.orange)),
ColorShading(value: GraphicsContext.Shading.color(.red)),
ColorShading(value: GraphicsContext.Shading.color(.blue)),
ColorShading(value: GraphicsContext.Shading.color(.green)),
ColorShading(value: GraphicsContext.Shading.color(.gray)),
ColorShading(value: GraphicsContext.Shading.color(.yellow)),
ColorShading(value: GraphicsContext.Shading.color(.purple))
]
TimelineView(.animation(minimumInterval: 0.0001, paused: isPaused)) { timeline in
Canvas { context, size in
_ = timeline.date.timeIntervalSinceNow
let circleDiameter: Double = (size.width * 0.2)
let circleRadius: Double = circleDiameter / 2
if isPaused == true {
ballPosition.updateBaseData(maxX: size.width, maxY: size.height, circleRadius: circleRadius)
}
let wheelOrigin = CGPoint(x: ballPosition.xPosition, y: ballPosition.yPostion)
context.stroke(
Path { path in
path.addArc(center: wheelOrigin, radius: circleRadius, startAngle: .degrees(0), endAngle: .degrees(360), clockwise: false)
}, with: ballColors[ballPosition.randomColor].value, lineWidth: 2
) // end context stroke
ballPosition.updatePosition()
} // end canvas
.frame(width: 800, height: 800)
} // end time line view
.onAppear() {
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { timer in
isPaused = false
timer.invalidate()
} // end timer
} // end on appear
}
}
I would like to create a range of angles between 235 degrees and 35 degrees. This range would run 235 to 359 plus 0 to 35. I am using it in the line of code below but this of course does not work since the first integer is greater than the last integer in the range.
randomAngle = Int.random(235...35).
Is there a way to accomplish this?
I have a program that reads in CVS data and writes it to a core data persistent store. I would like to change the location of the persistent store. Per Apple docs it appears that I should be able to achieve this by overriding it in a sub class of NSPersistentContainer. Unfortunately I have been unable to figure how to do this. Below is my attempt.
The Xcode error I get is "cannot call value of non-function type URL".
final class CoreDataContainer: NSPersistentContainer {
let storePathURL: URL = URL(string: "file:///Users/Chris/Developer/Persistent Store")!
override class func defaultDirectoryURL() -> URL {
return super.defaultDirectoryURL()
.absoluteURL(storePathURL)
}
}
The below code works. I pass in an array of core data entities and display attributes in a table. In the second table column I use a switch case statement to handle the 3 possible attributes I want to display. I could eliminate the switch case if I could figure out how to access a given core data attribute via a variable that contains the specific attribute name but have been unable to determine how to do so. Two attempts are commented out in the code.
struct DataTable: View {
private var closingValues: Array<TradingDayPrices>
var heading: String
var attribute: String
init(closingValues: [TradingDayPrices], heading: String, attribute: String) {
self.closingValues = closingValues
self.heading = heading
self.attribute = attribute
}
var body: some View {
Text(heading)
.foregroundColor(.black)
.font(Font.custom("Arial", size: 18))
.bold()
.padding(.top, 10)
Table(self.closingValues) {
TableColumn("Date") { value in
HStack {
Spacer()
Text(dateToStringFormatter.string(from: value.timeStamp!))
Spacer()
}
}
.width(100)
TableColumn("Closing Value") { value in
HStack {
Spacer()
// Text(String(format: "$ %.2f", value(forKey: attribute)))
// Text(String(format: "$ %.2f", value(attribute)))
switch attribute {
case "s1Close":
Text(String(format: "$ %.2f", value.s1Close))
case "s2Close":
Text(String(format: "$ %.2f", value.s2Close))
default:
Text(String(format: "$ %.2f", value.s3Close))
}
Spacer()
}
}
.width(100)
}
.foregroundColor(.black)
.font(Font.custom("Arial", size: 16))
.frame(width: 250)
Spacer()
.frame(height: 30)
}
}
I have a program that reads in CSV data from a text file on disk. It converts it to an array. It then encodes this array into a JSON string. It all works. The problem I have is the during the encoding process a double like 402.71 is converted to 402.70999999. How do I control the encoding process so that this number is 402.71 in the JSON string. Below is a line of CSV data, a line of the resulting array element and the resulting JSON object, the encoding function and the structures used for creating the array from the CSV data.
CSV data:
07/07/23, 403.03, 406.679, 402.71, 402.89, 3668080
Resulting array data:
TradingDays(tradingday: [DownloadHTML.Tradingday(timeStamp: 2023-07-07 00:00:00 +0000, open: 403.03, high: 406.679, low: 402.71, close: 402.89, volume: 3668080),
Resulting JSON object:
{
"tradingday" : [
{
"timeStamp" : "2023-07-07T00:00:00Z",
"low" : 402.70999999999998,
"high" : 406.67899999999997,
"volume" : 3668080,
"open" : 403.02999999999997,
"close" : 402.88999999999999
},
Function:
func EncodeTradingDays (tradingDays: TradingDays) async -> String {
var jsonData: Data?
var jsonString: String
let jsonEncoder = JSONEncoder()
jsonEncoder.dateEncodingStrategy = .iso8601
jsonEncoder.outputFormatting = [.prettyPrinted]
do {
jsonData = try jsonEncoder.encode(tradingDays)
} catch {
print("error encoding json data")
}
jsonString = String(data: jsonData!, encoding: .utf8)!
return jsonString
}
Data structures:
struct TradingDays: Codable {
var tradingday: [Tradingday]
}
struct Tradingday: Codable {
var timeStamp: Date
var open: Double
var high: Double
var low: Double
var close: Double
let volume: Int
enum CodingKeys: String, CodingKey {
case timeStamp
case open, high, low, close, volume
}
}
I am learning how to access SQLite without the use of Swift Data. I have a function that tests to see if a table exists and it works. The problem is that I do not understand why it works. It is my understanding that the SELECT statement returns a 0 if the table does not exist and 1 if it does. But the API step statement returns 101 (SQLITE_Done) if the table does not exist. But if the table does exist it does not return 101. Per the SQLite documentation it would appear that 101 means the operation has completed. What am I missing here and is there a way to capture the underlying SQLite 0 or 1 which would allow me to test for that? Below is my function.
func doesTableExist(db: OpaquePointer?) -> Bool {
var tableExists: Bool = true
let testForTable = """
SELECT name FROM sqlite_master
WHERE type='table'
AND name='Contact';
"""
var testForTablePtr: OpaquePointer?
if sqlite3_prepare_v2(db, testForTable, -1, &testForTablePtr, nil) == SQLITE_OK {
if sqlite3_step(testForTablePtr) == SQLITE_DONE {
tableExists = false
}
} else {
print("unable to compile sql statement testing to see if table exists")
}
return tableExists
}
I am learning SQLite queries. I use prepare statement and one or more calls to a step statement. The prepare statement knows the pointer to the database, the UFT8 select statement and the name of the pointer which will point to the complied SQL select statement. The first step call runs the compiled select statement and if data is returned, the first row is made available via column calls. It seems logical to me that the select statement returns all matching rows but if the first step statement only has access to the first row of data, the balance of matching rows must be stored somewhere. Is this the case and if so where are the query results stored? Or is does this work in some other manner?
I have a program that displays data in a table in the main view. It gets this data by running a C API Select statement. On the main view there is also a button which allows me to change the sort order to descending. To do so, this button calls the same function initially called to display data but with a different order by string for the Select statement. Selecting the button causes the data to be displayed in descending order and it is very fast. Unfortunately selecting the button also causes a warning to be displayed in Xcode "Application performed a reentrant operation in its NSTableView delegate". I do not understand what to do to resolve the issue. Any assistance will be appreciated. Below is the Context View and the View Model. The QueryDatabase function called in the view model is just the C api calls including Prepare, Step in a while loop and Finalize. If it would be helpful I can add it.
// Main View:
struct ContentView: View {
@ObservedObject var vm: SQLiteViewModel = SQLiteViewModel()
var body: some View {
if vm.loadingData == true {
ProgressView()
.task {
vm.GetSQLData(fundName: "Fund1", numYears: 1, sortOrder: "main.TimeStamp ASC")
}
} else {
HStack {
Spacer()
.frame(width: 135, height: 200, alignment: .center)
Table(vm.fundData) {
TableColumn("Fund") { record in
Text(record.fundName)
.frame(width: 60, height: 15, alignment: .center)
}
.width(60)
TableColumn("Date") { record in
Text(sqlDateFormatter.string(from: record.timeStamp))
.frame(width: 120, height: 15, alignment: .center)
}
.width(120)
TableColumn("Close") { record in
Text(String(format: "$%.2f", record.close))
.frame(width: 65, height: 15, alignment: .trailing)
}
.width(65)
} // end table
.font(.system(size: 14, weight: .regular, design: .monospaced))
.frame(width: 330, height: 565, alignment: .center)
Spacer()
.frame(width: 30, height: 30, alignment: .center)
VStack {
Button(action: {
vm.GetSQLData(fundName: "Fund1", numYears: 1, sortOrder: "main.TimeStamp DESC")
}) {
Text("Date Descending")
} // end button
.frame(width: 140)
} // end v stack
.font(.system(size: 12, weight: .regular, design: .monospaced))
} // end horizontal stack
.frame(width: 600, height: 600, alignment: .center)
} // end else statement
} // end view
}
// View Model:
class SQLiteViewModel: ObservableObject {
var db: OpaquePointer?
@Published var fundData: [TradingDay] = []
@Published var loadingData: Bool = true
init() {
db = OpenDatabase()
}
func GetSQLData(fundName: String, numYears: Int, sortOrder: String) {
DispatchQueue.main.async {
self.fundData = QueryDatabase(db: self.db, fundName: fundName, numYears: numYears, sortOrder: sortOrder)
self.loadingData = false
}
}
}
I have code that displays an NSTableView. I have the functionality I want but am having problems with the formatting. In the makeNSView function I can set the header cell text, background color and alignment. What I have been unable to determine is how to add what I would term padding to the header cell background color such that it is taller, i.e. extends further above and below the text. As a secondary issue, I would like to be able to control the border widths of the table cells i.e. the NSViews, such that they appear all the same. Right now, adjacent cell edges have double width borders and non adjacent cell edges have single width borders. Below are the displayed results and code.
struct BetterTableView: NSViewRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator: NSObject, NSTableViewDelegate, NSTableViewDataSource {
var dateSorting: String = "ASC"
var closingSorting: String = "ASC"
var closingValues: [ClosingValue] = []
override init() {
super.init()
self.closingValues = GetValues()
}
func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
switch tableColumn.title {
case "Date":
if dateSorting == "ASC" {
closingValues.sort {
$0.date > $1.date
}
dateSorting = "DESC"
} else {
closingValues.sort {
$0.date < $1.date
}
dateSorting = "ASC"
}
case "Closing":
if closingSorting == "ASC" {
closingValues.sort {
$0.date > $1.date
}
closingSorting = "DESC"
} else {
closingValues.sort {
$0.date < $1.date
}
closingSorting = "ASC"
}
default:
print("default")
}
tableView.reloadData()
tableView.scrollRowToVisible(0)
}
func numberOfRows(in tableView: NSTableView) -> Int {
closingValues.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
var nsView = NSView()
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes1: [NSAttributedString.Key: Any] = [
.foregroundColor: NSColor.blue,
.paragraphStyle: paragraphStyle,
.font: NSFont(name: "Arial", size: 14.0) as Any
]
var attributedString = NSAttributedString()
let tempNSView = NSTextField()
switch tableColumn {
case tableView.tableColumns[0]:
attributedString = NSAttributedString(string: closingValues[row].name, attributes: attributes1)
case tableView.tableColumns[1]:
attributedString = NSAttributedString(string: closingValues[row].date, attributes: attributes1)
case tableView.tableColumns[2]:
let closeAsString = String(format: "$%.2f", closingValues[row].close)
attributedString = NSAttributedString(string: closeAsString, attributes: attributes1)
default:
print("problem in table view switch statement")
}
tempNSView.backgroundColor = NSColor.white
tempNSView.isBordered = true
tempNSView.isEditable = false
tempNSView.attributedStringValue = attributedString
nsView = tempNSView
return nsView
}
} // end of coordinator class
func makeNSView(context: Context) -> NSScrollView {
let tableView = NSTableView()
tableView.style = .plain
tableView.delegate = context.coordinator
tableView.dataSource = context.coordinator
tableView.addTableColumn(NSTableColumn())
tableView.addTableColumn(NSTableColumn())
tableView.addTableColumn(NSTableColumn())
tableView.intercellSpacing = NSSize(width: 0.0, height: 0.0)
tableView.headerView?.frame.size.height = 20.0
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: NSColor.black,
.paragraphStyle: paragraphStyle,
.font: NSFont.systemFont(ofSize: 18)
]
let column0AttributedString = NSAttributedString(string: "Stock", attributes: attributes)
let column0Header = tableView.tableColumns[0]
column0Header.headerCell.drawsBackground = true
column0Header.headerCell.backgroundColor = NSColor.systemMint
column0Header.headerCell.alignment = .center
column0Header.headerCell.attributedStringValue = column0AttributedString
column0Header.sizeToFit()
column0Header.minWidth = 90.0
column0Header.maxWidth = 90.0
let column1AttributedString = NSAttributedString(string: "Date", attributes: attributes)
let column1Header = tableView.tableColumns[1]
column1Header.headerCell.drawsBackground = true
column1Header.headerCell.backgroundColor = NSColor.systemMint
column1Header.headerCell.alignment = .center
column1Header.headerCell.attributedStringValue = column1AttributedString
column1Header.sizeToFit()
column1Header.minWidth = 125.0
column1Header.maxWidth = 125.0
let column2AttributedString = NSAttributedString(string: "Closing", attributes: attributes)
let column2Header = tableView.tableColumns[2]
column2Header.headerCell.drawsBackground = true
column2Header.headerCell.backgroundColor = NSColor.systemMint
column2Header.headerCell.alignment = .center
column2Header.headerCell.attributedStringValue = column2AttributedString
column2Header.sizeToFit()
column2Header.minWidth = 90.0
column2Header.maxWidth = 90.0
let scrollView = NSScrollView()
scrollView.documentView = tableView
return scrollView
}
func updateNSView(_ nsView: NSScrollView, context: Context) {
let tableView = (nsView.documentView as! NSTableView)
print("in update ns view")
// work on this section
}
}