无法在xcode的控制台中输入任何内容[已关闭]

i5desfxk  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(312)

6小时前关门了。
Improve this question
我是swift的新手,这是我的输入代码,但是我不能在控制台输入任何东西。

import UIKit

print("Enter your name")
if let name=readLine(){
    print(name)
}
mi7gmzs6

mi7gmzs61#

在xCode开发应用上,输入不是在控制台中进行,而是在模拟设备或物理设备上进行。
1.创建一个新的IOS项目并选择SwiftUI作为接口。
2.替换您的内容视图(.swift)代码:

import SwiftUI

struct ContentView: View {
    @State var input: String
    
    var body: some View {
        VStack (alignment: .center) {
            Text("Enter Your name")
            TextField("Input", text: $input, prompt: Text("Your Name"))
                .frame(height: 48)
                .padding(EdgeInsets(top: 0, leading: 12, bottom: 0, trailing: 12))
                .cornerRadius(5)
                .overlay(
                    RoundedRectangle(cornerRadius: 5)
                        .stroke(lineWidth: 1.0)
                )
                .onSubmit {
                    print(input)
                }
        }
        .frame(width: 200)
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(input: "")
    }
}

3.将xxxApp(.swift)代码替换为:

import SwiftUI

@main
struct text_transcribeApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView(input: "")
        }
    }
}

相关问题