I'm an entrepreneur working independently, and I've always had a passion for technology, especially within the Apple ecosystem. I'm interested in participating in the Swift Student Challenge at WWDC24. Although I'm no longer a student, would enrolling in any STEM program still make me eligible to apply?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I’m developing an app to save words that users learn in a language they are studying.
Here’s the basic workflow:
Type a new word
Add the new word
Every time a word is added, it gets saved to a list that includes its meaning and the name of the language — useful when saving words in multiple languages
Problem:
For some reason, the “Add new word” button only works for the first word. The second word is not being added to the list.
Here is my code:
import SwiftUI
import NaturalLanguage
import Translation
struct ContentView: View {
@State private var inputWord: String = ""
@State private var detectedLanguage: String?
@State private var translationConfiguration: TranslationSession.Configuration?
@StateObject private var viewModel = WordViewModel()
var body: some View {
VStack(spacing: 24) {
// Input field
TextField("Type a word in any language", text: $inputWord)
.padding()
.background(Color(.systemGray6))
.cornerRadius(10)
// Button to translate and save
Button("Add new word") {
translateAndSave()
}
// .disabled(inputWord.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
.buttonStyle(.borderedProminent)
// List of saved words
Text("My Words")
.font(.system(.title2))
List {
ForEach(viewModel.words) { word in
VStack(alignment: .leading, spacing: 4) {
Text(word.inputWord)
.font(.headline)
Text(word.nativeTranslation)
.font(.subheadline)
.foregroundColor(.gray)
Text(word.detectedLanguage.uppercased())
.font(.caption)
.foregroundColor(.blue)
}
.padding(.vertical, 4)
}
}
Spacer()
}
.padding()
.animation(.easeInOut, value: detectedLanguage)
// Automatically triggers translation when configuration changes
.translationTask(translationConfiguration) { session in
do {
let response = try await session.translate(inputWord)
viewModel.addWord(
input: inputWord,
native: response.targetText,
detected: detectedLanguage ?? "Unknown"
)
// Reset fields after saving
inputWord = ""
detectedLanguage = nil
translationConfiguration = nil
} catch {
// Handle error
print("Translation error: \(error.localizedDescription)")
}
}
}
// Function to detect language
private func detectLanguage() {
let recognizer = NLLanguageRecognizer()
recognizer.processString(inputWord)
if let code = recognizer.dominantLanguage?.rawValue {
let name = Locale.current.localizedString(forIdentifier: code)
detectedLanguage = name?.capitalized
} else {
detectedLanguage = "Unable to detect"
}
}
// Function to prepare translation
private func translateAndSave() {
detectLanguage()
translationConfiguration = TranslationSession.Configuration(
source: nil,
target: Locale.Language(identifier: "en")
)
}
}
Question:
Does anyone have any idea what I can do to fix this?