以前用OC写了很多功能函数,直接全部用Swift改写还得花大量的时间。
如果是赶时间的项目可以在Swift中先引用原来的OC功能函数将新项目上线,后面再慢慢迭代~
Swift项目中引用OC文件:
第一步:
将以前写好的OC的.h和.m文件拖入Swift Project的时候,Xcode会自动弹出加入桥接器头文件的提示,点击Yes
第二步:
Project中生成一个新文件XX-Bridging-Header.h,此时这个文件中按OC格式引入需要在Swift中使用的头文件,如: #import "HeaderParallax.h"
// Swift@OC-Bridging-Header.h
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "HeaderParallax.h"
第三步:
在Swift中无需import,直接使用
//
// ViewController.swift
// Swift@OC
//
// Created by mingMac on 14-8-19.
// Copyright (c) 2014年 com.ming. All rights reserved.
//
import UIKit
class ViewController: UITableViewController {
var headerParallax = HeaderParallax()
override func viewDidLoad() {
super.viewDidLoad()
var header = UIImageView(image: UIImage(named: "3.png"))
headerParallax.InitHeaderForTableView(self.tableView, withView: header)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func scrollViewDidScroll(scrollView: UIScrollView!) {
headerParallax.scrollViewDidScroll(scrollView)
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return 0
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
return cell
}
}
PS:一般情况下Xcode会自动在Buliding Setting 中将Objective-C bridging Header 设置为桥接器头文件,如未设置可手动设置.