I have always struggled to try and understand how Xojo works with simple files and have looked at the examples and just get more confused.
I am trying to do something really simple but need help. All I want to do is write a text file to the desktop called “mytextfile.txt” with the text content of “Hello World!”. I dont want a dialog or anything else, just simply write a text file.
// get a reference to the folderitem (which may or may not exist)
dim f as folderitem = specialFolder.Desktop.Child("mytextfile.txt")
// try and create the file at the location the folderitem refers to
dim tos as TextOutputStream = TextOutputStream.Create(f)
// write my text into the new text output stream
tos.write "Hello World!"
// and nil things so they get flushed etc
tos = nil
f = nil
Be aware that not presenting the user with an Open or Save dialog will cause you a lot of headaches on Mac down the road. If this is only a learning exercise you’ll be fine.
But I thought that the GetSaveFolderItem shows the save dialog box
[quote=361444:@Norman Palardy]dim f as folderitem = specialFolder.Desktop.Child(“mytextfile.txt”)
dim tos as TextOutputStream = TextOutputStream.Create(f)
tos.write “Hello World!”
tos = nil
f = nil[/quote]
Thanks Norman that is exactly what I was after. It was the .Child bit I didnt realise you could do.
closing the stream leaves you with a non-nil reference that you literally cannot use for anything after the point you close it
but it’s also not nil
niling it means that the destructor runs which will force it to be flushed
AND if you try to use it for anything after this point you’re going to get a nil object exception
[quote=361487:@Norman Palardy]closing the stream leaves you with a non-nil reference that you literally cannot use for anything after the point you close it
but it’s also not nil
niling it means that the destructor runs which will force it to be flushed
AND if you try to use it for anything after this point you’re going to get a nil object exception
that wont happen if you just close it[/quote]
sounds like a deficit in the CLOSE command then (my opinion)
especially since Xojo own documentation makes more use of CLOSE than NIL in this regard
[quote=361559:@Norman Palardy]Except one big difference
NIL works - today
Close doesn’t[/quote]
mind explaining that?
CLOSE has been part of TextOutputStream since FOREVER… and works perfectly fine in the thousands of times I’ve used it.
Xojo OWN documentation examples use CLOSE more than NIL
I think Norm’s point is that , for ‘safety’, using NIL ensures that … If you later try to use the stream you will get a nilobjectexception
As opposed to
Which I would expect to generate some other kind of exception, personally.
As would anyone who tries to use a stream after the .close command is issued.
And of course if defined inside a method , it should nil when the method ends.
I hear what you say but I would never have expected/trusted that to happen just by setting a variable to nil.
I’ve been here before.
Objectively, I would not have been surprised by a bug report about ‘setting to nil without closing first causing mysterious memory leaks or locked files’.
I use .close()
… it does what it says on the tin, rather than hoping that the destructor will do it for you.
Set it to nil afterwards, if it satisfies a need.
[quote=361568:@Jeff Tullin]I
Objectively, I would not have been surprised by a bug report about ‘setting to nil without closing first causing mysterious memory leaks or locked files’.[/quote]
It doesn’t - that WOULD be a huge bug
Close followed by nil would be pedantic
Just niling it IS sufficient