PerhapYs的OC学习日记自己的swiftSwift开发

swift3.0和Objective-C的交互需要注意这些

2016-06-16  本文已影响2262人  ZeroJ

前言:

swift3.0出来后, 可以看到改变很大, 和cocoa, Foundation...的交互也变得更方便了, 同时swift编写的应用适配到iOS7, 所以, 我们可以相信: 在未来使用swift的情况会逐渐增加了, 同时会涉及到oc和swift在项目中并存的情况, 这里我重新读了官方的'Using swift with Cocoa and Objective-C(swift3)'的文档, 记录了一些个人觉得比较常用的笔记, 请大家选择性阅读(里面的代码 均来自文档)

  1. oc的初始化方法在swift中被引为
    init(...) --- 如果初始化不会失败
    init?(...) --- 如果初始化可能失败
    init!(...) --- 否则
class Singleton {
   static let sharedInstance = Singleton()
} 
或者
class Singleton {
   static let sharedInstance: Singleton = {
       let instance = Singleton()
       // setup code
       return instance
   }()
}
Color ColorCreateWithCMYK(float c, float m, float y, float k) CF_SWIFT_NAME(Color.init(c:m:y:k:));  
对应为swift中
extension Color {
    init(c: Float, m: Float, y: Float, k: Float)
}
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
   UITableViewCellStyleDefault,
   UITableViewCellStyleValue1,
   UITableViewCellStyleValue2,
   UITableViewCellStyleSubtitle
};
//对应与
enum UITableViewCellStyle: Int {
    case `default`
    case value1
    case value2
    case subtitle
}
typedef enum {
   MessageDispositionUnread = 0,
   MessageDispositionRead = 1,
   MessageDispositionDeleted = -1,
} MessageDisposition;
对应与
struct MessageDisposition: RawRepresentable, Equatable {}
var MessageDispositionUnread: MessageDisposition { get }
var MessageDispositionRead: MessageDisposition { get }
var MessageDispositionDeleted: MessageDisposition { get }
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
        UIViewAutoresizingNone                 = 0,
        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
        UIViewAutoresizingFlexibleWidth        = 1 << 1,
        UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
        UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
        UIViewAutoresizingFlexibleHeight       = 1 << 4,
        UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
对应与
public struct UIViewAutoresizing : OptionSet {
    public init(rawValue: UInt)
    
    public static var flexibleLeftMargin: UIViewAutoresizing { get }
    public static var flexibleWidth: UIViewAutoresizing { get }
    public static var flexibleRightMargin: UIViewAutoresizing { get }
    public static var flexibleTopMargin: UIViewAutoresizing { get }
    public static var flexibleHeight: UIViewAutoresizing { get }
    public static var flexibleBottomMargin: UIViewAutoresizing { get }
}
在swift中直接使用  let resize = [. flexibleLeftMargin, . flexibleWidth...]
#if arch(arm) || arch(arm64)
#if swift(>=3.0)
print("Using Swift 3 ARM code")
    #else
    print("Using Swift 2.2 ARM code")
#endif
#elseif arch(x86_64)
print("Using 64-bit x86 code.)
    #else
    print("Using general code.")
#endif
例如这个函数接受的参数可以传许多种
let x: Float = 0
func takesAPointer(_ p: UnsafePointer<Float>!) {
    // ...
}
takesAPointer(&x)
takesAPointer([0.0,1.0])
@objc public protocol MySwiftProtocol {
// 必须实现
    func requiredMethod()
    
    @objc optional func optionalMethod()
}
上一篇 下一篇

猜你喜欢

热点阅读