无法将String转换为Int在@AppStorage中。

huangapple go评论62阅读模式
英文:

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

你可以通过使用具有valueTextField来摆脱所有的转换。

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

huangapple
  • 本文由 发表于 2023年2月8日 23:27:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定