Need help with NSDictionary dictionaryWithObjectsAndKeys

Mac OSX specific forum
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Need help with NSDictionary dictionaryWithObjectsAndKeys

Post by Wolfram »

Whats wrong here?
I want to copy the owner from one file to another.

Code: Select all

filemanager = CocoaMessage(0, CocoaMessage(0, 0, "NSFileManager alloc"), "init")

filePath_A$ = "/anyfile.txt"

object =CocoaMessage(0, filemanager, "attributesOfItemAtPath:$", @filePath$, "error:", 0)

x =CocoaMessage(0, object, "fileSize")
Debug "file size " +PeekL(@x)

x =CocoaMessage(0, object, "fileOwnerAccountName")

owner.s =PeekS(CocoaMessage(0, x, "UTF8String"), -1, #PB_UTF8)
Debug "owner " +owner

filePath_B$ = "/anotherFile.txt"


;here is the problem
attr =CocoaMessage(0, object, "NSDictionary dictionaryWithObjectsAndKeys:", x, "NSFileOwnerAccountName", 0)

CocoaMessage(0, CocoaMessage(0, 0, "NSFileManager defaultManager"), "setAttributes:", attr, "ofItemAtPath:$", @filePath_B$, "error:", 0)
macOS Catalina 10.15.7
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Need help with NSDictionary dictionaryWithObjectsAndKeys

Post by wilbert »

If you want to set multiple attributes, you need
dictionaryWithObjects:forKeys:
and both the objects and the keys need to be a NSArray object.
Since you are only setting one file attribute, it's easier to do it like this.

Code: Select all

filemanager = CocoaMessage(0, 0, "NSFileManager defaultManager")

filePath_A$ = "/anyfile.txt"

object =CocoaMessage(0, filemanager, "attributesOfItemAtPath:$", @filePath_A$, "error:", 0)

CocoaMessage(@fileSize.q, object, "fileSize")
Debug "file size " +fileSize

x =CocoaMessage(0, object, "fileOwnerAccountName")

owner.s =PeekS(CocoaMessage(0, x, "UTF8String"), -1, #PB_UTF8)
Debug "owner " +owner

filePath_B$ = "/anotherFile.txt"


attr =CocoaMessage(0, 0, "NSDictionary dictionaryWithObject:", x, "forKey:$", @"NSFileOwnerAccountName")

CocoaMessage(0, filemanager, "setAttributes:", attr, "ofItemAtPath:$", @filePath_B$, "error:", 0)
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply