>>my app crashes when sandbox is enabled
So, if it's not crashing without the sandbox, then the probability is that you're trying to access a directory that requires explicit user approval to access.
>>I've tried different directories instead of Caches but they don't seem to make any difference
Did you try the Application Support directory? That's one that can accessed while sandboxed. (Note: When using Application Support unsandboxed, the convention is to create your own subfolder named with your app's bundle ID, so you don't clobber other app's files. When sandboxed, you don't need to create the subfolder — though of course you can if you want.)
The problem you have is that line 06 of your code fragment isn't safe, and NSSearchPathForDirectoriesInDomains returns no useful error information if it fails. (You should regard this function as outdated.) Instead, use FileManager.url(for:in:appropriateFor:create:) instead. It throws an error if something goes wrong.
If you do use a function that returns an array (FileManager.urls(for:in:) is the modern equivalent of NSSearchPathForDirectoriesInDomains), don't use the array subscript without first check that the array has something in it. I suspect this is the actual cause of your crash right now.
Or, you may actually want to use a temp directory for this. Instructions for creating a temp directory in a modern way are here:
https://developer.apple.com/documentation/foundation/filemanager/1407693-url
under the "Discussion" heading. (In this case, the 3rd parameter is used only to determine which disk volume to use for the temp directory. IIRC you can pass nil if you don't care.)
In general, your code shows a lot of places where you are "ignoring" optionality (using the "!" operator to prevent messages from the compiler counts as ignoring). Your code will be a lot more robust if you check for unexpected nil results at the point where they're generated, and don't propagate values with optional types through your code. The "guard let" and "if let" constructs are your best friend here. 🙂