{"id":18839759,"url":"https://github.com/maximbilan/ios-swift-objc-mix","last_synced_at":"2025-04-14T07:03:37.948Z","repository":{"id":25945476,"uuid":"29387010","full_name":"maximbilan/iOS-Swift-ObjC-Mix","owner":"maximbilan","description":"An example of the mixing Swift and Obj C code in the same project","archived":false,"fork":false,"pushed_at":"2018-09-22T07:21:39.000Z","size":406,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T20:44:27.529Z","etag":null,"topics":["ios","objective-c","swift","xcode"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maximbilan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-17T09:56:10.000Z","updated_at":"2023-04-17T11:50:21.000Z","dependencies_parsed_at":"2022-08-24T14:17:58.259Z","dependency_job_id":null,"html_url":"https://github.com/maximbilan/iOS-Swift-ObjC-Mix","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-Swift-ObjC-Mix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-Swift-ObjC-Mix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-Swift-ObjC-Mix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-Swift-ObjC-Mix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maximbilan","download_url":"https://codeload.github.com/maximbilan/iOS-Swift-ObjC-Mix/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248837285,"owners_count":21169374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios","objective-c","swift","xcode"],"created_at":"2024-11-08T02:43:59.750Z","updated_at":"2025-04-14T07:03:37.918Z","avatar_url":"https://github.com/maximbilan.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"An example of the mixing Swift and Obj C code in the same project\n============\n\nYes, there is opportunity to combine this languages in the same project. It’s great. Apple provides a tutorial how to do this magic. You can found \u003ca href=\"https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html\"\u003ehere\u003c/a\u003e. But in spite of it I would like to share my experience on real examples, because it’s not so simple as it says.\n\n## Swift code in Obj C project\n\nLet’s start from \u003ci\u003eObj C\u003c/i\u003e project, and will try to add swift code to this project.\n\nCreate the \u003ci\u003eObj C\u003c/i\u003e project.\n\nAnd create new \u003ci\u003eSwift\u003c/i\u003e class. For example, \u003ci\u003eUIViewController\u003c/i\u003e:\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/1.png)\n\u003cbr\u003e\n\u003cbr\u003e\nAfter that, you will see the following popup message:\n\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/2.png)\n\u003cbr\u003e\n\u003cbr\u003e\nPlease, choose \u003ci\u003e‘Yes’\u003c/i\u003e.\n\nNow, you have \u003ci\u003eios_objc_mix-Bridging-Header.h\u003c/i\u003e file. In this header you can import source files for your \u003ci\u003eSwift\u003c/i\u003e class.\n\nAfter that, you can do the next steps.\u003cbr\u003e\n\u003cbr\u003e\n1. Implement you \u003ci\u003eSwift\u003c/i\u003e class with \u003ci\u003e@objc\u003c/i\u003e attribute:\u003cbr\u003e\n\u003cpre\u003e\n//\n//  SwiftController.swift\n//  ios_objc_mix\n//\n//  Created by Maxim Bilan on 1/17/15.\n//  Copyright (c) 2015 Maxim Bilan. All rights reserved.\n//\n\nimport UIKit\n\n@objc class SwiftController: UIViewController {\n    \n}\n\u003c/pre\u003e\n\u003cbr\u003e\n2. \u003ci\u003eDefines module\u003c/i\u003e set to \u003ci\u003e‘YES’\u003c/i\u003e:\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/4.png)\u003cbr\u003e\u003cbr\u003e\n3. \u003ci\u003eEmbedded Content Contains Swift\u003c/i\u003e set to \u003ci\u003e‘YES’\u003c/i\u003e:\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/5.png)\u003cbr\u003e\u003cbr\u003e\n4. After that you should found the \u003ci\u003eProduct Module Name\u003c/i\u003e in your target settings:\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/6.png)\u003cbr\u003e\u003cbr\u003e\n5. And please include a header for \u003ci\u003eSwift\u003c/i\u003e compatibility for example to a pch file.\u003cbr\u003e\n\n\u003cpre\u003e\n#import “Product Module Name-Swift.h”\n\u003c/pre\u003e\n\n\u003cpre\u003e\n//\n//  ios_objc_mix-PrefixHeader.pch\n//  ios_objc_mix\n//\n//  Created by Maxim Bilan on 1/17/15.\n//  Copyright (c) 2015 Maxim Bilan. All rights reserved.\n//\n\n#ifdef __OBJC__\n    #import \u0026#60;UIKit/UIKit.h\u0026#62;\n    #import \u0026#60;Foundation/Foundation.h\u0026#62;\n#endif\n\n#import \"ios_objc_mix-Swift.h\"\n\n\u003c/pre\u003e\n\u003cbr\u003e\nAnd now you can use \u003ci\u003eSwift\u003c/i\u003e code☺\n\nPlease, see the example on \u003ca href=\"https://github.com/maximbilan/ios_swift_objc_mix\"\u003egithub\u003c/a\u003e.\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/8.png)\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/9.png)\u003cbr\u003e\u003cbr\u003e\n\u003cb\u003eNote:\u003c/b\u003e ios_objc_mix-Swift.h file you can’t find in your project browser ☹ Xcode automatically generates this header. Magic!\n\n\u003cb\u003eNote:\u003c/b\u003e if you have a lot of targets in your project. It really is a pain. You should import magic Product-module-name-Swift.h headers for each target.\n\n\u003cb\u003eNote:\u003c/b\u003e if you use \u003ci\u003eSwift\u003c/i\u003e classes in the \u003ci\u003eInterface Builder\u003c/i\u003e, you should set up the module.\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/10.png)\u003cbr\u003e\u003cbr\u003e\n## Obj C code in Swift project\n\nIt’s a similar process. And simpler.\n\nAdd \u003ci\u003eObj C\u003c/i\u003e file to project.\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/11.png)\u003cbr\u003e\nAnd also please choose \u003ci\u003e‘YES’\u003c/i\u003e:\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/12.png)\u003cbr\u003e\nInclude our \u003ci\u003eObj C\u003c/i\u003e View Controller to bridging header:\u003cbr\u003e\n\u003cpre\u003e\n//\n//  Use this file to import your target's public headers that you would like to expose to Swift.\n//\n\n#import \"ObjCViewController.h\"\n\n\u003c/pre\u003e\nAnd that’s all.\n\nYou also you can find example on \u003ca href=\"https://github.com/maximbilan/ios_swift_objc_mix\"\u003e github\u003c/a\u003e.\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/14.png)\u003cbr\u003e\n![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/15.png)\u003cbr\u003e\u003cbr\u003e\nHopefully this will save time for someone. Don’t forget star the github repository ☺\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximbilan%2Fios-swift-objc-mix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaximbilan%2Fios-swift-objc-mix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximbilan%2Fios-swift-objc-mix/lists"}