42

I am new to iOS development and have started directly with 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. They are "Cocoa Touch Class" and "Swift Class".

When I create a new Cocoa Touch Class, this is what I get(depending on the class inherited)

import UIKit

class CtTest: NSObject {

}

and when I create a new Swift file, I get this:-

import Foundation

The extension for both of these classes is .swift

If I add the class definition to the Swift file and modify it as follows:-

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

Rajeev Bhatia
  • 2,974
  • 1
  • 21
  • 29
  • 1
    Foundation is the framework that defines NSObject, NSString, NSArray, etc. all the basic objective-C classes. UIKit, on the other hand, defines views, view controllers, controls (e.g. UIButton), etc. Typically you import Foundation for model classes (data) and UIKit for view/controller classes (presentation, logic). – Nicolas Miari Jul 30 '14 at 06:19
  • 4
    Addendum: **Foundation** is common to both iOS and OSX. **UIKit** is iOS-only. – Nicolas Miari Jul 30 '14 at 06:20
  • 1
    Thanks for the answer Nicolas. Makes sense why the Cocoa Touch Class would import Foundation and the Swift classes would import UIKit. Should I then prefer Swift classes for View Controllers and Cocoa Touch Classes for models(though I would most likely use Swift Data types such as String, Int, Dictionary etc. over Foundation data types such as NSNumber,NSString and NSDictionary making Foundation kinda redundant). – Rajeev Bhatia Jul 30 '14 at 06:34
  • 1
    I haven't used swift myself yet, but check out the answer below by @drewag; it is very well written and complete. – Nicolas Miari Jul 30 '14 at 06:48
  • Yes indeed it is. I also found Cocoa Touch classes more useful when you need some methods added by default(as I mentioned in my comment to his answer). Thanks again for your inputs! – Rajeev Bhatia Jul 30 '14 at 06:53

1 Answers1

93

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. It includes data types like NSArray, NSString, and NSDictionary. However, Swift provides replacements for these data types: Array, String, and Dictionary. You should only need to import Foundation in files that try to interact with Objective-C APIs. 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.

UIKit

UIKit actually includes Foundation. If you import UIKit, you do not need to also import Foundation. UIKit is a collection of standard APIs specific to the iOS interface. It includes views like UITableView and controllers like UIViewController. You will need to import this whenever working on the UI of iOS apps.

Cocoa Touch Class

This template is useful for subclassing classes from UIKit and potentially having Xcode generate interface builder files for you. If you don't need either of those things, you should just use the Swift Class template. 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). 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.

Andrew
  • 3,825
  • 4
  • 30
  • 44
drewag
  • 93,393
  • 28
  • 139
  • 128
  • 1
    Thanks for the amazing answer drewag. I just tried creating two sample classes(one Swift and one Cocoa Touch) that would need to inherit UITableViewController. Sure enough I had to do all the heavy lifting for the Swift class but the Cocoa Touch class added all the required methods for me. Thanks again! – Rajeev Bhatia Jul 30 '14 at 06:50