Swift: Access struct/class variables by string name - swift

I would like to access a variables by its literal name in string form.
So far I've found two ways:
https://stackoverflow.com/a/51090177/2330482 which doesn't let me write to the variables
Doing https://stackoverflow.com/a/44461705/2330482 which crashes the app if the key is not found (I don't want to run that risk). If there is a way to check if the key exists first, that would likely resolve my problem. I could combine this with Mirror in the first linked answer to get all keys (and therefore know they exist) before accessing them via .value(forKey) but this seems a bit unsafe. Is there another way to catch the crash if there is no key, or another way for NSObject to first make sure the key doesn't exist?
The reason why I want to do this is to make a struct fallback to default values if an incomplete JSON is fed. If I'm able to safely access variables by their name I could probably solve this problem without having to write Codable implementations for each variable in the struct.

Related

Make the compiler tell me that two variables can’t have the same value

I have a list of objects that need to be loaded into my app. But the load order matters. I’ve created a var which is a part of each object that indicates the load order where the lowest number gets loaded in first. Then I’ve set the default to be 9999. I can manually set this value on each object and override the default. Currently this is where I’m at.
Should I add more objects which need to be loaded in a specific order in the future, I want to make sure there are no conflicts. It seems to me like I could do that by making each load order number on the objects unique, (i.e. no duplicate numbers in all the load order variables).
Is there a way I can make Xcode throw an error or warning if it detects that anything conforming to a protocol has the same value on a variable from that protocol as any of the other conforming objects?
Well, you could list all the objects in an array literal in the order you want them loaded, and forgo the load order property entirely:
let objectsToLoad = [
ObjectDescriptor("hello"),
ObjectDescriptor("world"),
]
But if you have objects defined in disparate places and don't want a single unified array literal that lists them all, then I don't think you can get help from the compiler (or the linker).
In a few months, when Swift will have macros, you'll probably be able to get help from the compiler or the linker. The strategy is to use a macro to wrap each load-order constant in a macro that also generates some type definition whose name includes the constant, e.g. enum _LoadOrder_1 {}, enum _LoadOrder_357 {}, etc. Then, if you have a duplicate, either the compiler or the linker will fail due to the multiple definitions for a single identifier.

Is it possible to get the type of a variable while computing completion suggestions?

I'm thinking of creating a VSCode extension to help people use a library I am creating. I've looked in 50 places and can only see documentation on getting the plain text of the document in which the completion suggestions are triggered.
My library exposes a function with two parameters, both objects with the same keys, but the first one will be defined already and the 2nd one will be passed in as an object literal. For example
const obj1 = {a:1, b:2};
doLibraryThing(obj1, {a:[], b:[]});
I want to provide code completion, or a snippet, or anything that can detect that doLibraryThing is being called, and know what properties were defined in obj1, even though usually it will be imported from another file; and then use those properties to generate the suggestion with the same properties, {a:[], b:[]}.
Is this possible?

Is it possible to create a function that returns a different struct based on an enum input in Unreal?

So I am working on a data driven game in Unreal and essentially I am working more on the back end trying to make it easier for the objects to get all of the relevant data. I was really trying to create a singular function that by passing in a type I could return a different output but, from what I've tried in the editor this doesn't seem possible.
I get the error: The type of Out Struct is undetermined. Connect something to Return Node to imply a specific type. I was trying to use a wildcard as an output but, it doesn't seem to be able to do that. Any insight on this problem would be greatly appreciated.
As mentioned in comments, just make base struct and inherit your *Config structs from it. In return you will send base struct.
You can read more about this solution here:
https://en.wikipedia.org/wiki/Factory_method_pattern

Using AVAsynchronousKeyValueLoading in Swift

I'm curious if there is a Swifty, safe way to use AVAsynchronousKeyValueLoading without having to hard code the keys I'm using. Specifically, I'd like to call loadValuesAsynchronously(forKeys:completionHandler:) with the key "availableMdiaCharacteristicsWithMediaSelectionOptions", but it currently isn't possible to get a String from any kind of KeyPath-based solution. I would love it if there was some kind of way to retrieve the name of a property in such a way that it would be checked at compile time whether the property exists. Is there such an API?
The solution was #keyPath. I for some reason thought that KeyPath had replaced #keyPath in Swift, but #keyPath(AVAsset. availableMediaCharacteristicsWithMediaSelectionOptions) gives the string "availableMediaCharacteristicsWithMediaSelectionOptions" which is exactly what I want.

Dealing with files in PSUnit

I'm writing a Powershell script which is going to go out into a client's current source control system and do a mass rename on all of the files so that they follow a new naming convention.
Being the diligent TDD developer that I am, I started with putting together a PSUnit test case. At first I was thinking that I would pass in a string to my function for the file name (along with a couple of other relevant parameters) and then return a string. Then it occurred to me that I am going to need to break apart the file name into an extension and a base name. Since System.IO.FileInfo already has that functionality, I thought why not just pass in a file object instead of a string?
If I do that however, then I don't see how I can write my PSUnit test without it being reliant on an external resource (in this case, the file must exist for me to get the FileInfo object - or does it?).
Is there a "clean" way to handle this? How are others approaching these issues?
Thanks for any help or advice!
My suggestion is: Be pragmatic and pass in the base name and extension as two separate strings. For convenience reasons, you can provide an overload that accepts a FileInfo.