Add the Swift file to the OC project and set the Oc-swift
Source: Internet
Author: User
In the project right-click Add a swift file, I added the Learnswift.swift add after Xcode popup ' whether to configure OC Bridge header file ' Prompt, click Confirm, the system automatically generates a project name-bridging-header.h file, mine is Learnswift-bridging-header.h.
Tips for configuring OC Bridge header Files Learnswift-bridging-header.h is essentially a header file for Swift, where all OC header files to be exposed to Swift are added, so that you can use the OC classes and methods directly when you write Swift.
Because Swift itself has no header files, only. Swift, and OC has. h and. m files. With this statement,. Swift can directly use OC's. h
My learnswift-bridging-header.h file content you can then write the SWIFT code directly in the Learnswift.swift.-Just declare a class, and hit a log:
My Learnswift.swift file contents
Compiled directly through, this is the use of OC classes and methods in Swift, everything goes well.
usingSwift in OC, I'm using the Swift Class I just created in appdelegate.m, so I'll refer to the swift header file, which is #import "Learnswift-swift.h", The classes and methods created by Swift can then be used arbitrarily in this. m file:
Learnswift *learnswift = [[Learnswift alloc] init]; [Learnswift logsth:@ "code from OC"];
The compilation runs directly over. Output:
This was a log from the Swift:code from OC #import "Learnswift-swift.h"
In fact, it is the project name-swift.h, which is also xcode automatically generated, according to all the SWIFT code you write, generate an OC. h file, the class and method declaration, so that in OC reference to this header file, is equivalent to quoting all Swift statements, can be used directly.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.