目前我能够制作如下的字母数组
[[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
Run Code Online (Sandbox Code Playgroud)
知道可以结束
[NSCharacterSet uppercaseLetterCharacterSet]
Run Code Online (Sandbox Code Playgroud)
如何制作阵列?
我们有一个具有一些“聊天”功能的应用程序,可以提出问题,用户可以使用一些预定义的选项进行回答:对于每个问题都会呈现一个新视图。其中一个选项是带有选择器的视图,从 iOS 16 开始,当带有选择器的视图消失时,此选择器会导致应用程序崩溃,并出现以下错误:Thread 1: Fatal error: Index out of range定位于class AppDelegate: UIResponder, UIApplicationDelegate {。在日志中我可以看到这个错误:Swift/ContiguousArrayBuffer.swift:600: Fatal error: Index out of range。
为了解决这个问题,我将代码重构到最低限度,甚至没有使用选择器,但仍然导致错误发生。当我从此视图中删除选择器时,它会再次工作。
查看发生错误的地方
struct PickerQuestion: View {
@EnvironmentObject() var questionVM: QuestionVM
let question: Question
var colors = ["A", "B", "C", "D"]
@State private var selected = "A"
var body: some View {
VStack {
// When removing the Picker from this view the error does not occur anymore
Picker("Please choose a value", selection: $selected) …Run Code Online (Sandbox Code Playgroud) func rand(max: Int?) -> Int {
var index = Int(arc4random())
return max? != nil ? (index % max!) : index
}
Run Code Online (Sandbox Code Playgroud)
我在最后一行得到一个例外: EXC_BAD_INSTRUCTION
我猜它与iPhone 5S是64位而5不是这样的事实有关,但我没有看到上面的函数处理64位的任何内容?
我能够通过以下调整解决问题,但我仍无法解释原因.
func rand(max: Int?) -> Int {
var index = arc4random()
return max? != nil ? Int(index % UInt32(max!)) : Int(index)
}
Run Code Online (Sandbox Code Playgroud) 当专门处理非可选String值时,字符串插值和字符串连接之间有什么区别?
struct MyModel {
let value1: String
let value2: String
var displayNameByConcatenation: String {
return value1 + "-" + value2
}
var displayNameByInterpolation: String {
return "\(value1)-\(value2)"
}
}
Run Code Online (Sandbox Code Playgroud)
displayNameByConcatenation和displayNameByInterpolation不同的情况?就像长 unicode 字符串一样?+或插值的行为以使它们在上面的示例中有所不同?请注意,从这个问题中我们了解到字符串插值将使用descriptionCustomStringConvertible 的 。但是String串联(运算符+)也调用吗description?
string string-concatenation string-interpolation swift swift3