UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Plz help with NavigvationLink

Forums > SwiftUI

Hello guys, I am new to swiftUI and I meet the error "Updating took more than 5 seconds" when I add the following code

@State var studentclass = "default"
@State var studentNumber = "default"
@State var targetedArrayIndex : Int = 1
@State var isActive = false
.
.
.
.
.
NavigationLink(destination: Result(result : results[targetedArrayIndex]), isActive: $isActive) {

    Button(action: {

      if studentclass == "1A" && studentNumber == "01" {targetedArrayIndex = 0}
      if studentclass == "1A" && studentNumber == "02" {targetedArrayIndex = 1}
      if studentclass == "1A" && studentNumber == "03" {targetedArrayIndex = 2}
      .
      .
      .
      .
      self.isLinkActive = true

      }) {.....}

In the above code, I am trying to read the result of a particular student from a json file stored in an array. I created a dropdown menu that allow user to select studentclass and studentNumber. Hence, I use the IF/THEN statement to judge the array index of the targeted student. However, once I pass the targetedArrayObject to Result.swift (which is another view for displaying student's result), Xcode report an error.

For debuging, I tried to replace the varible targetedArrayObject by 0, i.e.

NavigationLink(destination: Result(result : results[0]), isActive: $isActive) {.....}

It works and shows the result sucessfully.

So I get confused of why I get an error in the first case.

2      

A couple of points I noticed.

  1. You have initialized targetedArrayIndex to 1.

This would access the second element of an array. To use the first element as the default, the index should be 0. Arrays start counting at 0, not 1.

@State var targetedArrayIndex : Int = 0
  1. You have a lot of 'if' statements in the button. In your code each time the studentclass is evaluated, and on the following line it is evaluated again, and so on. This means the app is repeating the evalution of studentclass many times for the same condition.

Depending on the relationship between studentclass and studentnumber can the targetArrayIndex be a computed result (using a formula)? This would be the quickest operating solution.

If a computed result is not possible for your data model, it is still possible to save processing time by using a switch statement

NavigationLink(destination: Result(result : results[targetedArrayIndex]), isActive: $isActive) {

    Button(action: {

          self.isLinkActive = true

          switch studentclass {

          case "1A":
              if studentNumber == "01" {targetedArrayIndex = 0}
              if studentNumber == "02" {targetedArrayIndex = 1}
              if studentNumber == "03" {targetedArrayIndex = 2}
              .
              .
          case "< next case >":
              if studentNumber == "xx" {targetedArrayIndex = a}
              if studentNumber == "yy" {targetedArrayIndex = b}
              if studentNumber == "zz" {targetedArrayIndex = c}
              .
              .

          default:
              < perform the default action >
          }

.
.
.

2      

Thank you so much~~ I finally fix the error. ^o^

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.