I've written an Switft/macOS App that creates directories deep within its own hierarchy. (no problem). Porting to SwiftUI means these directories and files should go(default) to ~/Documents. Since my Documents dir is (chmod -r 700) the ported app cannot write due to permissions. Which begs he question: what ID does the running app use?
I found:
https://developer.apple.com/forums/thread/82503?answerId=249036022#249036022
and it did change Xcode's :
kMDItemFSOwnerGroupID = 20
kMDItemFSOwnerUserID = 502
but it still does not write as though it has my permissions.
Aside from changing my ~/Documents dir permissions, how do I make my App (build/run within Xcode) as me so it can write?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I am porting a functioning Swift App to SwiftUI with the ambitious intention of Multiple platform coding, so the project was created as such.
I've worked out every issue except as the title suggests.
My pertinent code:
import SwiftUI
import SceneKit
struct ContentView: View {
var scene : SCNScene = SCNScene = { () -> SCNScene in // DO NOT REPLACE OR MODIFY THE FIRST AND LAST LINES OF THIS VAR DECLARATION, e.g., "var scene:SCNScene{ ... }" else initScene(newScene) FAILS to create children!
let newScene = SCNScene()
initScene(newScene) // adds ChildNodes named: "precession" & "nuclearSpin"
let newCamera = SCNNode()
newCamera.position = SCNVector3(x: 0, y: 0 ,z: 40)
newCamera.name = "cameraNode"
newCamera.camera = SCNCamera()
newScene.rootNode.addChildNode(newCamera)
10. newScene.fogDensityExponent = 0.0 return newScene
}()
@EnvironmentObject var structures:Structures
@State private var Ncv = Int()
@State private var indexMomentcv = Int()
var body: some View {
ZStack{
20. SceneView(
21. scene: scene,
22. pointOfView: scene.rootNode.childNode(withName: "cameraNode", recursively: false),
23. options: [.allowsCameraControl, .autoenablesDefaultLighting, .temporalAntialiasingEnabled]
24. ).colorInvert()
25. VStack(alignment: .trailing){
26. Group{
27. HStack{
28. VStack{ // ... and so on
29.
I have verified the existence of all nodes created for the scene, i.e., the objects to be seen, using these useful functions:
func printChildNodeHierarchy(node: SCNNode) { node.enumerateChildNodes( { (anyNode,false) -> Void in return print( anyNode ) } ) } // SUPER handy
func printChildNodeTransform(node: SCNNode) { node.enumerateChildNodes( { (anyNode,false) -> Void in return print( anyNode.name!,"\t", anyNode.simdTransform,"\t",anyNode.scale ) } ) }
func printChildNodeAction (node: SCNNode) { node.enumerateChildNodes( { (anyNode,false) -> Void in return print( anyNode.name!,"\t", anyNode.hasActions,"\t",anyNode.actionKeys ) } ) }
...which print in the debugging area. I have verified the materials assignments to the elements of those nodes. So, everything is in place as in the functioning Swift/macOS App but I just don't see the objects.
What I've tried:
Since I run in DarkMode theme I have cycled through the combinations of Dark/Light and with/without line 24's .colorInvert() and swapping ZStack with HStack.
Some combo's do wash-out, but still no objects.
I zero'd out the fog thinking it might be too foggy. Not.
I've created a scene.scn file as a resource, applied the above attributes to it, reference it with scene = SCNScene(named: scene.scn"), but get the same non-result.
Idk if some checkbox is errant in the build/target etc. area. Wouldn't know if it was. I'm not a professional programmer.
Any thoughts are appreciated. I've been stuck for days.
oh yeah: as of this posting I am up to date on all Apple software, running 2018 MacMini/Intel/SSD
My ContentView has an @EnvironmentObject and a few @State var's.
The State vars are for use in TextFields, one can use a Picker or manual entry, and works fine.
The other State var is to have its value set when the user hits a Button whose action executes a host of functions that changes the values of over twenty @Published vars monitored/updated by the @EnvironmentObject vis-a-vis Combine. This TexField must also be manually set/edited when desired. Within that same Button(action:{}) the next line of code attempts to set a State var to the value of the Published EnvironmentObject that had been changed by all those functions in the previous line.
The problem is: the EnvironmentObject is not updated by the time this line of code is executed. Such that the Button must be pressed twice for the result to change the TextField.
I have scoured the web and did not find, or did not understand, what is amiss.
I have successfully used .id(UUID()) on Views to ensure updates, but this issue is within a Button/action and I wouldn't know how to implement it here as TextField is a struct, and I can't implement a View.id(UUID()) inside the Button. So far as I know.
Thanks.
I have modified Apple's Tutorial "CPU-GPU-Synchronization.xcodeproj" to suit my needs but cannot see how to launch it, e.g., as a sheet, from a Button inside a SwiftUI App.
I found this:
"developer.apple.com/forums/thread/119112"
which establishes the Coordinater wrapper, device and CommandQueue, etc., but how do I "replace?" those already established in ObjC code? For example, ObjC does not recognize SwiftUI struct's and I can't @objc preface a SwiftUI struct as I can a SwiftUI class(ref. the above thread).
Alternatively, is this the best venue to ask Apple to update/integrate their tutorials?
Thanks.
As stated in the title, I want to hide or show a StackView when a menu item is selected, which in turn changes a global variable.
How do I access the identity or handle of the StackView?
Code to draw a graph of data. Each abscissa has an ordinate range to be displayed as a line segment.
All data, i.e., scaled points are verified to be within the declared analysisView.bounds. strokeColors are verified within the range 0..1
BTW, no I don't need animation for this static data, but CALayer seemed to require more coding, and I found fewer code examples for it.
The code below has two problems:
1) it doesn't draw into the window
the weird behavior of min/max
The first is why I am posting. What am I missing?
import AppKit
class AnalysisViewController: NSViewController {
@IBOutlet var analysisView: NSView!
var ranges = [ClosedRange<Double>]()
var ordinateMinimum = CGFloat()
var ordinateMaximum = CGFloat()
var ordinateScale = CGFloat()
let abscissaMinimum:CGFloat = 1
let abscissaMaximum:CGFloat = 92
let abscissaScale :CGFloat = 800/92
let shapeLayer = CAShapeLayer()
var points = [CGPoint]() // created just to verify (in debugger area) that points are within analysisView.bounds
func genrateGraph() {
// ranges.append(0...0) // inexplicably FAILS! @ ordinateMinimum/ordinateMaximum if replaces "if N == 1" below
// ranges.append(0.1...0.1) // non-zero range does not fail but becomes the min or max, therefore, not useful
for N in 1...92 {
if let element = loadFromJSON(N) {
if N == 1 { ranges.append( element.someFunction() ) } // ranges[0] is an unused placeholder
// if N == 1 { ranges.append(0...0) } // inexplicably FAILS! @ ordinateMinimum/ordinateMaximum if replacing above line
ranges.append( element.someFunction() )
}
else { ranges.append(0...0) } // some elements have no range data
}
ordinateMinimum = CGFloat(ranges.min(by: {$0 != 0...0 && $1 != 0...0 && $0.lowerBound < $1.lowerBound})!.lowerBound)
ordinateMaximum = CGFloat(ranges.max(by: {$0 != 0...0 && $1 != 0...0 && $0.upperBound < $1.upperBound})!.upperBound)
ordinateScale = analysisView.frame.height/(ordinateMaximum - ordinateMinimum)
for range in 1..<ranges.count {
shapeLayer.addSublayer(CALayer()) // sublayer each abscissa range so that .strokeColor can be assigned to each
// shapeLayer.frame = CGRect(x: 0, y: 0, width: analysisView.frame.width, height: analysisView.frame.height) // might be unneccessary
let path = CGMutablePath() // a new path for every sublayer, i.e., range that is displayed as line segment
points.append(CGPoint(x: CGFloat(range)*abscissaScale, y: CGFloat(ranges[range].lowerBound)*ordinateScale))
path.move(to: points.last! )
points.append(CGPoint(x: CGFloat(range)*abscissaScale, y: CGFloat(ranges[range].upperBound)*ordinateScale))
path.addLine(to: points.last! )
path.closeSubpath()
shapeLayer.path = path
// shapeLayer.strokeColor = CGColor.white
let r:CGFloat = 1.0/CGFloat(range)
let g:CGFloat = 0.3/CGFloat(range)
let b:CGFloat = 0.7/CGFloat(range)
// print("range: \(range)\tr: \(r)\tg: \(g)\tb: \(b)") // just to verify 0...1 values
shapeLayer.strokeColor = CGColor(srgbRed: r, green: g, blue: b, alpha: 1.0)
}
}
override func viewDidLoad() {
super.viewDidLoad()
view.wantsLayer = true // one of these (view or analysisView) must be unneccessary
view.frame = CGRect(x: 0, y: 0, width: 840, height: 640)
analysisView.wantsLayer = true
analysisView.frame = CGRect(x: 0, y: 0, width: 840, height: 640)
genrateGraph()
}
}
I have searched but cannot find an example of this.
In my example I use playground to nest for-loops in a function called from within DispatchQueue.
I find many roadblocks:
//import AppKit // playground doesn't recognize AppKit?!
import Dispatch
import Foundation
var comboCount:NSKeyValueObservation?
//class viewContr: NSViewController {
@IBOutlet weak var progressBar: NSProgressIndicator!
// deinit { comboCount?.invalidate() }
@IBAction func calcStuff() {
DispatchQueue.global(qos: .userInitiated).async {
let combos = possibleCombos()
DispatchQueue.main.async {
for word in combos {
word.print0b8()
}
}
}
}
//}
func possibleCombos() -> [Int8] {
var combos = [Int8]()
for a in [0, 1] {
for b in [0, 1] {
for c in [0, 1] {
for d in [0, 1] {
combos.append(Int8(d | (c << 1) | (b << 2) | (a << 3)) )
comboCount = combos.count/16 as! NSKeyValueObservation
// known total combos = 16
// How do I pass combos.count out to a progress bar?
}
}
}
}
return combos
}
extension Int8 {func print0b8() {print("0b" + pad(string: String(self, radix: 2), toSize: 4))}}
func pad(string : String, toSize: Int) -> String {var padded = string;for _ in 0..<(toSize - string.count) {padded = "0" + padded};return padded}
Removing the @IB obstacles and the attempted declaration of a ViewController does produce output, but can't be used with a progress bar, e.g.,
import Dispatch
import Foundation
DispatchQueue.global(qos: .userInitiated).async {
let combos = possibleCombos()
DispatchQueue.main.async {
for word in combos {
word.print0b8()
}
}
}
func possibleCombos() -> [Int8] {
var combos = [Int8]()
for a in [0, 1] {
for b in [0, 1] {
for c in [0, 1] {
for d in [0, 1] {
combos.append(Int8(d | (c << 1) | (b << 2) | (a << 3)) )
// known total combos = 16
// How do I pass combos.count out to a progress bar?
}
}
}
}
return combos
}
extension Int8 {func print0b8() {print("0b" + pad(string: String(self, radix: 2), toSize: 4))}}
func pad(string : String, toSize: Int) -> String {var padded = string;for _ in 0..<(toSize - string.count) {padded = "0" + padded};return padded}
In my actual App, i.e., not this playground, I have a ProgressBar in my storyboard and an IBoutlet declared in my view controller. I just can't find how to pass a value out of "possibleCombos()" and into the NSProgressIndicator.
What I have read suggests a delegate but reading about delegates hasn't helped. I need an example.
Thanks.
This is the second issue in my post "ProgressBar with DispatchQueue", delegation.
I have used delegates with menu items but not program vars.
In the previous post my ViewController has a progress bar but I would like the progress.DoubleValue var to be updated in a remote function, i.e., a function not declared within the ViewController class. (If I moved the function into the ViewController it would double in size.)
How do I create and implement such a delegate? Please give example code. Thanks.
The model increments through a range of dates converted to Double and scaled, ranging from 0 to 1.0
I wish to show a determinate progress bar.
As I understand it, the ProgressView() must be declared in the ContentView and its progress var must be declared as a @State var
The ContentView declares the model vars as @EnvironmentObjects
If I declare:
@State private var = model.progressValue
I get the error:"Cannot use instance member 'model' within property initializer; property initializers run before 'self' is available"
btw the model class declares:
@Published var progressValue : Double
The model func (to increment through dates) is launched by a button in the ContentView.
idk how to get the incremented progressValue from the model to the ContentView State var.
My app generates piecemeal near terabyte data distributed over one hundred files. Due to RAM and internal HD limitations I must store to an external HD in piecemeal fashion. That is to say, append fresh records directly to an existing file without reading-in the whole file, appending, and writing it out again.
Ultimately (when data generation ceases) I will check each file to ensure uniqueness of member data structures.
Is Core Data the tool I should use?
If not, how do I append directly to disk-files without first reading it in?
Attached is an entire project (4 files) that mirrors my actual project including the failure to save to file.
Am I:
missing some syntax in this code?
failing to config a defaults file?
not set the necessary parameters in " "Build Settings" or "Build Rules etc.?
I was writing to JSON files, but now that I must append to files directly, and JSON doesn't do that easily, I am trying to write using native macOS tools.
WELL, IT SEEMS I CAN'T SEND YOU THE CODE, TOO MANY CHARS. I CAN'T ATTACH ANY FILE EITHER. WHY OFFER IT IF IT IS NOT ALLOWED?
ANYWAY, CAN YOU GLEAN ANYTHING FROM THIS... Thanks.
My debugger area:
2022-05-28 12:03:11.827372-0500 exampleClassInClassSecureCoding[1508:29981] Metal API Validation Enabled
2022-05-28 12:03:11.940123-0500 exampleClassInClassSecureCoding[1508:29981] *** NSForwarding: warning: object 0x600003cf7090 of class 'exampleClassInClassSecureCoding.classOne' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[exampleClassInClassSecureCoding.classOne replacementObjectForKeyedArchiver:]
2022-05-28 12:03:11.940416-0500 exampleClassInClassSecureCoding[1508:29981] Unrecognized selector -[exampleClassInClassSecureCoding.classOne replacementObjectForKeyedArchiver:]
Unrecognized selector -[exampleClassInClassSecureCoding.classOne replacementObjectForKeyedArchiver:]
Performing @selector(didPressButton:) from sender _TtC7SwiftUIP33_9FEBA96B0BC70E1682E82D239F242E7319SwiftUIAppKitButton 0x7ff08ab06480
Developing a program that needs maximum cores. My previous Intel Mac mini used all 6 cores, according to Activity Monitor. It showed 600% cpu usage. I assumed the M2 would use twelve, but only shows 400% cpu usage in Activity Monitor, ergo, two less!
I don't recall setting a core usage parameter in Xcode.
Any ideas?
Ventura 13.3 and Xcode 14.3 up to date as of this post.
Below is some simple code that does nothing, saves nothing. Yet the memory usage keeps rising. Am I missing something or is this a bug?
The code requires 6 core CPU minimum.
ContentView.swift
import SwiftUI
import Combine
struct ContentView: View {
@EnvironmentObject var loopClass:LoopClass
var body: some View {
ProgressView(value: Float(loopClass.loop_count), total: Float(loopClass.max_loops) ).progressViewStyle(.circular).padding()
Button("Run looper", action: {
loopClass.loopFunc()
}
)
}
}
LoopClass.swift
import Combine
class LoopClass: ObservableObject {
@Published var loop_count = 0
@Published var max_loops = 0
func loopFunc () {
let loopSet = [ 1, 3, 5, 7, 11, 13 ]
max_loops = Int(pow(Double(loopSet.count), 13.0))
print("max_loops = \(max_loops)")
for loop13 in loopSet {
DispatchQueue.global().async {
for loop12 in loopSet {
for loop11 in loopSet {
for loop10 in loopSet {
for loop9 in loopSet {
for loop8 in loopSet {
for loop17 in loopSet {
for loop16 in loopSet {
for loop5 in loopSet {
for loop4 in loopSet {
for loop3 in loopSet {
for loop2 in loopSet {
for loop1 in loopSet {
DispatchQueue.main.async{ self.loop_count += 1 }
}
}}}}}}}}}}}
} // DQ
} // for loop13
}
}
given [URL.lastPathComponent] how do I sort as Finder sorts in macOS?
Example: urlPaths = contentsOfDirectory(at: ... e.g., ["1H", "10Ne", "11Na"]
urlPaths.sort(by: { $0 < $1 } //Apple Swift version 5.9
print(urlPaths) // 10Ne 11Na 1H
whereas Finder filename sort gives // 1H 10Ne 11 Na
and is beautiful. I have always taken this sort for granted and am now buggered to duplicate it.
there are other unresolved posts on this issue but maybe mine is solvable.
I copied the files I contributed from a working project to a new project using the finder (drag and drop). So I did not copy entitlements. Xcode automatically added the files to the project just fine. My app uses data on a removable drive, or used to, because I now get the error above. At one point I copied the product app to the desktop hoping to get the permissions alert window, but it never came. Oddly, in System Settings/Privacy & Security/Files & Folders my "new" app is listed with "Removable Volumes" slider = on.
The file permissions (unix-like) on that drive haven't changed between projects and allow everyone to read.
How can I fix this?