英文:
Can't convert String to Int in @AppStorage
问题
我想将所有的@AppStorage都转换为Int(甚至是Float)值,然后进行乘法运算。或者我应该将salaryPh和hoursPm转换为Int值,但我也无法做到这一点。我尝试创建一个函数,可以将String转换为Int,但没有帮助我。
我认为我做错了什么,尝试寻找相同问题的解决方案,只找到了一个,但没有帮助我(链接)
import SwiftUI
struct HomeView: View {
var body: some View {
NavigationView {
ExtractedView()
}
}
}
struct ExtractedView: View {
@State var monthString = Date().getMonthString().lowercased().capitalized
@State private var salaryPh: String = ""
@State private var hoursPm: String = ""
@AppStorage("SALARY_KEY") var savedSalary = ""
@AppStorage("HOURS_KEY") var savedHoursPm = ""
var body: some View {
ZStack {
BackgroundView()
CalendarView()
RoundedRectangle(cornerRadius: 40)
.frame(width: 225, height: 100)
.foregroundColor(.blue)
.offset(y: 190)
VStack(alignment: .center, spacing: 13) {
Text("Your netto-salary per hour")
.foregroundColor(Color.white)
.font(Font.system(size: 23, design: .rounded))
.fontWeight(.light)
TextField("Salary", text: $salaryPh)
.frame(width: 100, height: 50)
.background(.black)
.opacity(0.5)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
.font(.system(.body, design: .monospaced))
.overlay (
RoundedRectangle(cornerRadius: 15)
.stroke(Color.blue, lineWidth: 4)
)
.onChange(of: salaryPh) { salaryPh in
self.savedSalary = salaryPh
}
.onAppear {
self.salaryPh = savedSalary
print("Loaded: \(savedSalary)")
}
}
.offset(y: -150)
VStack(alignment: .center, spacing: 13) {
Text("Hours in this month")
.foregroundColor(Color.white)
.font(Font.system(size: 23, design: .rounded))
.fontWeight(.light)
TextField("Hours", text: $hoursPm)
.foregroundColor(Color.white)
.frame(width: 100, height: 50)
.background(.black)
.opacity(0.5)
.multilineTextAlignment(.center)
.font(.system(.body, design: .monospaced))
.overlay (
RoundedRectangle(cornerRadius: 15)
.stroke(Color.blue, lineWidth: 4)
)
.onChange(of: hoursPm) { hoursPm in
self.savedHoursPm = hoursPm
}
.onAppear {
self.hoursPm = savedHoursPm
print("Loaded: \(savedHoursPm)")
}
}
.offset(y: -20)
VStack(spacing: 20) {
Text("In \(monthString) i make:")
.foregroundColor(Color.white)
.font(Font.system(size: 23, design: .rounded))
.fontWeight(.light)
}
.offset(y: 165)
}
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}
英文:
I want to convert all @AppStorage's to Int(or even to Float) value and after that multiply it. Or probably i should to convert salaryPh and hoursPm to Int values, but i also can't do it. I've tried to create function where i could change String to Int but it didn't help me
I think that i'm doing something wrong, tried to find the solution with same question and found only one, but it didn't help me(link)
import SwiftUI
struct HomeView: View {
var body: some View {
NavigationView {
ExtractedView()
}
}
}
struct ExtractedView: View {
@State var monthString = Date().getMonthString().lowercased().capitalized
@State private var salaryPh: String = ""
@State private var hoursPm: String = ""
@AppStorage("SALARY_KEY") var savedSalary = ""
@AppStorage("HOURS_KEY") var savedHoursPm = ""
var body: some View {
ZStack {
BackgroundView()
CalendarView()
RoundedRectangle(cornerRadius: 40)
.frame(width: 225, height: 100)
.foregroundColor(.blue)
.offset(y: 190)
VStack(alignment: .center, spacing: 13) {
Text("Your netto-salary per hour")
.foregroundColor(Color.white)
.font(Font.system(size: 23, design: .rounded))
.fontWeight(.light)
TextField("Salary", text: $salaryPh)
.frame(width: 100, height: 50)
.background(.black)
.opacity(0.5)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
.font(.system(.body, design: .monospaced))
.overlay (
RoundedRectangle(cornerRadius: 15)
.stroke(Color.blue, lineWidth: 4)
)
.onChange(of: salaryPh) { salaryPh in
self.savedSalary = salaryPh
}
.onAppear {
self.salaryPh = savedSalary
print("Loaded: \(savedSalary)")
}
}
.offset(y: -150)
VStack(alignment: .center, spacing: 13) {
Text("Hours in this month")
.foregroundColor(Color.white)
.font(Font.system(size: 23, design: .rounded))
.fontWeight(.light)
TextField("Hours", text: $hoursPm)
.foregroundColor(Color.white)
.frame(width: 100, height: 50)
.background(.black)
.opacity(0.5)
.multilineTextAlignment(.center)
.font(.system(.body, design: .monospaced))
.overlay (
RoundedRectangle(cornerRadius: 15)
.stroke(Color.blue, lineWidth: 4)
)
.onChange(of: hoursPm) { hoursPm in
self.savedHoursPm = hoursPm
}
.onAppear {
self.hoursPm = savedHoursPm
print("Loaded: \(savedHoursPm)")
}
}
.offset(y: -20)
VStack(spacing: 20) {
Text("In \(monthString) i make:")
.foregroundColor(Color.white)
.font(Font.system(size: 23, design: .rounded))
.fontWeight(.light)
}
.offset(y: 165)
}
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}
答案1
得分: 1
你可以通过使用具有value
的TextField
来摆脱所有的转换。
TextField(
"Double",
value: $myDouble,
format: .number
)
这个设置也适用于Int
。
一旦TextField
兼容数字,你可以将所有的字符串切换为正确的数字类型。
英文:
You can get rid of all the conversion by using TextField
with value
.
TextField(
"Double",
value: $myDouble,
format: .number
)
This setup will work with Int
too.
Once the TextField is compatible with numbers you can switch all the Strings to be the correct number type.
https://developer.apple.com/documentation/swiftui/textfield/init(_:value:format:prompt:)-3fh51
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论