import SwiftUI
import NaturalLanguage
struct ContentView: View {
@State var language = ""
@State var message = ""
var body: some View {
VStack {
TextField("Type foreign language here", text: $language)
.autocorrectionDisabled()
.padding()
Text("Language = \\(message)")
Button("Identify Language"){
if let prediction = predictLanguage(text: language) {
message = prediction
}
}
}
.padding()
}
func predictLanguage(text: String) -> String? {
let locale = Locale(identifier: "en")
let recognizer = NLLanguageRecognizer()
recognizer.processString(text)
guard let language = recognizer.dominantLanguage else {
return "Unknown Language"
}
return locale.localizedString(forLanguageCode: language.rawValue)
}
}
#Preview {
ContentView()
}
We are using the Natural Language Framework to identify the foreign language in this example.
import SwiftUI
import PhotosUI
struct ContentView: View {
@State var selectedItems: (PhotosPickerItem] = []
var body: some View {
VStack {
PhotosPicker(selection: selectedItems, maxSelectionCount: 2, matching: images) {
Text ("Select Some Pictures")
}
}
#Preview {
ContentView()
}