Please Help Me Fix This Issues

Here is my whole code, does someone knows how to fix it?

Answered by Claude31 in 741367022

Code (Part 2):

@available(macOS 10.15, *)
struct CallView: View {
    @StateObject var friendManager: FriendManager      // <<-- CHANGE HERE
    @State private var searchText: String = ""
    let username: String
    @available(macOS 10.15.0, *)
    var body: some View {
        VStack {
            // Search bar
            HStack {
                TextField("Search", text: $searchText)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                Button(action: searchForFriends) {
                    Image(systemName: "magnifyingglass")
                        .foregroundColor(.blue)
                }
            }
            .padding()
            // List of friends
            ScrollView {
                ForEach(friendManager.friends, id: \.self) { friend in       // <<-- CHANGE HERE
                    HStack {
                        Text(friend.username)
                            .font(.body)
                            .padding(.vertical, 8)
                        Spacer()
                        Button(action: {
                            // Call friend using call library
                        }) {
                            Image(systemName: "phone.fill")
                                .foregroundColor(.blue)
                                .padding(.vertical, 8)
                        }
                    }
                }
            }
        }
    }
    private func searchForFriends() {
        // Search for friends using API or local data
    }
}
struct MapView: View {
    @available(macOS 10.15.0, *)
    @State private var mapRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 43.6, longitude: 1.44), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))    // <<-- Set your region    
    var body: some View {
        Map(coordinateRegion: $mapRegion)
            .edgesIgnoringSafeArea(.all)
    }
}
struct Friend :Hashable {      // <<-- CHANGE HERE
    let username: String
}

struct Message : Hashable { // <<-- CHANGE HERE
    let text: String
    let sender: String
}

@available(macOS 10.15, *)
class FriendManager: ObservableObject {
    @Published var friends: [Friend] = []
    func addFriend(friend: Friend) {
        friends.append(friend)
    }
    func searchForFriends(searchText: String) -> [Friend] {
        // Search for friends using API or local data
        return []
    }
}
@available(macOS 10.15, *)
class ChatManager: ObservableObject {
    @Published var messages: [Message] = []
    func addMessage(_ message: Message) {      // <<-- CHANGE HERE
        messages.append(message)
    }
}

Please, Please, post also the code, not screen shots only.

  • First point: set target to MacOS 11.

Change here:

  • Second point: remove the private you have set to some properties (you don't show all code, so hard to be more specific).

import SwiftUI

@available(macOS 10.15, *)

struct AppView: View {

    @State private var phoneNumber: String = ""

    @State private var isLoggedIn: Bool = false

    @State private var username: String = ""

    @State private var selectedView: Int = 0

    @State private var messageToSend: String = ""

    @StateObject private var friendManager = FriendManager()

    @StateObject private var chatManager = ChatManager()

    var body: some View {

        VStack {

            if !isLoggedIn {

                // If the user is not logged in, display the login screen

                VStack(alignment: .leading) {

                    Text("Please enter your phone number:")

                        .font(.headline)

                    TextField("Phone number", text: $phoneNumber)

                        .textFieldStyle(RoundedBorderTextFieldStyle())

                        .padding(.bottom, 20)

                    Button(action: {

                        // Attempt to log in using phone number

                        isLoggedIn = true

                    }) {

                        Text("Log in")

                            .foregroundColor(.white)

                            .padding()

                            .background(Color.blue)

                            .cornerRadius(5)

                    }

                }

                .padding()

            } else if username == "" {

                // If the user is logged in but has not selected a username, display the username selection screen

                VStack(alignment: .leading) {

                    Text("Please select a username:")

                        .font(.headline)

                    TextField("Username", text: $username)

                        .textFieldStyle(RoundedBorderTextFieldStyle())

                        .padding(.bottom, 20)

                    Button(action: {

                        // Save username to user defaults or server

                    }) {

                        Text("Save")

                            .foregroundColor(.white)

                            .padding()

                            .background(Color.blue)

                            .cornerRadius(5)

                    }

                }

                .padding()

            } else {

                // If the user is logged in and has selected a username, display the chat, call, and map views

                ZStack {

                    TabView(selection: $selectedView) {

                        ChatView(friendManager: friendManager, chatManager: chatManager, username: username)

                            .tabItem {

                                Image(systemName: "bubble.left.and.bubble.right")

                                Text("Chat")

                            }

                            .tag(0)

                        CallView(friendManager: friendManager, username: username)

                            .tabItem {

                                Image(systemName: "phone.fill")

                                Text("Call")

                            }

                            .tag(1)

                        MapView()

                            .tag(2)

                    }

                    .accentColor(.blue)

                    HStack {

                        Spacer()

                        Button(action: {

                            // Show profile menu

                        }) {

                            Image(systemName: "person.crop.circle.fill")

                                .foregroundColor(.blue)

                                .padding()

                        }

                    }

                }

            }

        }

    }

}

@available(macOS 11.0, *)

struct ChatView: View {

    @StateObject private var friendManager: FriendManager

    @StateObject private var chatManager: ChatManager

    @State private var searchText: String = ""

    @State private var messageToSend: String = ""

    let username: String

    var body: some View {

        VStack {

            // Search bar

            HStack {

                TextField("Search", text: $searchText)

                    .textFieldStyle(RoundedBorderTextFieldStyle())

                Button(action: searchForFriends) {

                    Image(systemName: "magnifyingglass")

                        .foregroundColor(.blue)

                }

            }

            .padding()

            // List of chat messages

            ScrollView {

                ForEach(chatManager.messages, id: .self) { message in

                    VStack(alignment: .leading) {

                        Text(message.sender)

                            .font(.subheadline)

                            .foregroundColor(.gray)

                        Text(message.text)

                            .font(.body)

                            .padding(.vertical, 4)

                    }

                    .padding(.horizontal, 8)

                }

            }

            // Message input field

            HStack {

                TextField("Enter message", text: $messageToSend)

                    .textFieldStyle(RoundedBorderTextFieldStyle())

                Button(action: sendMessage) {

                    Image(systemName: "arrow.up.circle.fill")

                        .foregroundColor(.blue)

                }

            }

            .padding()

        }

    }

    private func searchForFriends() {

        // Search for friends using API or local data

    }

    private func sendMessage() {

        // Send message to server using network library

        chatManager.addMessage(Message(text: messageToSend, sender: username))

        messageToSend = ""

    }

}

@available(macOS 10.15, *)

struct CallView: View {

    @StateObject private var friendManager: FriendManager

    @State private var searchText: String = ""

    let username: String

    @available(macOS 10.15.0, *)

    var body: some View {

        VStack {

            // Search bar

            HStack {

                TextField("Search", text: $searchText)

                    .textFieldStyle(RoundedBorderTextFieldStyle())

                Button(action: searchForFriends) {

                    Image(systemName: "magnifyingglass")

                        .foregroundColor(.blue)

                }

            }

            .padding()

            // List of friends

            ScrollView {

                ForEach(friendManager.friends) { friend in

                    HStack {

                        Text(friend.username)

                            .font(.body)

                            .padding(.vertical, 8)

                        Spacer()

                        Button(action: {

                            // Call friend using call library

                        }) {

                            Image(systemName: "phone.fill")

                                .foregroundColor(.blue)

                                .padding(.vertical, 8)

                        }

                    }

                }

            }

        }

    }

    private func searchForFriends() {

        // Search for friends using API or local data

    }

}

struct MapView: View {

    @available(macOS 10.15.0, *)

    var body: some View {

        Map()

            .edgesIgnoringSafeArea(.all)

    }

}

struct Friend {

    let username: String

}

struct Message {

    let text: String

    let sender: String

}

@available(macOS 10.15, *)

class FriendManager: ObservableObject {

    @Published var friends: [Friend] = []

    func addFriend(friend: Friend) {

        friends.append(friend)

    }

    func searchForFriends(searchText: String) -> [Friend] {

        // Search for friends using API or local data

        return []

    }

}

@available(macOS 10.15, *)

class ChatManager: ObservableObject {

    @Published var messages: [Message] = []

    func addMessage(message: Message) {

        messages.append(message)

    }

}

Thanks.

I changed as follows to make it work:

  • First, define target as MacOS 12.0
  • Code (Part 1 - Changes are indicated in code)
import SwiftUI
import MapKit             // <<-- CHANGE HERE

@available(macOS 10.15, *)  // <<-- NO MORE NEEDED if target 12.0
struct AppView: View {
    @State private var phoneNumber: String = ""
    @State private var isLoggedIn: Bool = false
    @State private var username: String = ""
    @State private var selectedView: Int = 0
    @State private var messageToSend: String = ""
    @StateObject private var friendManager = FriendManager()
    @StateObject private var chatManager = ChatManager()
    @State var enteredName = ""  // <<-- ADD THIS

    var body: some View {
        VStack {
            if !isLoggedIn {
                // If the user is not logged in, display the login screen
                VStack(alignment: .leading) {
                    Text("Please enter your phone number:")
                        .font(.headline)
                    TextField("Phone number", text: $phoneNumber)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .padding(.bottom, 20)
                    Button(action: {
                        // Attempt to log in using phone number
                        isLoggedIn = true
                    }) {
                        Text("Log in")
                            .foregroundColor(.blue) // <<-- CHANGE .white)
                            .padding()
                            // REMOVE -->> .background(Color.blue)
                            .cornerRadius(5)
                    }
                }
                .padding()
            } else if username == "" { // <<-- CHANGE HERE
                // If the user is logged in but has not selected a username, display the username selection screen
                VStack(alignment: .leading) {
                    Text("Please select a username:")
                        .font(.headline)
                    TextField("Username", text: $username)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .padding(.bottom, 20)
                    Button(action: {
                        // Save username to user defaults or server
                        username = enteredName  // <<-- CHANGE HERE
                    }) {
                        Text("Save")
                            .foregroundColor(.blue) // <<-- CHANGE .white)
                            .padding()
                            // REMOVE -->> .background(Color.blue)
                            .cornerRadius(5)
                    }
                }
                .padding()
            } else {
                // If the user is logged in and has selected a username, display the chat, call, and map views
                ZStack {
                    TabView(selection: $selectedView) {
                        ChatView(friendManager: friendManager, chatManager: chatManager, username: username)
                            .tabItem {
                                Image(systemName: "bubble.left.and.bubble.right")
                                Text("Chat")
                            }
                            .tag(0)
                        CallView(friendManager: friendManager, username: username)
                            .tabItem {
                                Image(systemName: "phone.fill")
                                Text("Call")
                            }
                            .tag(1)
                        MapView()
                            .tabItem {  // <<-- ADD THIS
                                Image(systemName: "map")
                                Text("Map")
                            }
                            .tag(2)
                    }
                    .accentColor(.blue)
                    HStack {
                        Spacer()
                        Button(action: {
                            // Show profile menu
                        }) {
                            Image(systemName: "person.crop.circle.fill")
                                .foregroundColor(.blue)
                                .padding()
                        }
                    }
                }
            }
        }
    }
}
@available(macOS 11.0, *)
struct ChatView: View {
    @StateObject var friendManager: FriendManager       // <<-- CHANGE HERE - REMOVE private
    @StateObject var chatManager: ChatManager      // <<-- CHANGE HERE - REMOVE private
    @State private var searchText: String = ""
    @State private var messageToSend: String = ""
    let username: String
    
    var body: some View {
        VStack {
            // Search bar
            HStack {
                TextField("Search", text: $searchText)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                Button(action: searchForFriends) {
                    Image(systemName: "magnifyingglass")
                        .foregroundColor(.blue)
                }
            }
            .padding()
            // List of chat messages
            ScrollView {
                ForEach(chatManager.messages, id: \.self) { message in        // <<-- CHANGE HERE
                    VStack(alignment: .leading) {
                        Text(message.sender)
                            .font(.subheadline)
                            .foregroundColor(.gray)
                        Text(message.text)
                            .font(.body)
                            .padding(.vertical, 4)
                    }
                    .padding(.horizontal, 8)
                }
            }
            // Message input field
            HStack {
                TextField("Enter message", text: $messageToSend)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                Button(action: sendMessage) {
                    Image(systemName: "arrow.up.circle.fill")
                        .foregroundColor(.blue)
                }
            }
            .padding()
        }
    }
    private func searchForFriends() {
        // Search for friends using API or local data
    }
    private func sendMessage() {
        // Send message to server using network library
        chatManager.addMessage(Message(text: messageToSend, sender: username))
        messageToSend = ""
    }
}

NOTE: Follow up in second post

Accepted Answer

Code (Part 2):

@available(macOS 10.15, *)
struct CallView: View {
    @StateObject var friendManager: FriendManager      // <<-- CHANGE HERE
    @State private var searchText: String = ""
    let username: String
    @available(macOS 10.15.0, *)
    var body: some View {
        VStack {
            // Search bar
            HStack {
                TextField("Search", text: $searchText)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                Button(action: searchForFriends) {
                    Image(systemName: "magnifyingglass")
                        .foregroundColor(.blue)
                }
            }
            .padding()
            // List of friends
            ScrollView {
                ForEach(friendManager.friends, id: \.self) { friend in       // <<-- CHANGE HERE
                    HStack {
                        Text(friend.username)
                            .font(.body)
                            .padding(.vertical, 8)
                        Spacer()
                        Button(action: {
                            // Call friend using call library
                        }) {
                            Image(systemName: "phone.fill")
                                .foregroundColor(.blue)
                                .padding(.vertical, 8)
                        }
                    }
                }
            }
        }
    }
    private func searchForFriends() {
        // Search for friends using API or local data
    }
}
struct MapView: View {
    @available(macOS 10.15.0, *)
    @State private var mapRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 43.6, longitude: 1.44), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))    // <<-- Set your region    
    var body: some View {
        Map(coordinateRegion: $mapRegion)
            .edgesIgnoringSafeArea(.all)
    }
}
struct Friend :Hashable {      // <<-- CHANGE HERE
    let username: String
}

struct Message : Hashable { // <<-- CHANGE HERE
    let text: String
    let sender: String
}

@available(macOS 10.15, *)
class FriendManager: ObservableObject {
    @Published var friends: [Friend] = []
    func addFriend(friend: Friend) {
        friends.append(friend)
    }
    func searchForFriends(searchText: String) -> [Friend] {
        // Search for friends using API or local data
        return []
    }
}
@available(macOS 10.15, *)
class ChatManager: ObservableObject {
    @Published var messages: [Message] = []
    func addMessage(_ message: Message) {      // <<-- CHANGE HERE
        messages.append(message)
    }
}

THANK YOU VERY MUCH! It worked but there is still this one mistake in the App section, and I don't know how to fix it:

Never mind, I just fixed it by changing it to AppView()

Still thank you very much for your support!

Please Help Me Fix This Issues
 
 
Q