New answers tagged

2

This let filePath = Bundle.main.path(forResource: "test", ofType: "dat") guard filePath != nil else { return } let fileURL = URL(fileURLWithPath: filePath!) is an anti-pattern in Swift, see also When should I compare an optional value to nil? on Stack Overflow. Instead of testing a value against nil and then force-unwrapping it, you should use optional ...


1

Close on destruction You shouldn't need to explicitly close your file member on destruction. Since it's a safe variable and not an unsafe pointer, fstream's own destructor will be called and the file will be closed. Const members You should make path const, since it doesn't change over the course of the object's lifetime. File object scope inFile is not ...


3

Use the force library Luke! Your code does use std::vector, but for reasons I don't understand, it also uses new to allocate a manually managed dynamic array as well. You don't seem to gain anything from this, so I'd advise using std::vector throughout. I'd also look at the standard library's algorithms. Some of them (e.g., std::remove_copy_if) could be ...


1

Since writeTo does not modify v, it should take that parameter as a const reference (const std:::vector<T>&v). In LineEditor, the constructor doesn't initialize ary, so if you construct a LineEditor object and destroy it without calling removeLines (or if that function returns, possibly by an exception, before you allocate the memory) you'll ...


3

This is my first post here, but I thought I might give this one a shot. Your code could be made more user friendly by providing custom error messages. When you get the filename from the command line arguments, you assume that the user will provide at least one command line argument. You could provide a helpful error message if they don't. If opening a ...


Top 50 recent answers are included