创建firstapp项目,在Xcode上可见到以下的项目结构。
项目结构
- firstapp
- AppDelegate.swift
- ViewController.swift
- Main.storyboard
- Assets.xcassets
- LaunchScreen.storyboard
- info.plist
- Products
- firstapp.app
描述说明
简单地描述一下各个文件的作用:
- AppDelegate.swift
@UIApplicationMain
class AppDelegate : UIResponder, UIApplicationDelegate
通过@UIApplicationMain注解,创建了一个Application对象来管控整个App的生命周期,正如Android中的Application对象。
同时也创建了一个AppDelegate对象,来负责控制窗口的显示,绘制,切换等事务,简单来说,由我们来开发所实现的逻辑都是由AppDelegate来负责的。
AppDelegate是整个iOS App的入口。
ViewController.swift
iOS是很彻底的MVC架构,每一个View都会有一个对应的Controller来处理对应的业务逻辑和界面交互。ViewController就是Xcode在创建项目的时候,默认为我们创建的一个View所对应的Controller。
如果喜欢,你也可以将其改名,只是不要忘了,在Main.Storyboard中,对应的View也要顺应修改其Controller的名字。Main.storyboard
Main.storyboard负责维护UI的展现和一些交互。通过Main.storyboard及很多的控件,如ImageView, Label, TextField等,我们可以在这里可视化地利用拖拽实现我们的UI效果。LaunchScreen.soryboard
LaunchScreen.storyboard主要是App启动界面的UI维护。App的启动,会有启动界面,在此界面,我们也可以添加我们个性化的东西,如主页图,新手引导等,而启动完成之后,就会进入Main.storyboard界面了。Assets.xcassets
主要是放置资源的文件夹,如图片和数据等。Info.plist
Info.plist主要是对app的配置,类似Android中AndroidManifest.xml 文件,在其中可以配置启动界面是否是LaunchScreen.storyboard,主界面是否是Main.storyboard,版本号,app名称,app图标等。
导入第三方库
在项目录下创建 Podfile文件,项目结构如下:
- firstapp
- firstapp.xcodeproj
- Podfile
例如导入SwiftHTTP,则Podfile内容如下:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'SwiftHTTP', '~> 1.0.0'
在当前项目根目录下执行:
pod install
Creating shallow clone of spec repo `master` from `https://github.com/CocoaPods/Specs.git`
CocoaPods 1.0.0.beta.1 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.
Updating local specs repositories
CocoaPods 1.0.0.beta.1 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.
Analyzing dependencies
Downloading dependencies
Installing SwiftHTTP (1.0.2)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `firstapp.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
注意,在安装完成之前有一句提示,
Please close any current Xcode sessions and use
firstapp.xcworkspacefor this project from now on.
也就是说,利用Pod导入了第三方库之后,以后要打开这个项目就要使用firstapp.xcworkspace了。
创建完之后,在项目目录下就会出现以下
- Podfile
- Podfile.lock
- Pods
- firstapp
- firstapp.xcodeproj
- firstapp.xcworkspace
重新通过firstapp.xcworkspace打开Xcode,我们就可以看到多了一个Pods的项目目录,在其下面可看到SwiftHttp框架了。
编译成功,之后就可以使用SwiftHTTP了。
随便找一个swift文件,导入swift,如下:
import SwiftHTTP
编译一下,只要没有问题,就是导入成功,可以正常使用。
本文介绍了iOS应用的基本项目结构,包括AppDelegate.swift、ViewController.swift、Main.storyboard、LaunchScreen.storyboard、Assets.xcassets和info.plist的作用。此外,还详细讲解了如何通过Podfile导入第三方库SwiftHTTP,并强调了导入后应使用.xcworkspace文件打开项目。
691

被折叠的 条评论
为什么被折叠?



