简体   繁体   English

Swift 类与 Xcode6 中的 Cocoa Touch 类

[英]Swift class vs Cocoa Touch Class in Xcode6

I am new to iOS development and have started directly with Swift.我是 iOS 开发新手,直接开始使用 Swift。

When I want to add a new file to a project, the IDE presents me with two files which seem to be used interchangeably in the tutorials that I have seen so far.当我想向项目添加一个新文件时,IDE 会向我显示两个文件,这两个文件在我目前看到的教程中似乎可以互换使用。 They are "Cocoa Touch Class" and "Swift Class".它们是“Cocoa Touch Class”和“Swift Class”。

When I create a new Cocoa Touch Class, this is what I get(depending on the class inherited)当我创建一个新的 Cocoa Touch 类时,这就是我得到的(取决于继承的类)

import UIKit

class CtTest: NSObject {

}

and when I create a new Swift file, I get this:-当我创建一个新的 Swift 文件时,我得到了这个:-

import Foundation

The extension for both of these classes is .swift这两个类的扩展名都是 .swift

If I add the class definition to the Swift file and modify it as follows:-如果我将类定义添加到 Swift 文件并修改如下:-

import UIKit
import Foundation

class SwTest:NSObject
{
}

is there any difference that remains between the two files.这两个文件之间是否存在任何差异。

If so, which classes are preferred in what scenarios eg.如果是这样,哪些类在哪些情况下是首选的,例如。 View Controllers, Models etc.视图控制器、模型等。

Thank You谢谢你

There is no difference between the two different templates other than the code they put into the file by default.除了默认放入文件的代码外,这两个不同的模板之间没有区别。 Like you noticed, they both have the same extension and they are both plain text files – the compiler does not treat them any differently.正如您所注意到的,它们都具有相同的扩展名,并且都是纯文本文件——编译器不会对它们进行任何不同的处理。

The general rule of thumb is to only import a module if you need it in that file – basically you get a compile error otherwise.一般的经验法则是仅在该文件中需要时才导入模块 - 否则基本上会出现编译错误。 You will get a compiler error because of missing imports if you try to use types from them.如果您尝试使用其中的类型,您将因为缺少导入而收到编译器错误。

Foundation基础

Foundation is basically the Apple development standard library, implemented in Objective-C. Foundation 基本上是 Apple 开发标准库,用 Objective-C 实现。 It includes data types like NSArray, NSString, and NSDictionary.它包括 NSArray、NSString 和 NSDictionary 等数据类型。 However, Swift provides replacements for these data types: Array, String, and Dictionary.然而,Swift 提供了这些数据类型的替代品:数组、字符串和字典。 You should only need to import Foundation in files that try to interact with Objective-C APIs.您应该只需要在尝试与 Objective-C API 交互的文件中import Foundation However, since all existing APIs are written in Objective-C (since Swift is so new), you will have to import Foundation in almost all files except for basic domain objects.然而,由于所有现有的 API 都是用 Objective-C 编写的(因为 Swift 是如此新),所以除了基本域对象之外,您几乎必须在所有文件中导入 Foundation。

UIKit用户界面工具包

UIKit actually includes Foundation. UIKit 实际上包括 Foundation。 If you import UIKit, you do not need to also import Foundation.如果导入 UIKit,则不需要同时导入 Foundation。 UIKit is a collection of standard APIs specific to the iOS interface. UIKit 是特定于 iOS 界面的标准 API 的集合。 It includes views like UITableView and controllers like UIViewController.它包括像 UITableView 这样的视图和像 UIViewController 这样的控制器。 You will need to import this whenever working on the UI of iOS apps.每当处理 iOS 应用程序的 UI 时,您都需要导入它。

Cocoa Touch Class可可触摸班

This template is useful for subclassing classes from UIKit and potentially having Xcode generate interface builder files for you.这个模板对于从 UIKit 继承类很有用,并且可能让 Xcode 为你生成界面构建器文件。 If you don't need either of those things, you should just use the Swift Class template.如果你不需要这些东西,你应该只使用 Swift Class 模板。 Only have your object inherit from NSObject if you really have to (if you need to be able to use your Swift class from Objective-C).如果确实需要(如果您需要能够使用 Objective-C 中的 Swift 类),则仅让您的对象从 NSObject 继承。 If you don't inherit from NSObject, there are extra optimizations that the compiler can make and it avoids some of the slower parts of the Objective-C runtime.如果您不从 NSObject 继承,编译器可以进行额外的优化,并避免 Objective-C 运行时的一些较慢的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM