O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The MessageBuilder Swift class - accessing Objective-C code from Swift

The Swift MessageBuilder class will contain one method named getPersonalizedMessage(). This method will use the getMessage() method from the Objective-C Messages class to retrieve a message and will then customize that message prior to returning it to the function that called it. Here is the code for the Swift MessageBuiler class:

import Foundation 
 
class MessageBuilder: NSObject { 
  func getPersonalizedMessage(name: String) -> String {  
    let messages = Messages() 
    let retMessage = "To: " + name + ", " + messages.getMessage()  
    return retMessage; 
  } 
} 

When we define this class, we create it as a subclass of the NSObject class. If a Swift class will be accessed from the Objective-C ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required