SwiftUI 函数显示 "Modifying state during view update, this will cause undefined behavior."
SwiftUI function shows "Modifying state during view update, this will cause undefined behavior."
该代码的工作原理在于它会像我想要的那样调出 5 个随机辅音和 2 个元音,但它显示一条警告消息“在视图更新期间修改状态,这将导致未定义的行为。”
我是 Swift 的新手,当我搜索 swiftui 和“在视图更新期间修改状态,这将导致未定义的行为”时,我不明白我在网上阅读的答案。至少我不太了解修复我自己的代码。我离弄清楚如何修改我的代码以消除警告还差得很远。请帮忙
仅供参考 - 我想将我的元音和辅音保存在(排序的)数组中,当按下按钮以滚动新的随机元音和辅音时更新这些数组。
import SwiftUI
struct ContentView: View {
@State private var vowels = ["X","X"]
@State private var consonants = ["X","X","X","X","X"]
var body: some View {
VStack {
Spacer()
Text(rollConsonants())
Text(rollVowels())
Spacer()
Button("Reroll") {
_ = rollVowels()
_ = rollConsonants()
}
}
.font(.largeTitle)
}
func rollVowels() -> String {
vowels = ["A","E","I","O","U"].shuffled().prefix(2).sorted() // message here
let string = vowels.joined(separator:" ")
return string
}
func rollConsonants() -> String {
let commonConsonants = ["C","D","L","N","R","S","T"].shuffled().prefix(2)
var otherConsonants = ["B","F","G","H","K","M","P","V","W","Y","*"].shuffled().prefix(3)
let rareConsonants = ["J","Q","X","Z"].shuffled()
if let i = otherConsonants.firstIndex(of: "*") {
otherConsonants[i] = rareConsonants[0]
}
consonants = (commonConsonants + otherConsonants).sorted() // message here
let string = consonants.joined(separator:" ")
return string
}
}
是因为:
Text(rollConsonants())
Text(rollVowels())
您正在修改状态,而 SwiftUI 正在尝试呈现文本。
此更改通过将更新状态的行为与读取状态的行为分开来修复警告:
import SwiftUI
struct ContentView: View {
@State private var vowels = ["X", "X"]
@State private var consonants = ["X", "X", "X", "X", "X"]
var formattedVowels: String {
vowels.joined(separator: " ")
}
var formattedConsonants: String {
consonants.joined(separator: " ")
}
var body: some View {
VStack {
Spacer()
Text(formattedConsonants)
Text(formattedVowels)
Spacer()
Button("Reroll") {
rollVowels()
rollConsonants()
}
}
.font(.largeTitle)
}
func rollVowels() {
vowels = ["A", "E", "I", "O", "U"].shuffled().prefix(2).sorted() // message here
}
func rollConsonants() {
let commonConsonants = ["C", "D", "L", "N", "R", "S", "T"].shuffled().prefix(2)
var otherConsonants = ["B", "F", "G", "H", "K", "M", "P", "V", "W", "Y", "*"].shuffled().prefix(3)
let rareConsonants = ["J", "Q", "X", "Z"].shuffled()
if let i = otherConsonants.firstIndex(of: "*") {
otherConsonants[i] = rareConsonants[0]
}
consonants = (commonConsonants + otherConsonants).sorted() // message here
}
}
该代码的工作原理在于它会像我想要的那样调出 5 个随机辅音和 2 个元音,但它显示一条警告消息“在视图更新期间修改状态,这将导致未定义的行为。”
我是 Swift 的新手,当我搜索 swiftui 和“在视图更新期间修改状态,这将导致未定义的行为”时,我不明白我在网上阅读的答案。至少我不太了解修复我自己的代码。我离弄清楚如何修改我的代码以消除警告还差得很远。请帮忙
仅供参考 - 我想将我的元音和辅音保存在(排序的)数组中,当按下按钮以滚动新的随机元音和辅音时更新这些数组。
import SwiftUI
struct ContentView: View {
@State private var vowels = ["X","X"]
@State private var consonants = ["X","X","X","X","X"]
var body: some View {
VStack {
Spacer()
Text(rollConsonants())
Text(rollVowels())
Spacer()
Button("Reroll") {
_ = rollVowels()
_ = rollConsonants()
}
}
.font(.largeTitle)
}
func rollVowels() -> String {
vowels = ["A","E","I","O","U"].shuffled().prefix(2).sorted() // message here
let string = vowels.joined(separator:" ")
return string
}
func rollConsonants() -> String {
let commonConsonants = ["C","D","L","N","R","S","T"].shuffled().prefix(2)
var otherConsonants = ["B","F","G","H","K","M","P","V","W","Y","*"].shuffled().prefix(3)
let rareConsonants = ["J","Q","X","Z"].shuffled()
if let i = otherConsonants.firstIndex(of: "*") {
otherConsonants[i] = rareConsonants[0]
}
consonants = (commonConsonants + otherConsonants).sorted() // message here
let string = consonants.joined(separator:" ")
return string
}
}
是因为:
Text(rollConsonants())
Text(rollVowels())
您正在修改状态,而 SwiftUI 正在尝试呈现文本。
此更改通过将更新状态的行为与读取状态的行为分开来修复警告:
import SwiftUI
struct ContentView: View {
@State private var vowels = ["X", "X"]
@State private var consonants = ["X", "X", "X", "X", "X"]
var formattedVowels: String {
vowels.joined(separator: " ")
}
var formattedConsonants: String {
consonants.joined(separator: " ")
}
var body: some View {
VStack {
Spacer()
Text(formattedConsonants)
Text(formattedVowels)
Spacer()
Button("Reroll") {
rollVowels()
rollConsonants()
}
}
.font(.largeTitle)
}
func rollVowels() {
vowels = ["A", "E", "I", "O", "U"].shuffled().prefix(2).sorted() // message here
}
func rollConsonants() {
let commonConsonants = ["C", "D", "L", "N", "R", "S", "T"].shuffled().prefix(2)
var otherConsonants = ["B", "F", "G", "H", "K", "M", "P", "V", "W", "Y", "*"].shuffled().prefix(3)
let rareConsonants = ["J", "Q", "X", "Z"].shuffled()
if let i = otherConsonants.firstIndex(of: "*") {
otherConsonants[i] = rareConsonants[0]
}
consonants = (commonConsonants + otherConsonants).sorted() // message here
}
}