每隔 class 从单个文件中调用一个结构

Call a structure from a single file in every other class

我为需要在其他 class 中调用的结构制作了一个文件,但我正在尽一切努力使其工作,但似乎我不会单独制作并弄清楚.

单个文件代码:

import Foundation

struct CustomLanguage {

func createBundlePath () -> Bundle {
    let selectedLanguage = "en" 
    let path = Bundle.main.path(forResource: selectedLanguage, ofType: "lproj")
    return Bundle(path: path!)!
    }
}

其他class:

import Foundation
import UIKit

class signInViewController: UIViewController {

@IBOutlet weak var welcomeLabelSignIn: UILabel!

override func viewDidLoad() {
    welcomeLabelSignIn.text = NSLocalizedString("Welcome!", tableName: nil, bundle: "the structure call here" , value: "", comment: "")
}

因此在捆绑包中:我需要调用该结构

the single file for the structure

I can't call it to localize the labels, buttons... in other classes

试试这个;

 struct CustomLanguage {

      static func createBundlePath () -> Bundle {
        let selectedLanguage = "en" 
        let path = Bundle.main.path(forResource: selectedLanguage,  ofType: "lproj")
       return Bundle(path: path!)!
     }
}

然后;

     welcomeLabelSignIn.text = NSLocalizedString("Welcome!", tableName: nil, bundle: CustomLanguage.createBundlePath() , value: "", comment: "")