Swift与OC混编

以前用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 设置为桥接器头文件,如未设置可手动设置.









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值