Post

Replies

Boosts

Views

Activity

How can I set the position of an app window on the screen for macOS with Swiftui?
I am trying to determine how to set the initial position of an app window for macOS. I have tried using NSViewRepresentable and .setFrameTopLeftPoint with no success. Is my syntax incorrect to achieve the desired results or should I be looking at some other method? struct WindowAccessor: NSViewRepresentable { @Binding var window: NSWindow? func makeNSView(context: Context) -> NSView { let view = NSView() view.window?.setFrameTopLeftPoint(CGPoint(x: 600, y: 400)) DispatchQueue.main.async { self.window = view.window } return view } func updateNSView(_ nsView: NSView, context: Context) {} } and var body: some Scene { WindowGroup { ContentView() .background(WindowAccessor(window: $window)) }
0
0
664
Nov ’21
How do I configure the headings in a SwiftUI table.
Hello, The following code displays the correct data but I am unable to figure out how to format the tables headings , including font size and alignment. Any help in pointing me in the right direction will be appreciated. Chris struct SummaryTable: View { private var localArray: [TradingDayPrices] init(passedInArray: [TradingDayPrices]) { self.localArray = passedInArray } var body: some View { let funds: [Fund] = GetSummaryTable(localArray: localArray) Table(funds) { TableColumn("Index Fund") { value in Text(value.name) .padding(.vertical, 2) } .width(90) TableColumn("Shares") { value in HStack { Spacer() Text(value.shares) Spacer() } } .width(90) TableColumn("Share Price") { value in HStack { Spacer() Text(value.sharePrice) Spacer() } } .width(90) TableColumn("Total") { value in HStack { Spacer() Text(value.total) } } .width(110) TableColumn("One Year Gain") { value in HStack { Spacer() Text(value.percentChange) Spacer() } } .width(90) } // end table .tableStyle(.inset(alternatesRowBackgrounds: true)) .padding([.leading, .trailing], 185) .padding([.top], 30) .padding([.bottom], 700) } }
0
0
736
Jan ’23
Swift - SQLite3 Wal Checkpoint -> Unsafe Pointer Parameter
I have a program that adds data from CSV files to core data. Once I have saved the managed object context to the persistent store I want to run a checkpoint to flush the contents of the WAL file into the SQLite file. I have added code to my class in an attempt to do so but am unable to determine on the line "sqlite3_wal_checkpoint( ...)" what to put in the unsafe pointer parameter (denoted with ??????? in the code). class UpdateCoreData: ObservableObject { @Published var isLoading: Bool = true var moc: NSManagedObjectContext init(moc: NSManagedObjectContext) { self.moc = moc Task { await IsDataStoreEmpty(moc: self.moc) let fund1 = CSVtoCoreData(fundName: "Fund1", fileName: "Fund1.csv") let fund2 = CSVtoCoreData(fundName: "Fund2", fileName: "Fund2.csv") let fund3 = CSVtoCoreData(fundName: "Fund3", fileName: "Fund3.csv") await fund1.CSVtoCoreDataG(moc: self.moc) await fund2.CSVtoCoreDataG(moc: self.moc) await fund3.CSVtoCoreDataG(moc: self.moc) do { try moc.save() } catch { print("Error saving. \(error)") } --- start of added code let fileURL = URL(fileURLWithPath: "/Users/Chris/Downloads/Persistent Store 2/Funds.sqlite") var dataBase: OpaquePointer? sqlite3_open(fileURL.path, &dataBase ) if sqlite3_open(fileURL.path, &dataBase) != SQLITE_OK { print("Unable to open sqlite file") } else { print("Succesfully opened sqlite file") } sqlite3_wal_checkpoint(dataBase, ??????? ) sqlite3_close(dataBase) --- end of added code DispatchQueue.main.async { self.isLoading = false } } } // end init }
0
0
577
May ’23
SwiftUI + Xcode + OS X: Center container on screen
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()     } }
1
0
522
Aug ’21
How do I pass an instance of a class between models in MVVM
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
1
0
522
Aug ’22
Charts module not available in Xcode 14.0.1 for macOS app
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.
1
1
1.8k
Oct ’22
Creating Instance of Class of type NSWindowController
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 } }
1
0
653
Mar ’23
Swift Core Data - Change Location of Persistent Store
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) } }
1
0
1.1k
Apr ’23
Swift - access attribute in array of core data entities in a table column via a variable
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) } }
1
0
883
Apr ’23
How do I control the formatting of values when encoding data as JSON
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 } }
1
0
578
Jul ’23
SQLite - Testing for existence of a table
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 }
1
0
1.4k
Jul ’23
Trying to understand SQLite step statement
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?
1
0
567
Aug ’23
Updating displayed data generates reentrant warning
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 } } }
1
0
681
Aug ’23
How do I modify height of tableview header cell background color
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 } }
1
0
1k
Aug ’23