Testing iOS Backend?
Hi there,
Relatively new to iOS programming, but been programming other stuff for awhile. I'm writing an app with a MVVC architecture (s/o CS193p lmao), and I have a Model (really a couple of Models) with some functionality that involves requesting data from the Internet, and parsing it. As you might expect, I have a Model that performs the web scraping, and one that handles the parsing for a specific data format, so it can be presented eventually. I don't currently have a "frontend", since I wanted to flesh out more of the backend first.
Which brings me to my question - what's the best way to sanity check this is all working, that is, how can I run this backend code and just verify things are behaving? Because this involves a couple different models interacting, I'm relying on XCode to resolve what each type is and where I've defined it, so I couldn't really just compile my code from the command line without getting a bunch of missing definition/import errors. However, I'm not sure how to actually "run" anything in XCode without a frontend, it also seems kind of silly to just render a simple View that calls my code in an `.onAppear` or something. If I was using another language, I might be asking for the equivalent of a "main" function I could stick in some of my Swift files to instantiate some of my structs and call some functions, but from what I understand about iOS the point of entry for an app is more complicated to that.
So what's the best practice here? Is it Playgrounds, or XCode Tests or something? What do people like to use?
11 comments · 80% upvoted · other discussions
The last time i did this earlier this year i used the XCode console. i had to extract some nested data to JSON format and wanted to make sure it was doing it correctly.
I can’t remember the exact library/methods i used but i think JSONDecoder() would be a decent bet for just printing to the console. Otherwise the only other thing i’d suggest is rough-and-ready SwiftUI views which shouldn’t take long to do.
doodlebug80085 [OP] 1 points · May 2024
I see what you mean! I don't really have a problem printing my program's data to the console, my issue is actually calling and executing my code without using a View in the Preview tab. Since I haven't made my frontend yet, I could just make a Text View with an .onAppear modifier to call my code from the Simulator/Preview tab, but that seems silly. I wasn't sure if XCode had an alternative means of running backend code.
noidtiz 2 points · May 2024
Ok yeah, my mistake, i misunderstood and oversimplified your post. it’s late where i am but i should have read it again.
i don’t really have any experience of Vapor but it is a popular package for server-side Swift. on their site (Vapor.codes) they have a ready-made example which is 6 lines code, though it’d be more than that to conform it to your two models. The only way i could see it conflicting with the @main entrypoint wrapper is if XCode starts asking you to explicitly type everything, as Vapor using “Application” is a little ambiguous of an init call. I have no first-hand experience with Vapor so i don’t want to lead you on a wild goose chase here.
doodlebug80085 [OP] 2 points · May 2024
Yah, I've used Vapor before, and I think you're right and that would work. I guess I'm just spoiled from other languages (cough, python) where you can just sort of set up a `main` function from some of your backend code and start running things then and there.
I ended up writing XCode tests to instantiate/experiment with things from my backend, and not do the whole "run a Simulator and make a View and test stuff there" hack, only to learn that XCode tests run a device simulator anyway! I guess that's where the tests get executed.
noidtiz 2 points · May 2024
i know what you mean, the last 3 months i’ve been building in Node JS and Django. i’m a little rusty XCode. Good luck!