swift命名空间
1、在oc中我们通过类名能非常方便的动态到创建对象。
- (id)productWithClassName:(NSString *)claName{
class cla = NSClassFromString(claName);
if (cla) {
id obj = [[cla alloc] init];
return obj;
}
}但是如果将这段代码直接转换成swift代码,一运行是会直接闪退的,因为swift中是存在命名空间的。swift中 import UIKit 等都是导入对应到命名空间,swift中同一个命名空间类是不需要 import 。swift中类的完成命名应该是"命名空间"+.+"类名"。
下面是一个通过配置 tableView的dataSource 来跳转不同到界面
class ViewController: UIViewController {
public var dataSource:[[String:String]] = [["title":"pageOne","class":"PageOneViewController"],
["title":"pageTwo","class":"PageTwoViewController"]];
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.title = "首页"
let tableView:UITableView = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.plain);
tableView.delegate = self;
tableView.dataSource = self;
self.view.addSubview(tableView);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}关键代码: 先动态取到当前项目的命名空间, 然后通过命名空间和类名拼成完整到类名
Last updated