import Foundation
print("请输入一个字符串:")
let fruit = readLine() ?? ""
var validPrice = false
var price = ""
var p1 = 0
var p2 = 0
repeat {
print("请输入一个字符串:")
price = readLine() ?? ""
let containsNonDigits = price.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted)
if containsNonDigits != nil {
print("输入的字符串含有非数字字符,请重新输入!")
} else {
validPrice = true
if let intValue = Int(price) {
p1 = intValue
p2 = Int(price, radix: 10) ?? 0
}
}
} while !validPrice
var dic: [String: String] = [:]
var arr: [String] = []
dic[fruit] = price
print("字典的内容为:\(dic)")
for (_, value) in dic {
arr.append(value)
}
print("数组的内容:\(arr)")
arr.remove(at: 0)
if arr.isEmpty {
print("对数组元素进行删除后:数组内容为\(arr),即数组为空!")
} else {
print("数组的第一个元素为:\(arr[0])")
}