我有一个文本字段,可以在图表中显示十个字符串。如果textfield one有文本,那么它将显示在textfield 2上,如果textfield 2有文本,那么它将转到textfield 3,依此类推。但由于某种原因,它总是在textfield 1中显示字符串。我附加了我的代码,这可能是一个愚蠢的错误。
@IBOutlet weak var name: UITextField!
let pone = UserDefaults.standard
let ptwo = UserDefaults.standard
let pthree = UserDefaults.standard
let pfour = UserDefaults.standard
let pfive = UserDefaults.standard
let psix = UserDefaults.standard
let pseven = UserDefaults.standard
let peight = UserDefaults.standard
let pnine = UserDefaults.standard
let pten = UserDefaults.standard
let pelleven = UserDefaults.standard
let ptwelve = UserDefaults.standard
let pthirteen = UserDefaults.standard
let pfourteen = UserDefaults.standard
let pfifteen = UserDefaults.standard
if pone.string(forKey: "number1") != nil{
if !(pone.string(forKey: "number1")?.isEmpty)! {
pone.set(name.text, forKey: "number1")
}
}else
if ptwo.string(forKey: "number2") != nil{
if !(ptwo.string(forKey: "number2")?.isEmpty)! {
ptwo.set(name.text, forKey: "number2")
}
}else
if pthree.string(forKey: "number3") != nil{
if !(pthree.string(forKey: "number3")?.isEmpty)! {
pthree.set(name.text, forKey: "number3")
}
}else
if pfour.string(forKey: "number4") != nil{
if !(pfour.string(forKey: "number4")?.isEmpty)! {
pfour.set(name.text, forKey: "number4")
}
}else
if pfive.string(forKey: "number5") != nil{
if !(pfive.string(forKey: "number5")?.isEmpty)! {
pfive.set(name.text, forKey: "number5")
}
}else
if psix.string(forKey: "number6") != nil{
if !(psix.string(forKey: "number6")?.isEmpty)! {
psix.set(name.text, forKey: "number6")
}
}else
if pseven.string(forKey: "number7") != nil{
if !(pseven.string(forKey: "number7")?.isEmpty)! {
pseven.set(name.text, forKey: "number7")
}
}else
if peight.string(forKey: "number8") != nil{
if !(peight.string(forKey: "number8")?.isEmpty)! {
peight.set(name.text, forKey: "number8")
}
}else
if pnine.string(forKey: "number9") != nil{
if !(pnine.string(forKey: "number9")?.isEmpty)! {
pnine.set(name.text, forKey: "number9")
}
}else
if pten.string(forKey: "number10") != nil{
if !(pten.string(forKey: "number10")?.isEmpty)! {
pten.set(name.text, forKey: "number10")
}
}else
if pelleven.string(forKey: "number11") != nil{
if !(pelleven.string(forKey: "number11")?.isEmpty)! {
pelleven.set(name.text, forKey: "number11")
}
}else
if ptwelve.string(forKey: "number12") != nil{
if !(ptwelve.string(forKey: "number12")?.isEmpty)! {
ptwelve .set(name.text, forKey: "number12")
}
}else
if pthirteen.string(forKey: "number13") != nil{
if !(pthirteen.string(forKey: "number13")?.isEmpty)! {
pthirteen.set(name.text, forKey: "number13")
}
}else
if pfourteen.string(forKey: "number14") != nil{
if !(pfourteen.string(forKey: "number14")?.isEmpty)! {
pfourteen.set(name.text, forKey: "number14")
}
}else
if pfifteen.string(forKey: "number15") != nil{
if !(pfifteen.string(forKey: "number15")?.isEmpty)! {
pfifteen.set(name.text, forKey: "number15")
}
}
答案 0 :(得分:1)
这是我提出的替代所有这些:
let keys = (0...15).map{ "number\($0)" }
for key in keys {
guard let existingValue = UserDefaults.standard.string(forKey: key),
existingValue.isEmpty else {
continue
}
UserDefaults.standard.set(name.text, forKey: key)
}