Saturday, November 21, 2026
HomeiOS Developmentios - SwiftUI code too lengthy for the compiler to type-check it,...

ios – SwiftUI code too lengthy for the compiler to type-check it, however I can not seem to break it down into components

[ad_1]

I’m attempting to make a view iteratively from a JSON file fetched from a PHP URL. The view is made with a for every loop to create an data field for each completely different station within the JSON and likewise makes them a navigation hyperlink. Ideally, I’d faucet the field to get extra data on a particular tide station (all station information hasn’t been introduced on the view but).

Since I’m utilizing a for every loop, I characterize the present station quantity within the information array with i. To be able to get the identical information because the field I click on I exploit the identical i quantity for different related information.

My problem is that as I’m constructing every thing in the identical for every loop to maintain that quantity i within the scope, my code will get too lengthy for the compiler to verify. I heard that this might occur even when the code wasn’t too lengthy however merely for a typing mistake someplace, however I’ve but to discover a mistake that breaks the code and actually imagine it’s because of the size because it was working if I commented-out some components.

To resolve that I perceive I want to interrupt my code into completely different sections for the compiler to verify them individually in order to not begin the method each time.

Nonetheless, as I’m constructing every thing into the for every loop to have the i quantity within the scope, I can not make sub-views that use that quantity.

I’m not completely positive that is the query that will finest resolve my problem, however how can I move the for every loop parameter into one other view/operate/or one thing else?

I apologize for the code being very tough and never following good coding practices, if not only for SwiftUI, I’m fairly inexperienced with programming basically. I left the whole lot of the code to make certain to not go away any chance out.

import Basis
import SwiftUI
import Alamofire
import SwiftyJSON
import MapKit

public var array_tides_site_name = [String]()
public var array_tides_next_lw_time = [String]()
public var array_tides_next_lw_height = [Double]()
public var array_tides_next_hw_time = [String]()
public var array_tides_next_hw_height = [Double]()
public var array_tides_tidal_state = [String]()
public var array_tides_latitude = [Double]()
public var array_tides_longitude = [Double]()
public var array_tides_observed_height = [Double]()
public var array_tides_predicted_height = [Double]()
public var array_tides_surge = [Double]()

struct Previews_GeneralTides_Previews: PreviewProvider {
    static var previews: some View {
        GeneralTidesView()
    }
}

struct GeneralTidesView: View {
    
    var physique: some View {
            
            ScrollView(.vertical, showsIndicators: false) {
                
                VStack (alignment: .heart) {
                    
                    Spacer()
                    
                    Picture(systemName: "chart.xyaxis.line")
                        .font(.largeTitle)
                        .body(width: 300, peak: 300, alignment: .heart)
                    
                    Spacer()
                    
                    Textual content("Extra Stations")
                        .font(.title3)
                        .foregroundStyle(LinearGradient(colours: [.primary, .secondary], startPoint: .topLeading, endPoint: .bottomTrailing))
                        .body(width: screenWidth, peak: 40)
                        .background(.thickMaterial)
                    
                    Spacer()
                    
                    Divider()
                        .onAppear() {loadStationData()}
                    
                    Spacer()
                    
                    StationList()
                    
                }
                .navigationBarTitle("Tides")
                .navigationBarTitleDisplayMode(.inline)
        }
    }
}

func loadStationData(){
    
let generalTideUrl = "http://www.pla.co.uk/hydrographics/ajax/ltoverview.php"

AF.request(generalTideUrl, methodology: .get).responseJSON(){ (generalTideResponse) in
    swap generalTideResponse.outcome {
    case .success:
        //print(generaltideresponse.outcome)
        let generalTideResult = attempt? JSON(information: generalTideResponse.information!)
        //print(generaltideresult)
        //print(generalTideResult!["tides"])
        let generalTideArray = generalTideResult!["tides"]
        
        array_tides_site_name.removeAll()
        array_tides_next_lw_time.removeAll()
        array_tides_next_lw_height.removeAll()
        array_tides_next_hw_time.removeAll()
        array_tides_next_hw_height.removeAll()
        array_tides_tidal_state.removeAll()
        array_tides_latitude.removeAll()
        array_tides_longitude.removeAll()
        array_tides_observed_height.removeAll()
        array_tides_predicted_height.removeAll()
        array_tides_surge.removeAll()
        
        for i in generalTideArray.arrayValue {
            //print(i)
            
            let site_name = i["site_name"].stringValue
            array_tides_site_name.append(site_name)
            
            var next_lw_time = i["next_lw_time"].stringValue
            let lwRange = next_lw_time.startIndex..<next_lw_time.index(next_lw_time.startIndex, offsetBy: 11)
            next_lw_time.removeSubrange(lwRange)
            array_tides_next_lw_time.append(next_lw_time)
            
            let next_lw_height = i["next_lw_height"].doubleValue
            array_tides_next_lw_height.append(next_lw_height)
            
            var next_hw_time = i["next_hw_time"].stringValue
            let hwRange = next_hw_time.startIndex..<next_hw_time.index(next_hw_time.startIndex, offsetBy: 11)
            next_hw_time.removeSubrange(hwRange)
            array_tides_next_hw_time.append(next_hw_time)
            
            let next_hw_height = i["next_hw_height"].doubleValue
            array_tides_next_hw_height.append(next_hw_height)
            
            let tidal_state = i["tidal_state"].stringValue
            array_tides_tidal_state.append(tidal_state)
            
            let latitude = i["latitude"].doubleValue
            array_tides_latitude.append(latitude)
            
            let longitude = i["longitude"].doubleValue
            array_tides_longitude.append(longitude)
            
            let predictedHeight = i["predicted_height"].doubleValue
            array_tides_predicted_height.append(predictedHeight)
            
            let observedHeight = i["observed_height"].doubleValue
            array_tides_observed_height.append(observedHeight)
            
            let surge = i["surge_height"].doubleValue
            array_tides_surge.append(surge)
        }
        
        break
    case .failure:
        print(generalTideResponse.error!)
        break
    }
}.resume()
}


struct StationList: View {
    @State personal var mapRegion = MKCoordinateRegion(heart: CLLocationCoordinate2D(latitude: 51.5, longitude: -0.12), span: MKCoordinateSpan(latitudeDelta: 0.6, longitudeDelta: 0.6))

    
    
    var physique: some View {
        
        ForEach(0..<array_tides_site_name.depend) { i in
            
            NavigationLink(vacation spot: Group{
                
                VStack(alignment: .heart) {
                    stationNavLink()
                    Textual content(array_tides_site_name[i])
                        .font(.largeTitle)
                    
                    Map(coordinateRegion: $mapRegion, annotationItems: [Location(name: array_tides_site_name[i], coordinate: CLLocationCoordinate2D(latitude: array_tides_latitude[i], longitude: array_tides_longitude[i]))]) { location in
                        MapMarker(coordinate: location.coordinate)}
                        .body(width: screenWidth, peak: 250)
                    
                    
                    HStack {
                        
                        RoundedRectangle(cornerRadius: 10)
                            .foregroundStyle(.thinMaterial)
                            .body(width: 150, peak: 120)
                            .overlay {
                                
                                VStack(alignment: .main , spacing: 10) {
                                    
                                    Textual content("Subsequent Low Tide:")
                                    
                                    HStack {Textual content("Time: "); Textual content(array_tides_next_lw_time[i])}.foregroundColor(.secondary)
                                    
                                    HStack {Textual content("Peak: "); Textual content(array_tides_next_lw_height[i].description); Textual content("m")}.foregroundColor(.secondary)
                                    
                                     }
                            }
                        
                        RoundedRectangle(cornerRadius: 10)
                            .foregroundStyle(.thinMaterial)
                            .body(width: 150, peak: 120)
                            .overlay {
                                
                                VStack(alignment: .main, spacing: 10) {
                                    
                                    Textual content("Subsequent Excessive Tide:")
                                    
                                    HStack {Textual content("Time: "); Textual content(array_tides_next_hw_time[i])}.foregroundColor(.secondary)
                                    
                                    HStack {Textual content("Peak: "); Textual content(array_tides_next_hw_height[i].description); Textual content("m")}.foregroundColor(.secondary)
                                    
                                     }
                            }
                        
                        
                    }
                    
                    Textual content(array_tides_surge[i].description)
                    
                }
                
            }){
            
            ZStack {
                
                RoundedRectangle(cornerRadius: 8)
                    .strokeBorder(.white.opacity(0.3), lineWidth: 1)
                    .background(RoundedRectangle(cornerRadius: 8).fill(.thinMaterial))
                    .body(width: screenWidth - 40, peak: 80)
                    .overlay() {
                        
                        VStack(alignment: .main) {
                                                     
                            Spacer()
                            
                            Textual content(array_tides_site_name[i])
                                .padding(.main, 10)
                                .foregroundStyle(LinearGradient(colours: [.secondary, .secondary.opacity(0.8)], startPoint: .main, endPoint: .trailing))

                            Spacer()
                            
                            Divider()
                            
                            Spacer()
                            
                            Group {
                            HStack(){
                                Spacer()
                                Textual content("Excessive Water: ")
                                Textual content(array_tides_next_hw_time[i])
                                Spacer()
                                Textual content("Low Water: ")
                                Textual content(array_tides_next_lw_time[i])
                                Spacer()
                                
                                if array_tides_tidal_state[i] == "Flood" { Picture(systemName: "arrow.up").foregroundColor(.inexperienced) }
                                else { Picture(systemName: "arrow.down").foregroundColor(.pink) }
                                
                                Spacer()
                                }
                            }
                            
                            Spacer()
                            
                        }
                    }
                }
            }
        }
    }
}


var stationNavLink: some View {
    Textual content(array_tides_surge[currentStation])
}


struct Location: Identifiable {
    let id = UUID()
    let title: String
    let coordinate: CLLocationCoordinate2D
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments