NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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.

   

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 >
          }

.
.
.

   

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

   

Hacking with Swift is sponsored by Emerge

SPONSORED Emerge helps iOS devs write better, smaller apps by profiling binary size on each pull request and surfacing insights and suggestions. Companies using Emerge have reduced the size of their apps by up to 50% in just the first day. Built by a team with years of experience reducing app size at Airbnb.

Set up a demo!

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

Reply to this topic…

You need to create an account or log in to reply.

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.