swift - EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, sub code=0x0). error while making data structure -


i practicing array form of data structure swift.

i made class "student"

and there functions display() , delete()

however, application not working.

there error message

exc_bad_instruction (code=exc_i386_invop, sub code=0x0).

i think error "optional" problem.

here code.

class student {   var studentarray = [[string]?]() var numberofstudents : int =  10;     func display()  {      (var = 0; < numberofstudents   ; i++)      {          print("{");          (var j = 0; j < 2; j++)          {              print(studentarray[i]![j] + " ");          }          print("}");       }  }   func  delete( value : string)  {      var = 0      ( = 0; < numberofstudents   ; i++)       {            if (value == studentarray[i]![1])          {              break;          }       }       if (i == numberofstudents - 1 )      {          print("not found");       }      else      {          (var k = i; k < numberofstudents - 1  ; k++)          {               studentarray[k]![1] = studentarray[k+1]![1];             studentarray[k]![0] = studentarray[k+1]![0];  }           numberofstudents--;      }   }  }    var hello = student()   hello.studentarray = [["0","0ee"],["9","9ee", ]] hello.display() // have error @ point  hello.studentarray 

could explain me?

there several mistakes in code. actual error caused numberofstudents variable, hard coded 10, though array contains 2 elements. use studentarray.count in for loop, not 10. read swift manual. should not using optionals nor c-style for loops in example.

here's how it...

class student {                         // capitalise classes                                         // unnecessary whitespace removed     var studentarray: [[string]] = []   // no need optionals here     /*                   var numberofstudents : int =  10;   // var useless & wrong, no need semi-colon       */             func display() {         /* swift-ier way          student in studentarray {              print("{")              field in student {                  print(field + " ")              }              print("}")          }          however, using indexing:          */         in 0 ..< studentarray.count {             print("{")             j in 0 ..< studentarray[i].count { // don't *know* 2                 print(studentarray[i][j] + " ")    // don't need semi-colons unless want put multiple statements on same line             }             print("}")         }     }     /* func delete() not used in question, removed answer */ }  var hello = student()  hello.studentarray = [["0","0ee"], ["9","9ee", ]] // note spurious (but not wrong) comma hello.display()  hello.studentarray 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -