Swift创建第一个项目
2020-09-22 本文已影响0人
醉倾城1
1、打开 xcode 工具,选择 File => New => Project

2、我们选择一个"Single View App",并点击"next",创建一个简单示例app应用。

3、接着我们输入项目名称(Product Name),公司名称(Organization Name),公司标识前缀名(Organization identifier) 还要选择开发语言(Language).
其中Language有两个选项:Objective-c和swift,因为我们是学习swift当然选择swift项了。 点击"Next"下一步,后面在相关目录下点击“create”

4、项目创建后,默认生成了一个示例文件,可以看到swift将oc中的h和m文件合并成了一个文件(即swift后缀名文件).

5、SceneDelegate中默认使用的是ContentView,ContentView中有如下代码
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, World!")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

故运行模拟器,最终结果是界面上显示“Hello World!”,如下:

6、如果不使用系统提供的ContentView,做如下更改:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// let contentView = ContentView()
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
// window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = UIColor.red
window.addSubview(view)
window.makeKeyAndVisible()
}
}
运行后会发现出现了一个全红的界面

至此,我们的第一个Swift项目就完成了。