4

I am new to IOS developing, and want to use the Swift language instead of Objective-C.

I know few concepts about Cocoa touch, and I want to know : Can Swift do everything that Objective-C can do ?

fishinear
  • 6,101
  • 3
  • 36
  • 84
chenzhongpu
  • 6,193
  • 8
  • 41
  • 79

3 Answers3

5

There are a lot of things that can be done in Objective-C but cannot be done in Swift, without implementing it in Objective-C and then using it from Swift. Some of them include:

  • Catching Objective-C exceptions
  • Using C++ APIs (through Objective-C++)
  • Using NSInvocation, performSelector: and other ways of making calls dynamically where the method to call is chosen at runtime
  • Handling unimplemented method calls using forwardInvocation:
  • Provide a function for use in C APIs that take a function pointer
newacct
  • 119,665
  • 29
  • 163
  • 224
3

The only concept I know that is in Objective-C but not in Swift, is Key-Value Observing (KVO). You can use KVO for a Swift class to observe the property of an Objective-C class, but you cannot observe any arbitrary property of a Swift class. See this answer for more details.

Community
  • 1
  • 1
fishinear
  • 6,101
  • 3
  • 36
  • 84
0

This is an interesting question but essentially the answer must be NO because you can use Objective-C resources in swift using bridging-headers. Xcode automatically translates Swift to Objective-C and vice versa. However, if you cannot write Objective-C code then you cannot include your own custom objective-c classes in your swift projects!

It all depends on how you like to code. Apple have said that Objective-C is still a 'first class' language meaning that they are going to run Swift and Objective-C side by side for the foreseeable future. Personally I prefer Objective-C because you can use C very easily (as anything that is legal in C is also legal in Objective-C) added to which Swift is a more procedural in style where Objective-C is quite clearly object orientated.

It is worth noting that the Cocoa and Cocoa Touch classes are all objective-c classes and so it may be useful to have a working knowledge of Objective-C. I think the best advice I've heard so far is, if you have the time, learn both!

Rob Sanders
  • 5,197
  • 3
  • 31
  • 58