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 making an attempt 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 info field for each completely different station within the JSON and likewise makes them a navigation hyperlink. Ideally, I might faucet the field to get extra info on a particular tide station (all station information hasn’t been offered on the view but).

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

My situation 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 examine. 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 really consider it’s as a result of size because it was working if I commented-out some components.

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

Nevertheless, 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 solely certain that is the query that may greatest clear up my situation, however how can I cross 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 everything of the code to make sure 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: .middle) {
                    
                    Spacer()
                    
                    Picture(systemName: "chart.xyaxis.line")
                        .font(.largeTitle)
                        .body(width: 300, top: 300, alignment: .middle)
                    
                    Spacer()
                    
                    Textual content("Extra Stations")
                        .font(.title3)
                        .foregroundStyle(LinearGradient(colours: [.primary, .secondary], startPoint: .topLeading, endPoint: .bottomTrailing))
                        .body(width: screenWidth, top: 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
    change generalTideResponse.end result {
    case .success:
        //print(generaltideresponse.end result)
        let generalTideResult = strive? 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 non-public var mapRegion = MKCoordinateRegion(middle: 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.rely) { i in
            
            NavigationLink(vacation spot: Group{
                
                VStack(alignment: .middle) {
                    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, top: 250)
                    
                    
                    HStack {
                        
                        RoundedRectangle(cornerRadius: 10)
                            .foregroundStyle(.thinMaterial)
                            .body(width: 150, top: 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("Top: "); Textual content(array_tides_next_lw_height[i].description); Textual content("m")}.foregroundColor(.secondary)
                                    
                                     }
                            }
                        
                        RoundedRectangle(cornerRadius: 10)
                            .foregroundStyle(.thinMaterial)
                            .body(width: 150, top: 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("Top: "); 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, top: 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(.purple) }
                                
                                Spacer()
                                }
                            }
                            
                            Spacer()
                            
                        }
                    }
                }
            }
        }
    }
}


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


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

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *