How to call Objective-C code from Swift?

Tutorial on how to use Objective-C Classes in Swift and visa versa
06 September 2017   6703

Let's consider two different ways of calling Obj-C code from Swift.

Using Objective-C Classes in Swift

Step 1: Add Objective-C Implementation -- .m

Add a .m file to your class, and name it CustomObject.m.

Step 2: Add Bridging Header

When adding your .m file, you'll receive a promt with Yes, No and Cancel buttons. Hit Yes.

Step 3: Add Objective-C Header -- .h

Add another .h file and name it CustomObject.h.

Step 4: Build your Objective-C Class

In CustomObject.h

#import <Foundation/Foundation.h>

@interface CustomObject : NSObject

@property (strong, nonatomic) id someProperty;

- (void) someMethod;

@end

In CustomObject.m

#import "CustomObject.h"

@implementation CustomObject 

- (void) someMethod {
    NSLog(@"SomeMethod Ran");
}

@end

Step 5: Add Class to Bridging-Header

In YourProject-Bridging-Header.h

#import "CustomObject.h"

Step 6: Use your Object

In SomeSwiftFile.swift:

var instanceOfCustomObject: CustomObject = CustomObject()
instanceOfCustomObject.someProperty = "Hello World"
println(instanceOfCustomObject.someProperty)
instanceOfCustomObject.someMethod()

There is no need to import explicitly; that's what the bridging header is for.

Using Swift Classes in Objective-C

Now, let's go visa versa and learn how to use Swift Classes in Objective-C.

Step 1: Create New Swift Class

Add a .swift file to your project, and name it MySwiftObject.swift.

In MySwiftObject.swift:

import Foundation

class MySwiftObject : NSObject {

    var someProperty: AnyObject = "Some Initializer Val"

    init() {}

    func someFunction(someArg:AnyObject) -> String {
        var returnVal = "You sent me \(someArg)"
        return returnVal
    }   
}

Step 2: Import Swift Files to ObjC Class

In SomeRandomClass.m:

#import "<#YourProjectName#>-Swift.h"

The file:<#YourProjectName#>-Swift.h should already be created automatically in your project, even if you can not see it.

Step 3: Use your class

MySwiftObject * myOb = [MySwiftObject new];
NSLog(@"MyOb.someProperty: %@", myOb.someProperty);
myOb.someProperty = @"Hello World";
NSLog(@"MyOb.someProperty: %@", myOb.someProperty);
NSString * retString = [myOb someFunction:@"Arg"];
NSLog(@"RetString: %@", retString);

JetBrains to Release AppCode 2018.2

Great news for macOS and iOS developers all over the world
10 August 2018   648

JetBrains has published an update to AppCode - IDE for the development of iOS and macOS applications. In the new version, interface icons have been changed, a plug-in for working with databases has been added, and support for the Touch Bar has been implemented.

Swift:

  • The function of selection of closure is added. To do this, select the desired code, press the ^T keys and select Closure from the drop-down menu. The parameters are renamed and their names changed in the following way
  • Renaming now occurs without displaying additional dialogs. The function works correctly for:
    • Objective C arguments;
    • the names of the classes exported from Swift to Objective C using the @objc attribute;
    • Swift keywords used to declare an enumeration in Objective C;
    • parameter declarations in Objective C that are converted to Swift using a set of heuristic rules.
  • Implemented autocomplete: when you type "?" Or "!", The IDE automatically inserts the option immediately after the currently selected item.
  • Hints for autocomplete Swift attributes are also available when typing @:
  • The use of the navigation chain now transfers to the desired code fragment.
  • Comments to the documentation are displayed in the Structure panel (⌘7) and the Class / Symbol pop-up windows (⌘0 / ⌥⌘0):
  • You can create stub methods for parameters with the names of superclasses and protocols, as well as for the properties that are changed and the selected protocol parameters that are not in the superclass.
  • The Add / Remove menu added to AppCode 2017.3 can be used for Swift iterations: 

Debugger and assembly messages:

  • AppCode now performs the processing of LLDB scripts (for example, Chisel).
  • You can add an interrupt point by pressing the ⌥⏎ key combination: 
  • Added the Information for the names of the target platform when assembling
  • Messages about run-time run steps are collected in the drop-down menu and do not close information about the progress of the assembly:

Database and Touch Bar support

  • Added a database support plugin, which includes management of SQL and databases from JetBrains DataGrip.
  • Run, collect and update the project, troubleshoot, and make changes made possible from the touch panel Touch Bar. Functions are configured in Preferences | Appearance & Behavior | Menus and Toolbars | Touch Bar.
  • Version control and interface
  • Added the ability to open an unlimited number of VCS Log tabs.
  • You can remove a tag from the git repository commit using the context menu of the Log tab.
  • The required repository component is opened in the Project Tool Window using the Browse Repository at Revision.
  • Skipping the Push dialog when using Commit and Push is configured in Preferences | Version Control | Git
  • Redesigned interface icons

Get more info here.

In late July 2018, JetBrains released an update for the PhpStorm development environment and introduced a new version of DataGrip. In addition, the company reported the termination of support for obsolete license servers.