Create empty file swift
Using your script in the linked question, try:
shell("touch file.txt")
The command touch
will create the file file.txt
.
You do not need the terminal for anything here, just run your .swift
file and you will have your file.
In case you need the file to write to, just use something like this:
let url = URL(fileURLWithPath: "file.txt")
try "Some string".write(to: url, atomically: true, encoding: .utf8)
You file file.txt
will contain the string "Some string" in it.
Update
AFAIK it is not possible to just create a file, but you could create a file with an empty string:
try "".write(to: url, atomically: true, encoding: .utf8)
As mentioned by OP in comment, it is also necessary to disable sandboxing: Remove Sandboxing
Create empty file for writing?
I don't see anything wrong with what you've got... sounds logical if you are planning on writing to the file in a stream.
If the size and availability of your data is such that you don't need to maintain an open channel to which you can stream data (which I imagine is not your case since you explicitly specified needing to create an empty file), you could eliminate the second line:
NSString *content = @"Put this in a file please.";
NSData *fileContents = [content dataUsingEncoding:NSUTF8StringEncoding];
[[NSFileManager defaultManager] createFileAtPath:@"/Some/Path/foo.txt"
contents:fileContents
attributes:nil];
How to create a file in an iOS App made with Swift?
Don't use hardcoded paths like "AppData/Documents", instead it's better to ask the system for e.g. the path to the app support directory using
let appSupportDir = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let filePath = appSupportDir.appendingPathComponent("points.txt").path
create an empty file of specific size and write new data to specific position with fseek
Example:
NSString *filePath = [@"~/Desktop/FHTest.txt" stringByExpandingTildeInPath];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:[NSData new] attributes:nil];
NSFileHandle *fh;
NSData *data1 = [@"test Data" dataUsingEncoding:NSUTF8StringEncoding];
fh = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fh writeData:data1];
[fh closeFile];
NSData *data2 = [@"more Data" dataUsingEncoding:NSUTF8StringEncoding];
fh = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fh seekToFileOffset:100];
[fh writeData:data2];
[fh closeFile];
Note that the seekToFileOffset:100
will fill expand the file as necessary filling as necessary with 0x00 bytes.
To create a file of specific size, in this case 100 bytes:
unsigned long long size = 100;
[[NSFileManager defaultManager] createFileAtPath:filePath contents:[NSData new] attributes:nil];
NSFileHandle *fh = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fh seekToFileOffset:size-1];
[fh writeData:[@"\x00" dataUsingEncoding:NSUTF8StringEncoding]];
[fh closeFile];
New files are always empty when creating a new class in Xcode
You code shows that you created Cocoa classes not Cocoa Touch. When creating file choose iOS and then select Cocoa Touch Classes and create file sub class of UITableViewController. Then you will get as you expected.
Related Topics
Swift Combine How Set<Anycancellable> Works
Accessibility Custom Actions Aren't Announced in Swift
Crash Casting Wknsurlrequest As? Other Type
Nsmanagedobjectcontext's Propagatesdeletesatendofevent Set to False Causes Error on Save
Why Swift Call Too Shallow Here
Arkit - Raycasting Using a World Ray Instead of a Screen Point
Hide Header While Collection View Is Loading
Uipangesturerecognizer Is Not Working in iOS 13
How to Distinguish Bool and Int in Swift
Swift - Boundingbox Cause Exc_Bad_Access (Code=1)
Codable: Does Not Conform to Protocol 'Decodable'
Member Operator '==' Must Have at Least One Argument of Type
Cell Is Duplicated Multiple Times When Posting to Firebase
Horizontal Scrolling in Spritekit (Vs. a Viewcontroller)
What Does an Underscore "_" Mean in Swift
Localize iOS 14 Dynamic Widget Configuration
How to Spin and Add a Linear Force to an Entity Loaded from Reality Composer