Join Simon Allardice for an in-depth discussion in this video Instantiating objects, part of Comparing Swift and Objective-C.
- Swift and Objective-C are both object-oriented programming languages. We instantiate and use objects based on defined classes. Swift can instantiate and use objects where classes were written in Objective-C and vice versa. It is true that if we use Swift-specific language features, there might be limitations on what we can do with a Swift object in Objective-C, but first, the fundamentals. To instantiate a simple object in Objective-C, we'd often write code like this, using the combination of alloc and init to first allocate memory and then initialize the allocated object.
In Swift, we'd instantiate a simple object this way, using the name of the class followed by parentheses. We'd now have an object of type NSDate whether we're using Objective-C or Swift versions here. Now in Swift, we don't need alloc or init, nor do we need a word like "new" that you might see in Java or C Sharp. It's simply inferred that we are instantiating an object by the fact that we're creating a new variable using var and setting its initial value equal to the class name NSDate with parentheses here.
And that we'll call the initializer for the NSDate class. Also, in Objective-C we need the asterisk to say that this name, myDate, is a pointer to an NSDate object. In Swift, we just name the variable. For those of you coming from Objective-C, or C, understand that the use of explicit pointers is not needed in Swift, and in most cases you don't have direct access to pointers. Now, it's technically possible to use pointer types in Swift if you're interacting with libraries written in Standard C.
There are options to allow it, but it is very controlled, and you kind of have to work to do it. With normal Swift and Swift interacting with Objective-C, as we're focusing on here, you shouldn't even be thinking about pointers ninety-nine percent of the time. All right, whereas in Objective-C using the name of the class twice, once before the pointer to say what type this is, and again as part of the call to the alloc method, we only need the class name once in our Swift version and our variable, MyDate, is inferred of an object of type NSDate.
We can be explicit about providing a type for this variable. After the variable name, we use the colon followed by the class name, the type annotation in Swift. But, if we left this off, this code is obvious enough to the compiler that we are creating an object of type NSDate and Swift will infer the type. Now, once we've instantiated an object, we want to start using it. Calling methods, accessing properties, and there is more we can do when instantiating. We often want multiple ways to initialize an object, to create it in a certain state, and we'll get to that in a moment.
First, let's review Calling Methods.
Need a refresher on either language? Check out Swift Essential Training and Objective-C Essential Training.
- Exploring the interoperability of Swift and Objective-C
- Comparing language syntax
- Understanding how each language uses objects and methods
- Bridging arrays, dictionaries, and numbers
- Dealing with nil return types from Objective-C methods
- Working with NSError in Swift
- Working with Swift and Objective-C strings
- Adding Swift to an existing Objective-C project
- Inheriting between languages