How to declare a constant in swift that can be used in objective c

前端 未结 6 1510
刺人心
刺人心 2020-12-03 04:28

if I declare the swift constant as a global constant like:

let a = \"123\"

but the a cannot be found in

6条回答
  •  臣服心动
    2020-12-03 04:47

    You should not have any problem by using let in Objective-C, next example was made with Xcode 7.2 :

    MyClass.swift

    import Foundation
    import UIKit
    @objc class MyClass : NSObject { // <== @objc AND NSObject ARE BOTH NECESSARY!!!
    let my_color = UIColor( red:128/255,green:32/255,blue:64/255,alpha:1 ) // <== CONSTANT!!!
    }
    

    MyObjectiveC.m

    #import "PROJECTNAME-Swift.h" // <== NECESSARY TO RECOGNIZE SWIFT CLASSES!!!
    @interface MyObjectiveC ()
    @end
    @implementation MyObjectiveC
    @synthesize tableview; // <== ANY UI OBJECT, JUST AS EXAMPLE!!!
    - (void) viewDidLoad () {
    MyClass * mc = [ [ MyClass alloc ] init ]; // <== INSTANTIATE SWIFT CLASS!!!
    tableview.backgroundColor = mc.my_color;   // <== USE THE CONSTANT!!!
    }
    @end
    

    PROJECTNAME is the name of your Xcode project, as shown in Project Navigator.

提交回复
热议问题