Noah Braunfeld
Noah Braunfeld

Reputation: 45

Error with no useful explanation in SwiftUI view and struct

I am trying to make a SwiftUI view about chemistry. I decided to create a description view where information about each element is displayed. I want to call a function to generate the electron configuration, but it gives me an error with seemingly no useful information. When I try to call a function in the view, I get this error:

Error Domain=com.apple.dt.ultraviolet.service Code=12 "Rendering service was interrupted" UserInfo={NSLocalizedDescription=Rendering service was interrupted}

I have tried switching how I call the functions but nothing has worked.

Here is the code

Element Object:

import Foundation
import Combine

struct Element: Decodable, Identifiable, Hashable {
    public var id: Int
    public var name: String
    public var symbol: String
    public var atomicMass: Double
    public var atomicRadius: Double
    public var density: Double
    public var eConfig: [Int]

    /*
     -- electron config layout
     [2, 2, 6, ...] -> 1s2 2s2 2p6 ...
     */

    func createOrbital() -> String {
        var orbital: String = ""

        // MARK: Orbital Loop
        for (index, val) in self.eConfig.enumerated() {
            let block = orbitalBlock(from: index)!
            var level = getRow(from: index)!

            // MARK: Subtraction rules
            if block == "d" {
                level -= 1
            } else if block == "f" {
                level -= 2
            }

            orbital += String(level)
            orbital += String(block)
            orbital += superscript(from: val)
        }

        return orbital
    }
}

func superscript(from val: Int) -> String {
    let str = String(val)
    let supers = [
        "0": "\u{2070}",
        "1": "\u{00B9}",
        "2": "\u{00B2}",
        "3": "\u{00B3}",
        "4": "\u{2074}",
        "5": "\u{2075}",
        "6": "\u{2076}",
        "7": "\u{2077}",
        "8": "\u{2078}",
        "9": "\u{2079}"]

    var newStr = ""
    for char in str {
        let key = String(char)
        newStr.append(Character(supers[key]!))
    }
    return newStr
}

func orbitalBlock(from index: Int) -> String? {
    switch (index) {
    case 1...4, 11, 12, 19, 20, 37, 38, 55, 56, 87, 88:
        return "s"
    case 5...10, 13...18, 31...36, 49...54, 81...86, 113...118:
        return "p"
    case 21...30, 39...48, 72...80, 104...112:
        return "d"
    case 57...71, 89...103:
        return "f"
    default:
        return nil
    }
}

func getRow(from index: Int) -> Int? {
    switch (index) {
    case 1...2:
        return 1
    case 3...10:
        return 2
    case 11...18:
        return 3
    case 19...36:
        return 4
    case 37...54:
        return 5
    case 55...86:
        return 6
    case 87...118:
        return 7
    default:
        return nil
    }
}

Element View:

import SwiftUI


struct ElementDetailView: View {
    public var element: Element
    private var orbital: String

    init(element: Element) {
        self.element = element
        self.orbital = element.createOrbital()
    }

    var body: some View {
        VStack {
            Text(orbital)
        }
    }
}



struct ElementDetailView_Previews: PreviewProvider {
    static var previews: some View {
        ElementDetailView(element: Element(id: 1, name: "Hydrogen", symbol: "H", atomicMass: 2.0, atomicRadius: 2.0, density: 2.0, eConfig: [1]))
    }
}

Upvotes: 0

Views: 291

Answers (2)

E.Coms
E.Coms

Reputation: 11531

The correct order in for should be :

     for ( val, index) in self.eConfig.enumerated() {...}

The original one is wrong:

     for (index, val) in self.eConfig.enumerated() {

Upvotes: 0

Hrabovskyi Oleksandr
Hrabovskyi Oleksandr

Reputation: 3275

Just run your app and you'll see crashes because of force unwrapping nil at the lines of code:

let block = orbitalBlock(from: index)!
var level = getRow(from: index)!

enter image description here

Upvotes: 1

Related Questions