I've run into a problem. I've created a file and saved 1000 jsons elements in it.
I want to extract, update, delete a specific json.
I'm creating the file the normal way using FileManager.default and writing info to it using FileManager.default.createFile(atPath: itemPath, contents: data, attributes: nil)
How can I do that without extracting the entire json array and then writing it again in swift.
I'm thinking something like select interogations etc.
Would be glad if you can help me with this.
Create an index in the file header to record the start position and length of the file corresponding to JSON.
Related
So, I'm creating an app that works like a bot, it makes a call to an API from time to time, then it receives a response in a json-like format and saves it like this:
finalResult = RestClient.get( apiUrl, headers = apiHeaders )
jsonData = JSON.parse(ActiveSupport::Gzip.decompress(finalResult))
time = Time.now
File.write("public/#{time}.json", jsonData)
I'm using ActiveSupport to be able to parse this Gzip compressed data, since it's a lot of data, otherwise it takes forever to get it. Then I get the time the data was received, basically, and I use it to name the file so that I can keep good track of it.
What I need to do now is compress this .json file, if possible, into a .zip file(it can be .rar, or .7z, or .tz, whatever) before I upload it to my storage so it takes less space. Is there anyway that I can do something similar to File.write but to save it as a zipped json file? I already checked stuff like zlib and ruby-zip, but they only let me zip files that already "exist", so I can't save it as a zipped .json directly, I'd need to take the .json file and then zip it, but how could I do that if the name of the file is a Time.now and it always change?
I'd appreciate any help, thanks in advance :)
EDIT¹
Giving some more details that may help you to help me:
I created a controller and model to handle this, since I'll be using ActiveStorage. It's ResponsesController and Response model, and the only parameter that the Response model has is has_one_attached :json_file. I intend to use Heroku to handle the CRON job of calling the API and I'll upload the .json files(or .zip files) to an AWS storage.
Is it possible to load a [String : Any] dictionary into a temporary file stream, without writing the contents to disk anywhere, then feed the stream to transferFile, as the file parameter? I know this is possible in PHP, but am not so sure about Swift.
I have come across a problem whereby my data exceeds the messaging limits. Therefore I either need to develop my own protocol to transfer the data, or somehow pass the data to transferFile.
I do not want to write my data to temporary files, transfer, and then delete them, as this is pretty dirty.
Why not just write the contents of the dictionary to a temporary file and pass the URL of that file to transferFile? I know you think that this is a "dirty" way to do things but since you need to pass a file URL to transferFile, that might be your only option - at least, that's the only option I know of ...
I'm looking for the best way (or easiest) to import data into my iOS app using Swift. I've got a file containing recipes and I have to read in the recipe names and instructions.
Example:
Fudge brownies
Mix ingredients in processors until consistent.
Prepare baking sheet with coconut oil and set over at 425.
....
So I have to import several dozen recipes, my questions are
Would it be best to read in a text file?
If so how is this done in Swift?
Also how do I avoid issues with reading the title and recipe into separate variables?
You can read in a text file quite easily doing something like this:
let path = NSBundle.mainBundle().pathForResource("fileName", ofType: "txt")
var dataString = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)
Note you'll have to import Foundation.
You could then create a method to parse the dataString, something like
func parseDataString(string: String)
which you could send the dataString to.
You could put markers (e.g. special characters like (*) ) in the text file that would allow this method to figure out where the titles end and the directions start. There are a number of ways it could be done.
You could then persist your data using CoreData.
I would strongly suggest using JSON data in the files. JSON is a very simple markup format that gives structure to text files, and lets you basically say things like title=BBQ ribs. The reason you use JSON is that Cocoa has really good JSON handling built right in. Check out this thread, it probably does exactly what you want...
How do I parse JSON with Objective-C?
I am receiving a text file from a socket over TCP/IP. There is no file extension (or filename for that matter) as the data is received as bytes. I can take the data from this (in the form of NSData) and load it into a UITextView and display it fine.
I want to persist this data to a file. However, I don't know what format the data is in (txt, rtf, doc, docx)? I assume it as .txt and save it in the documents directory, but is there a programmatic way of finding out for sure? I've looked around StackOverflow and at the documentation and haven't been able to find anything.
And is there a way to get the details of the file attributes like the file creation date.
Thanks in advance.
When you send a file over a TCP/IP connection, only the file contents will be converted to data and be passed across. If you want the filename,extension and the file attributes, then you will have to add those details separately and append it with the data to be sent. Then you can parse it at the receiver end and use the results inside your app.
You can choose the file type you want when you save the data, you can get attributes from file,please refer to Get file creation date.
My application accepts an uploaded file from the user and parses it, making use of seek and rewind methods quite heavily to parse blocks from the file (lines can begin with 'start' or 'end' to enclose a section of data, etc).
A new requirement allows the user to upload encrypted files. I've implemented decryption of the content of the file and return the content string to the existing method. I can parse the string as a CSV but lose the file controls.
Storing an unencrypted version of the file is not an option for business reasons.
I'm using FasterCSV but not averse to using something else if I can keep the seek/rewind behaviour.
Current code:
FasterCSV.open(path, 'rb') do |csv| # Can I open a string as if it were a file?
unless csv.eof? # Catch empty files
# Read, store position, seek, rewind all used during parsing
position = csv.pos
row = csv.readline
csv.seek(pos)
After some digging and experimentation I've found that it was possible to retain the IO methods by using the StringIO class like so:
csv = StringIO.new(decrypted_content)
unless csv.nil?
unless csv.eof? # Catch empty files
position = csv.pos
row = csv.readline.chomp.split(',')
csv.seek(pos)
Only change is needing to manually split the line to be able to use it like a csv row, not much extra work.
You don't need the CSV gem anymore but if you prefer the seek/rewind behaviour you can roll your own for strings. Something like this might work for your scenario:
array_of_lines=unecrypted_file_string.split('\n')
array_of_lines.each_with_index do |line,index|
position=index
row=line
seek=line[10]
end