Education

RSS for tag

Teach students of all ages to code in Swift using a variety of resources in your curriculum.

Posts under Education tag

108 Posts

Post

Replies

Boosts

Views

Activity

ReadLine() - Very Common But Unsolvable Error "Unexpectedly found nil while unwrapping an Optional value"
Something wrong with Readline. I can't use readLine()!)! My code is really simple: var aYear = Int(readLine()!)! func isLeap(year: Int) {       var leap = "NO"       //IF divisible by 4 with no remainders.   if year % 4 == 0 {     leap = "YES"     //Is leap year, unless:   }   if year % 100 == 0 {     leap = "NO"     //Is not leap year, unless:   }   if year % 400 == 0 {     leap = "YES"     //Is leap year.   }   print(leap)   } //Don't change this isLeap(year: aYear) This error keeps coming up. I can't figure out how to fix this error: "__lldb_expr_122/readline .playground:1: Fatal error: Unexpectedly found nil while unwrapping an Optional value Playground execution failed: error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x18f494588). The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation." It is really strange because in online compiler this simple code works just fine. PLEASE HELP ME. I CAN'T FIND THE SOLUTION. It's kinda internal xcode error or what...
3
0
2.1k
Mar ’22
My view moves down and down with every opening of that view.
This is the content view which moves down on the screen as I open it from different view. when I click top left button to open menu. The menu When I click the Home option from menu whole view moves down on screen. The whole view gets moved down on screen each time I open it. //the content view import SwiftUI struct ContentView: View {     var body: some View {                       NavigationView{                               ScrollView{                      Text("Men")                          .font(.system(size: 30, weight:.bold))                      .padding(.trailing, 320.0)                  ScrollView(.horizontal){                      HStack{                          NavigationLink(destination:mentshirt()){                          custom(content:Image("mentshirt"),over:"T-shirt",col: .black)                          }                          NavigationLink(destination:menjeans()){                          custom(content:Image("menjeans"),over:"Jeans",col: .black)                          }                          NavigationLink(destination:menjacket()){                          custom(content:Image("menjacket"),over: "Jacket",col: .black)                          }                      }                  }                      Text("Women")                          .font(.system(size: 30,weight: .bold))                          .padding(.trailing,280.0)                  ScrollView(.horizontal){                      HStack{                          NavigationLink(destination:womentshirt()){                          custom(content:Image("womentshirt"),over:"T-shirt",col: .black)                          }                          NavigationLink(destination:womenjeans()){                          custom(content:Image("womenjeans"),over:"Jeans",col: .black)                          }                          NavigationLink(destination:womenjacket()){                          custom(content:Image("womenjacket"),over:"Jacket",col: .black)                          }                      }                  }                      Text("Kids")                          .font(.system(size: 30,weight: .bold))                          .padding(.trailing,320.0)                  ScrollView(.horizontal){                      HStack{                          NavigationLink(destination:kidtshirt()){                              custom(content:Image("kidtshirt"),over:"T-shirt",col:.black)                      }                          NavigationLink(destination:kidjeans()){                          custom(content:Image("kidjeans"),over:"Jeans",col: .black)                          }                              NavigationLink(destination:kidjacket()){                          custom(content:Image("kidjacket"),over:"Jacket",col:.black)                              }                      }                  }                  }                  .navigationBarBackButtonHidden(true)                  .navigationBarItems(leading:                                          NavigationLink(                                          destination: clothlist()){                                                  navcustom(content:                                                             Image(systemName: "line.horizontal.3"),col: .white)                          },                      trailing:                              HStack{                      Image("logo")                          .resizable()                          .clipShape(Circle())                          .shadow(color:.white,radius: 10)                          .frame(width: 30, height: 30, alignment: .center)                              Spacer(minLength: 80)                          NavigationLink(destination: cart()){                              navcustom(content:                                         Image(systemName: "cart"),col: .white)                          }                          Spacer(minLength: 15)                              NavigationLink(destination: login()){                                  navcustom(content:Image(systemName: "person.crop.circle.fill"), col: .white)                              }                          }                  )              }.navigationBarBackButtonHidden(true)             .ignoresSafeArea()         } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         Group {               ContentView()                 .environment(\.colorScheme, .dark)         }     } } func custom(content:Image,over:String,col:Color) -> some View {     content         .resizable()         .overlay(Text(over).font(.system(size: 20,weight: .bold)).foregroundColor(col),alignment: .bottomLeading)         .frame(width: 400, height: 500, alignment: .center)         .padding(5)                        } func navcustom(content:Image,col:Color)-> some View {     content         .resizable()         .frame(width: 30, height: 30, alignment: .center)         .foregroundColor(col) } //the menu view from which I open content view import SwiftUI struct clothlist: View {     var body: some View {         List         {             NavigationLink(destination:ContentView())             {             Text(Image(systemName: "house"))+Text(" Home")             }             Text(Image(systemName: "person"))+Text(" Men")             NavigationLink(destination:menjeans())             {             Text("Jeans")             }             NavigationLink(destination:menjacket())             {             Text("Jacket")             }             NavigationLink(destination:mentshirt())             {             Text("Tshirt")             }             Text(Image(systemName: "person"))+Text(" Women")             NavigationLink(destination:womenjeans())             {             Text("Jeans")             }             NavigationLink(destination:womenjacket())             {             Text("Jacket")             }             NavigationLink(destination:womentshirt())             {             Text("Tshirt")             }             Group{             Text(Image(systemName: "person"))+Text(" Kids")             NavigationLink(destination:kidjeans())             {             Text("Jeans")             }             NavigationLink(destination:kidjacket())             {             Text("Jacket")             }             NavigationLink(destination:kidtshirt())             {             Text("Tshirt")             }             }         }         .navigationBarBackButtonHidden(true)     } } struct clothlist_Previews: PreviewProvider {     static var previews: some View {         clothlist()     } } how to stop the view from moving down on screen on every open?
0
0
1.5k
Mar ’22
Ive created a login view but as soon as I leave that view all the textfield and password field is empty.
I want the details entered in textfield and password should stay even after changing the view or closing the app and the person should be logged in even after he closes the app, Can anyone help me out? import SwiftUI import Firebase struct login: View {     @State var chec:String=""     @State var email:String=""     @State var navigated = false     @State var loggedin:Bool=false     @State var pass:String=""     @State private var secured:Bool=true     var body: some View     {                 VStack                 {                     if(loggedin==true)                     {                     Image("logo")                         .resizable()                         .clipShape(Circle())                         .shadow(color:.green,radius: 10)                         .frame(width: 200, height: 200, alignment: .center)                     }                     else                     {                         Image("logo")                             .resizable()                             .clipShape(Circle())                             .shadow(color:.red,radius: 10)                             .frame(width: 200, height: 200, alignment: .center)                     }                     Spacer(minLength: 50.0)                     Text(chec)                         .foregroundColor(.red)                     HStack                     {                         Text("E-mail:")                         TextField("", text:$email)                             .foregroundColor(.white)                             .frame(width: 275)                             .textFieldStyle(.roundedBorder)                             .autocapitalization(.none)                             .disableAutocorrection(true)                     }                         HStack                         {                         Text("Password:")                             if secured                             {                                 SecureField("",text:$pass)                                     .background(Color.black)                                     .foregroundColor(.white)                                     .textFieldStyle(.roundedBorder)                                     .autocapitalization(.none)                                     .disableAutocorrection(true)                             }                             else                             {                                 TextField("",text: $pass)                                     .background(Color.black)                                     .foregroundColor(.white)                                     .textFieldStyle(.roundedBorder)                                     .autocapitalization(.none)                                     .disableAutocorrection(true)                             }                             Button(action: {self.secured.toggle()})                             {                                 if secured                                 {                                     Image(systemName:"eye.slash")                                 }                                 else                                 {                                     Image(systemName:"eye")                                 }                             }.buttonStyle(BorderlessButtonStyle())                         }                     VStack{                         Button(action: {                             if(email==""&&pass=="")                             {                                 chec="Enter E-mail and Password"                             }                             else if(pass=="")                             {                                 chec="Enter Password"                             }                             else if(email=="")                             {                                 chec="Enter E-mail"                             }                             guard !email.isEmpty,!pass.isEmpty else                         {                             return                         }                             loginfunc()                         }){                             Text("Login")                         }                         NavigationLink(destination: signup(),label:{Text("Not Yet signed? Create new account")})                             .padding(.top, 3.0)                     }                     .padding()                     Spacer(minLength: 400)                 }                      }     func loginfunc(){         Auth.auth().signIn(withEmail: email, password: pass) { result, error in             if error != nil{                 print(error?.localizedDescription ?? "")                 loggedin=false                 chec="E-mail or Password wrong!"             }else {                 print("success")                 loggedin=true             }             if(loggedin==true)             {                 chec=""                 print("wow so nice to get work done")             }         }     } } //} struct ButtonView: View {     var buttext:String     var body: some View {         Text(buttext)         .frame(width: 200, height: 100, alignment: .center)         .background(Color.black)         .foregroundColor(Color.white)     } } struct login_Previews: PreviewProvider {     static var previews: some View {         login()             .environment(\.colorScheme, .dark)     } }
1
0
1.2k
Mar ’22
I tried using multilinetextalignment but my text is not aligning on left of iPhone screen, what is the reason for it?
import SwiftUI struct lastview: View {     var img:UIImage     var texty:String     var body: some View {         ScrollView(.vertical){         Image(uiImage: img)             .resizable()             .frame(width: 500, height: 500, alignment: .center)             Text(texty)                 .multilineTextAlignment(.leading)                          }     } }
1
0
1.3k
Mar ’22
I used array to print image. but I also want to print text with the images using array how can I use it ,like I want to use same tray but want to print different text with their images using the same array like 2D array.
import SwiftUI struct menjeans: View {     var menje:[UIImage] =         [UIImage(named:"IMG_9917")!,         UIImage(named:"IMG_9920")!,         UIImage(named:"IMG_9923")!]     let layout=[GridItem(.flexible()),GridItem(.flexible())]     var body: some View     {         NavigationView         {             ScrollView(.vertical)             {             LazyVGrid(columns: layout,spacing: 2)                 {                 ForEach(menje, id: .self)                     {                     imager in                         maincustom(content: Image(uiImage:imager), text: "df")                     }                     .navigationBarTitle("Men")                 }             }         }     } } struct menjeans_Previews: PreviewProvider {     static var previews: some View {         menjeans()     } }
0
0
1.3k
Feb ’22
"code in Objective-C to maintain"
I am learning Swift and when I was casually browsing for job openings to see what the employers expect one thing caught my eyes. It says something like, In addition to Swift, "code in Objective-C to maintain" What does that mean? If I learn Swift and have a good understanding, will learning and using Obj-C be easier? Is it a common requirement for iOS/macOS developers? Appreciate your time!
1
0
765
Jan ’22
ReadLine() - Very Common But Unsolvable Error "Unexpectedly found nil while unwrapping an Optional value"
Something wrong with Readline. I can't use readLine()!)! My code is really simple: var aYear = Int(readLine()!)! func isLeap(year: Int) {       var leap = "NO"       //IF divisible by 4 with no remainders.   if year % 4 == 0 {     leap = "YES"     //Is leap year, unless:   }   if year % 100 == 0 {     leap = "NO"     //Is not leap year, unless:   }   if year % 400 == 0 {     leap = "YES"     //Is leap year.   }   print(leap)   } //Don't change this isLeap(year: aYear) This error keeps coming up. I can't figure out how to fix this error: "__lldb_expr_122/readline .playground:1: Fatal error: Unexpectedly found nil while unwrapping an Optional value Playground execution failed: error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x18f494588). The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation." It is really strange because in online compiler this simple code works just fine. PLEASE HELP ME. I CAN'T FIND THE SOLUTION. It's kinda internal xcode error or what...
Replies
3
Boosts
0
Views
2.1k
Activity
Mar ’22
My view moves down and down with every opening of that view.
This is the content view which moves down on the screen as I open it from different view. when I click top left button to open menu. The menu When I click the Home option from menu whole view moves down on screen. The whole view gets moved down on screen each time I open it. //the content view import SwiftUI struct ContentView: View {     var body: some View {                       NavigationView{                               ScrollView{                      Text("Men")                          .font(.system(size: 30, weight:.bold))                      .padding(.trailing, 320.0)                  ScrollView(.horizontal){                      HStack{                          NavigationLink(destination:mentshirt()){                          custom(content:Image("mentshirt"),over:"T-shirt",col: .black)                          }                          NavigationLink(destination:menjeans()){                          custom(content:Image("menjeans"),over:"Jeans",col: .black)                          }                          NavigationLink(destination:menjacket()){                          custom(content:Image("menjacket"),over: "Jacket",col: .black)                          }                      }                  }                      Text("Women")                          .font(.system(size: 30,weight: .bold))                          .padding(.trailing,280.0)                  ScrollView(.horizontal){                      HStack{                          NavigationLink(destination:womentshirt()){                          custom(content:Image("womentshirt"),over:"T-shirt",col: .black)                          }                          NavigationLink(destination:womenjeans()){                          custom(content:Image("womenjeans"),over:"Jeans",col: .black)                          }                          NavigationLink(destination:womenjacket()){                          custom(content:Image("womenjacket"),over:"Jacket",col: .black)                          }                      }                  }                      Text("Kids")                          .font(.system(size: 30,weight: .bold))                          .padding(.trailing,320.0)                  ScrollView(.horizontal){                      HStack{                          NavigationLink(destination:kidtshirt()){                              custom(content:Image("kidtshirt"),over:"T-shirt",col:.black)                      }                          NavigationLink(destination:kidjeans()){                          custom(content:Image("kidjeans"),over:"Jeans",col: .black)                          }                              NavigationLink(destination:kidjacket()){                          custom(content:Image("kidjacket"),over:"Jacket",col:.black)                              }                      }                  }                  }                  .navigationBarBackButtonHidden(true)                  .navigationBarItems(leading:                                          NavigationLink(                                          destination: clothlist()){                                                  navcustom(content:                                                             Image(systemName: "line.horizontal.3"),col: .white)                          },                      trailing:                              HStack{                      Image("logo")                          .resizable()                          .clipShape(Circle())                          .shadow(color:.white,radius: 10)                          .frame(width: 30, height: 30, alignment: .center)                              Spacer(minLength: 80)                          NavigationLink(destination: cart()){                              navcustom(content:                                         Image(systemName: "cart"),col: .white)                          }                          Spacer(minLength: 15)                              NavigationLink(destination: login()){                                  navcustom(content:Image(systemName: "person.crop.circle.fill"), col: .white)                              }                          }                  )              }.navigationBarBackButtonHidden(true)             .ignoresSafeArea()         } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         Group {               ContentView()                 .environment(\.colorScheme, .dark)         }     } } func custom(content:Image,over:String,col:Color) -> some View {     content         .resizable()         .overlay(Text(over).font(.system(size: 20,weight: .bold)).foregroundColor(col),alignment: .bottomLeading)         .frame(width: 400, height: 500, alignment: .center)         .padding(5)                        } func navcustom(content:Image,col:Color)-> some View {     content         .resizable()         .frame(width: 30, height: 30, alignment: .center)         .foregroundColor(col) } //the menu view from which I open content view import SwiftUI struct clothlist: View {     var body: some View {         List         {             NavigationLink(destination:ContentView())             {             Text(Image(systemName: "house"))+Text(" Home")             }             Text(Image(systemName: "person"))+Text(" Men")             NavigationLink(destination:menjeans())             {             Text("Jeans")             }             NavigationLink(destination:menjacket())             {             Text("Jacket")             }             NavigationLink(destination:mentshirt())             {             Text("Tshirt")             }             Text(Image(systemName: "person"))+Text(" Women")             NavigationLink(destination:womenjeans())             {             Text("Jeans")             }             NavigationLink(destination:womenjacket())             {             Text("Jacket")             }             NavigationLink(destination:womentshirt())             {             Text("Tshirt")             }             Group{             Text(Image(systemName: "person"))+Text(" Kids")             NavigationLink(destination:kidjeans())             {             Text("Jeans")             }             NavigationLink(destination:kidjacket())             {             Text("Jacket")             }             NavigationLink(destination:kidtshirt())             {             Text("Tshirt")             }             }         }         .navigationBarBackButtonHidden(true)     } } struct clothlist_Previews: PreviewProvider {     static var previews: some View {         clothlist()     } } how to stop the view from moving down on screen on every open?
Replies
0
Boosts
0
Views
1.5k
Activity
Mar ’22
Resource recommendations for beginners?
Hi friends. I'm new around here and just migrated from Windows to macOS. I started working on iOS development with Swift. So if you have any study resource suggestions for beginners, can you share them? I am open to all resources such as documents, websites, videos.
Replies
0
Boosts
0
Views
631
Activity
Mar ’22
Ive created a login view but as soon as I leave that view all the textfield and password field is empty.
I want the details entered in textfield and password should stay even after changing the view or closing the app and the person should be logged in even after he closes the app, Can anyone help me out? import SwiftUI import Firebase struct login: View {     @State var chec:String=""     @State var email:String=""     @State var navigated = false     @State var loggedin:Bool=false     @State var pass:String=""     @State private var secured:Bool=true     var body: some View     {                 VStack                 {                     if(loggedin==true)                     {                     Image("logo")                         .resizable()                         .clipShape(Circle())                         .shadow(color:.green,radius: 10)                         .frame(width: 200, height: 200, alignment: .center)                     }                     else                     {                         Image("logo")                             .resizable()                             .clipShape(Circle())                             .shadow(color:.red,radius: 10)                             .frame(width: 200, height: 200, alignment: .center)                     }                     Spacer(minLength: 50.0)                     Text(chec)                         .foregroundColor(.red)                     HStack                     {                         Text("E-mail:")                         TextField("", text:$email)                             .foregroundColor(.white)                             .frame(width: 275)                             .textFieldStyle(.roundedBorder)                             .autocapitalization(.none)                             .disableAutocorrection(true)                     }                         HStack                         {                         Text("Password:")                             if secured                             {                                 SecureField("",text:$pass)                                     .background(Color.black)                                     .foregroundColor(.white)                                     .textFieldStyle(.roundedBorder)                                     .autocapitalization(.none)                                     .disableAutocorrection(true)                             }                             else                             {                                 TextField("",text: $pass)                                     .background(Color.black)                                     .foregroundColor(.white)                                     .textFieldStyle(.roundedBorder)                                     .autocapitalization(.none)                                     .disableAutocorrection(true)                             }                             Button(action: {self.secured.toggle()})                             {                                 if secured                                 {                                     Image(systemName:"eye.slash")                                 }                                 else                                 {                                     Image(systemName:"eye")                                 }                             }.buttonStyle(BorderlessButtonStyle())                         }                     VStack{                         Button(action: {                             if(email==""&&pass=="")                             {                                 chec="Enter E-mail and Password"                             }                             else if(pass=="")                             {                                 chec="Enter Password"                             }                             else if(email=="")                             {                                 chec="Enter E-mail"                             }                             guard !email.isEmpty,!pass.isEmpty else                         {                             return                         }                             loginfunc()                         }){                             Text("Login")                         }                         NavigationLink(destination: signup(),label:{Text("Not Yet signed? Create new account")})                             .padding(.top, 3.0)                     }                     .padding()                     Spacer(minLength: 400)                 }                      }     func loginfunc(){         Auth.auth().signIn(withEmail: email, password: pass) { result, error in             if error != nil{                 print(error?.localizedDescription ?? "")                 loggedin=false                 chec="E-mail or Password wrong!"             }else {                 print("success")                 loggedin=true             }             if(loggedin==true)             {                 chec=""                 print("wow so nice to get work done")             }         }     } } //} struct ButtonView: View {     var buttext:String     var body: some View {         Text(buttext)         .frame(width: 200, height: 100, alignment: .center)         .background(Color.black)         .foregroundColor(Color.white)     } } struct login_Previews: PreviewProvider {     static var previews: some View {         login()             .environment(\.colorScheme, .dark)     } }
Replies
1
Boosts
0
Views
1.2k
Activity
Mar ’22
I tried using multilinetextalignment but my text is not aligning on left of iPhone screen, what is the reason for it?
import SwiftUI struct lastview: View {     var img:UIImage     var texty:String     var body: some View {         ScrollView(.vertical){         Image(uiImage: img)             .resizable()             .frame(width: 500, height: 500, alignment: .center)             Text(texty)                 .multilineTextAlignment(.leading)                          }     } }
Replies
1
Boosts
0
Views
1.3k
Activity
Mar ’22
I used array to print image. but I also want to print text with the images using array how can I use it ,like I want to use same tray but want to print different text with their images using the same array like 2D array.
import SwiftUI struct menjeans: View {     var menje:[UIImage] =         [UIImage(named:"IMG_9917")!,         UIImage(named:"IMG_9920")!,         UIImage(named:"IMG_9923")!]     let layout=[GridItem(.flexible()),GridItem(.flexible())]     var body: some View     {         NavigationView         {             ScrollView(.vertical)             {             LazyVGrid(columns: layout,spacing: 2)                 {                 ForEach(menje, id: .self)                     {                     imager in                         maincustom(content: Image(uiImage:imager), text: "df")                     }                     .navigationBarTitle("Men")                 }             }         }     } } struct menjeans_Previews: PreviewProvider {     static var previews: some View {         menjeans()     } }
Replies
0
Boosts
0
Views
1.3k
Activity
Feb ’22
"code in Objective-C to maintain"
I am learning Swift and when I was casually browsing for job openings to see what the employers expect one thing caught my eyes. It says something like, In addition to Swift, "code in Objective-C to maintain" What does that mean? If I learn Swift and have a good understanding, will learning and using Obj-C be easier? Is it a common requirement for iOS/macOS developers? Appreciate your time!
Replies
1
Boosts
0
Views
765
Activity
Jan ’22