What code can be used as the pure swift (not swiftUI) as a user-interface?

I have two questions,

  1. I have XCode running on mac M1. I want to know how to open a pure swift code (not swiftUI) project in XCode. It always opens with swiftUI. Someone earlier suggested to open Storyboard or XIB for the interface, but the problem is when I go to file-->New--> Project there's no anything named Storyboard or XIB

  2. The following code is from swiftUI, it shows a user interface with a text saying "hello world", how to get the same output with only using pure swift code? Any code suggestions in pure swift code without importing any external packages/frameworks etc?

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
        .padding()
    }
}

There is no way to create GUI apps using just the Swift language. By itself, Swift is pretty barebones, offering nothing higher level than collections and async primitives. You'll have to use SwiftUI, UIKit, or some other UI framework unless you want to draw a UI from scratch. Even then you'd be interfacing with some sort of windowing or drawing framework, so it's still not "pure".

Didn't know pure swift is this much incapable. There should be a way isn't it? may be with a long enough code, can it popup a user interface?

Sure, you could probably do it entirely manually, but I'm not even sure where to start with that.

So, people who use pure swift use it only to make software without user interfaces? I'm pretty sure there is a way as if the swift is low level, there should be a way to pop a user interface. C++ is also low level, people even make games out of it. The games are more than user-interfaces which contains huge amount of graphics far exceeded the simple user interfaces.

Nobody limits themselves to "pure" Swift.

Other than languages similar to MS's Visual Basic, few programming languages have any sort of graphics primitives or UI as part of the language or even their standard libraries. All of them rely on external libraries, which may be written in the same language, or something completely different.

4 Likes

Then what's the use of not having a swiftUI application? why people hate it that much?
Most of the people here says "No swiftUI questions, only "swift" questions. So, I thought swift must be pretty capable that's why they are backing "pure swift" than "swiftUI".
It seems like they're even use alot of frameworks behind the scenes, they just hate "swiftUI" because they don't have it in their PCs. Sad situation though.

That's just because Apple doesn't want SwiftUI questions here, just on their DevForums or anywhere else. You should use SwiftUI if it makes sense for your application.

2 Likes

I don't know that anyone hates SwiftUI. It's just that it's a proprietary framework that is only available on Apple's hardware. It's out of scope for these forums.

6 Likes

That means pure swift is capable of writing more advanced (yet time consuming) components, which can be used in or as a user interface etc. So, I'm asking what kind of code can I use to pop-up a user interface in pure swift.

As far as I know, SwiftUI is written entirely in Swift. But the question is: what do you mean by "pure Swift"?

2 Likes

I don't want to use swiftUI, I want to use pure swift to pop a user interface. It doesn't matter how long the code is or how many generations it takes to write it (I'll let my grand son to continue). I just want to use pure swift to write my software which will have a graphical user interface. I will wait for someone who knows then.

1 Like

I used "pure swift", just because people often confuse "swift" with "swiftui". So, just assume, "pure swift" = "swift".

If it was written in swift, then that means there's a way to write it. What is the code to pop a simple user interface?

I can't tell how much you know about how computer software is layered. To do what you seem to be asking, you'd have to write the operating system in Swift, along with the graphics drivers. Then on top of that, write a library in Swift that interfaces with that driver.

If all you want to do is write in Swift and have it communicate with other software that eventually reaches down to the video hardware, you can do that today. You can interface with any number of C libraries without writing anything but Swift. That includes Core Graphics on Apple platforms and GTK on Linux, Windows and MacOS.

5 Likes

I'm using mac m1, which is created by apple. So, they have already created the most suitable operating system and graphics drivers which must work on swift. I don't think I have to write entire operating system if I don't use windows or linux :relieved: I'm on MacOS already. MacOS definitely must be knowing what swift is trying to say. So, I think I just need the long code that would ask macOS to pop a user-interface for the given parameters.

It's possible you're severely underestimating the sheer size the topic 'graphical user interfaces' truly is. There are so many things to consider. Like operating system, drawing backend, the UI components, etc. There is no 'write code and run everywhere' for GUIs without 'external packages'.

Since you're on macOS, here's some very minimal code to get a window to popup using the OS' main GUI framework:

import AppKit
import Foundation

class SampleApp: NSObject, NSWindowDelegate {
	let window = NSWindow()

	override init() {
		super.init()

		_ = NSApplication.shared

		window.setFrame(NSRect(x: 0, y: 0, width: 500, height: 500), display: false)
		window.styleMask = [.closable, .miniaturizable, .resizable, .titled]
		window.backingType = .buffered
		window.center()
		window.makeKeyAndOrderFront(nil)
		window.delegate = self
		window.title = "Hello window"

		NSApp.setActivationPolicy(.regular)
		NSApp.activate(ignoringOtherApps: true)
		NSApplication.shared.run()
	}

	func windowWillClose(_ notification: Notification) {
		NSApplication.shared.terminate(0)
	}
}

let app = SampleApp()

The code should work right out of the gate. You can even launch it via Swift CLI

$ swift hellowindow.swift

This won't work on any other platform because, again, it uses macOS' main interface framework: AppKit. (Which SwiftUI also uses in the backend for macOS. It uses UIKit on iOS)

4 Likes

If you want to get down to the bare metal and write your UI directly, then you'll likely want to start by learning Metal, which will allow you to interface with the machine's graphics card to do some drawing to the screen. (I suggest using a framework instead of attempting to work with the hardware directly, to save yourself a bunch of time.)

Start small, by learning how to get the graphics card to display a single pixel, then some lines, then more complex shapes. Learn how to draw in various colors, and fill shapes. Maybe how to draw some gradients. Once you've done all of that, you'll have most of the basic pieces you'll need to draw a good chunk of your UI.

The next really hard part is drawing text, which you'll need to steep yourself into a bit. I'll save a bit of the description here, but you can read up on some of the challenges. You'll need to learn how to draw read font data, render glyphs, shape text, do subpixel aliasing if relevant, etc.

Once you've got all that done, you're ready to make the UI actually interactive. Using something like DriverKit (again, just to abstract away having to deal with the bare hardware itself, which is a massive pain), you'll need to build up an event loop to start accepting mouse and keyboard input. You can then start reading mouse clicks, keyboard key presses, and more.

With all of that in place, you've got a lot of the tools in place to write your UI from the bottom up. And oh, don't forget to make it actually look pretty, unlike some older UI libraries.


Or, you can use AppKit/UIKit/SwiftUI, which built on all of the ideas that our parents and grandparents worked on so that we're the grandchildren who get to benefit. :wink: There's nothing wrong with that.

Swift is plenty capable — it can do all of these things and more. As could Objective-C, C, C++, Rust, <whatever>, you name it. But the key is that very few languages actually come with some form of UI machinery attached. If you ask "how do pop up a UI in C?", the answer is usually "well, do you want to use Qt, or wxWidgets, or fltk, or ...". Just because you can do something in "pure C", or "pure Swift", or "pure ...", doesn't mean it makes a lot of sense to.

And as for discussion about SwiftUI being off-topic for these forums…

As others have mentioned, it's off-topic because this is a space for talking about the open-source components of Swift and its development. SwiftUI is an Apple-specific, closed-source UI library that is one of several. It's also off-topic to discuss AppKit and UIKit for similar reasons. If Swift did include an open-source UI library as part of the language, it's very likely that it would be on-topic to discuss here.

11 Likes

yeah I get it. Thank you for the code.