Post

Replies

Boosts

Views

Activity

why it says "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"?
I have IOS projets, it work but suddenly crash and throw error like "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" Why? import Foundation import AVFoundation struct Video : Identifiable {   var id = UUID()   var player : AVPlayer   var user: User } struct User: Identifiable {   var id = UUID()   let userName: String   let userImage: String } struct MockData {   let videos: [Video] = [     Video(player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "reel_1", ofType: "mp4")!)),        user: User(userName: "cristiano", userImage: "user_9")),     Video(player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "reel_2", ofType: "mp4")!)),        user: User(userName: "mann_daar", userImage: "user_3")), ] }
1
0
1.2k
Sep ’21
Cannot convert value of type 'AnyViewModel<BookListState, Never>.Type' to expected argument type 'AnyViewModel<BookListState, Never>'
I want to put BookListView() on the content view but it throw error like "Cannot convert value of type 'AnyViewModel<BookListState, Never>.Type' to expected argument type 'AnyViewModel<BookListState, Never>'" Any idea? BookListView: import SwiftUI struct BookListState {   var service: BookService   var books: [Book] } struct BookListView: View {   @ObservedObject var viewModel: AnyViewModel<BookListState, Never>   var body: some View {     NavigationView {       ScrollView {       VStack(alignment: .leading){       ForEach(viewModel.state.books) { book in         NavigationLink(destination: NavigationLazyView(BookDetailView(service: self.viewModel.state.service, bookId: book.id))) {           BookRow(book: book)         }       }             }       }     }   } } struct BookListView_Previews: PreviewProvider {   static var previews: some View {     let viewModel = AnyViewModel(BookListViewModel(service: MockBookService()))     return BookListView(viewModel: viewModel)   } } ContentView: import SwiftUI struct ContentView: View {   var body: some View {    BookListView(viewModel: viewModel)   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
1
0
411
Oct ’21
Why NavigationLink not work for text button in SwiftUI?
I have text button, and I want to use navigation link in this project, but it is not work when I click the button? Any idea?   @State var isLink = false       var body: some View {     GeometryReader { geometry in         ZStack {    NavigationLink(destination: SecondView(), isActive: $isLink) {                 Button() {                   self.isLink = true                 } label: {                                       Text("Button")                     .padding()                     .frame(width: 250, height: 50, alignment: .center)                     .foregroundColor(Color.white)                     .background(Color("color"))                     .clipShape(Capsule())                  }}}}
1
0
491
Feb ’22
why it throw an error as "'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead"in SwiftUI 3.0?
I have a project, after update it to swiftUI 3.0, it is throw an error for    .animation(.spring()) as animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead, any idea?  @Binding var display: Bool   private var background: some View {   Color.black    .fillParent()    .opacity(0.6)    .animation(.spring())  }
1
0
691
Feb ’22
Why it is throw an error as "'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead" in SwiftUI?
I have project in SwiftUI 2.0 but when I update to SwiftUI 3.0 it is throw an error for windows as a windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead any idea?     .padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top)
0
0
668
Feb ’22
Why I am not success to pass data from one view to another view in SwiftUI?
I have a problem when try to pass data from one view to another view, it is throw an error for   ImageDetails() as a "Missing argument for parameter 'data' in call" I do not know what I missed? struct ImageModel: Identifiable, Hashable {   var id = UUID().uuidString   var name: String   } var datas = [       ImageModel(name: "davis"), ] struct ImageRowView: View {   var data: ImageModel   var body: some View {            NavigationLink(destination: ImageDetailsView(data: ImageModel)){                       HStack{} }}} struct ImageDetailsView: View {      var body: some View {           ImageDetails()   } } struct ImageDetailsView_Previews: PreviewProvider {   static var previews: some View {           ImageDetailsView()         } } struct ImageDetails : View {      var data: ImageModel   var body: some View{   VStack{             Text(data.name)                      } }
1
0
435
Feb ’22
How can I add costom emoji to example chat app?
I have simple chat app, and I want to use simple emoji in this chat app, my emoji is located in EmojiView an when I click the emoji button, I want to add in live chat, but I do not know how I will forward for it, I was look at many resources but I did not find any example on internet, is it possible to do it? import SwiftUI struct MessageDetailsView: View {   var body: some View {     HomeMessageDetails()}} struct MessageDetailsView_Previews: PreviewProvider {      static var previews: some View {     MessageDetailsView()      }} struct HomeMessageDetails : View {   @State var message = ""   @StateObject var allMessages = Messages()   @State private var emojiData = false   var body: some View{           ZStack {       VStack{         ScrollView(.vertical, showsIndicators: false, content: {           ScrollViewReader{reader in             VStack{               ForEach(allMessages.messages){message in                 ChatBubble(message: message)               }                .onChange(of: allMessages.messages) { (value) in                 if value.last!.chatMessages{                   reader.scrollTo(value.last?.id)}}}             .padding([.horizontal,.bottom])}})         HStack {             HStack{             TextField("Message", text: self.$message)             Button(action: {             emojiData = true             }, label: {               Image("emoji")             }) .sheet(isPresented: $emojiData) {           EmojiView()         } }           .padding(.vertical, 10)           .padding(.horizontal)                Button(action: {             allMessages.messages.append(Message(id: Date(), message: message, chatMessages: true))                           message = ""           }, label: {             Image("reply")              })   }         .padding(.horizontal)       }} }} struct ChatBubble : View {   var message : Message   var body: some View{     HStack(alignment: .top,spacing: 10){       if message.chatMessages{         Text(message.message)           .foregroundColor(Color("black))         }             else{         Text(message.message)             .foregroundColor(.white)  }}     .id(message.id)}} struct Message : Identifiable,Equatable{   var id : Date   var message : String   var chatMessages : Bool    } class Messages : ObservableObject{   @Published var messages : [Message] = []   init() {           let strings = ["Hii","Hello !!!!"]     for i in 0..<strings.count{       messages.append(Message(id: Date(), message: strings[i], chatMessages: i % 2 == 0 ? true : false))}}           func writeMessage(id: Date,message: String,chatMessages: Bool){           messages.append(Message(id: id, message: message, chatMessages: chatMessages))}} struct EmojiView: View {   var body: some View {  Button(action: {             }, label: {               Image("smile_emoji")                           }) }}
0
0
398
Feb ’22
Why I am not success to pass data from one view to another view for many data in SwiftUI?
I ask this question before, it was answered but it is not work for many data, like when I click the any list items, all details pass name of "david", I want to pass data of user which I clicked, where I missed here? struct ImageModel: Identifiable, Hashable {   var id = UUID().uuidString   var name: String   } var datas = [       ImageModel(name: "davis"),  ImageModel(name: "carry"),   ImageModel(name: "maria"), ] struct ImageRowView: View {   var data: ImageModel   var body: some View {            NavigationLink(destination: ImageDetailsView(data: ImageModel)){                       HStack{} }}} struct ImageDetailsView: View {      var body: some View {          ImageDetails(data:  ImageModel(name: "davis"))   } } struct ImageDetailsView_Previews: PreviewProvider {   static var previews: some View {           ImageDetailsView()         } } struct ImageDetails : View {      var data: ImageModel   var body: some View{   VStack{             Text(data.name)                      } }
1
0
477
Feb ’22
How can use model in view shortly?
I have small restaurans data in content view, but I want to use this data shortly inside of the homeview, I am typing   to use  @State var restaurants: [ Restaurant] but in other view I have HomeView, it is throw an error like     Cannot convert value of type '[Restaurant].Type' to expected argument type '[Restaurant]' for    HomeView( restaurants: [Restaurant]) line of code, I do not know what I missed? Any idea? struct HomeView: View { @State var restaurants = [ Restaurant(name: "Cafe Deadend", image: "cafedeadend"), Restaurant(name: "Homei", image: "homei"), Restaurant(name: "Teakha", image: "teakha"), Restaurant(name: "Cafe Loisl", image: "cafeloisl"), ] }
0
0
331
Feb ’22
why it is throw an error as a Value of type 'ContentView' has no member 'restaurants' in SwiftUI?
I have example of swiftUI project, and it is throw an error as a "Value of type 'ContentView' has no member 'restaurants'" for    self.restaurants.remove(atOffsets: indexSet) } line of code, I do not know what I missed, any idea? import SwiftUI struct ContentView: View {          @State private var selectedRestaurant: Restaurant?       var body: some View {     List {       ForEach(restaurants) { restaurant in         BasicImageRow(restaurant: restaurant)           .contextMenu {                           Button(action: {               // mark the selected restaurant as check-in               self.checkIn(item: restaurant)             }) {               HStack {                 Text("Check-in")                 Image(systemName: "checkmark.seal.fill")               }             }                           Button(action: {               // delete the selected restaurant               self.delete(item: restaurant)             }) {               HStack {                 Text("Delete")                 Image(systemName: "trash")               }             }                           Button(action: {               // mark the selected restaurant as favorite               self.setFavorite(item: restaurant)             }) {               HStack {                 Text("Favorite")                 Image(systemName: "star")               }             }           }           .onTapGesture {             self.selectedRestaurant = restaurant           }           .actionSheet(item: self.$selectedRestaurant) { restaurant in                           ActionSheet(title: Text("What do you want to do"), message: nil, buttons: [                               .default(Text("Mark as Favorite"), action: {                 self.setFavorite(item: restaurant)               }),                               .destructive(Text("Delete"), action: {                 self.delete(item: restaurant)               }),                               .cancel()             ])           }       }       .onDelete { (indexSet) in         self.restaurants.remove(atOffsets: indexSet)       }     }   }       private func delete(item restaurant: Restaurant) {     if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {       self.restaurants.remove(at: index)     }   }       private func setFavorite(item restaurant: Restaurant) {     if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {       self.restaurants[index].isFavorite.toggle()     }   }       private func checkIn(item restaurant: Restaurant) {     if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {       self.restaurants[index].isCheckIn.toggle()     }   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } } struct BasicImageRow: View {   var restaurant: Restaurant       var body: some View {     HStack {       Image(restaurant.image)         .resizable()         .frame(width: 40, height: 40)         .cornerRadius(5)       Text(restaurant.name)               if restaurant.isCheckIn {         Image(systemName: "checkmark.seal.fill")           .foregroundColor(.red)       }               if restaurant.isFavorite {         Spacer()                   Image(systemName: "star.fill")           .foregroundColor(.yellow)       }     }   } } model: struct Restaurant: Identifiable {   var id = UUID()   var name: String   var image: String   var isFavorite: Bool = false   var isCheckIn: Bool = false } var restaurants = [ Restaurant(name: "Cafe Deadend", image: "cafedeadend"),        Restaurant(name: "Homei", image: "homei"),        Restaurant(name: "Teakha", image: "teakha"),        Restaurant(name: "Cafe Loisl", image: "cafeloisl"), ]
1
0
1.8k
Feb ’22
Why list items not able to delete in SwiftUI?
I have a simple app in SwiftUI, and I try to delete list items in app , project is working, but still list items not able to delete, I do not know what I did not put in my codes, any idea will be appreciated. struct MyView: View { @State private var selectedUsers: MyModel? var body: some View { ScrollView(.vertical, showsIndicators: false, content: { VStack(content: { ForEach(datas){ data in MyRowView(data: data) .contextMenu { Button(action: { self.delete(item: data) }) { Text("delete") } } .onTapGesture { selectedUsers = data } } .onDelete { (indexSet) in selectedUsers.remove(atOffsets: indexSet) }}) })} private func delete(item data: MyModel) { if let index = datas.firstIndex(where: { $0.id == data.id }) { datas.remove(at: index) } }} model: struct MyModel: Identifiable, Hashable, Codable { var id = UUID().uuidString var name: String } var datas = [ MyModel(name: "david"), MyModel(name: "marry"), ]
3
0
2k
Feb ’22