swift初探(供objective c开发人员參考)

6月初的wwdc苹果推出了一门新的开发语言swift.系统10.9.3以上安装xcode6 beta版就可以体验swift。

苹果公司做了尽可能多的努力让这门语言迅速成为一个工业级的有用编程语言,而不是华而不实的花瓶。


通过xcode6查看文档,能够发现之前现存的接口都多了swift适配。举比例如以下(下面都从苹果官方文档中摘录):

简单方法适配:

swift:

func UIImageJPEGRepresentation(_ image: UIImage!,
                             _ compressionQuality: CGFloat) -> NSData!

oc:

NSData * UIImageJPEGRepresentation (UIImage *image, CGFloat compressionQuality);


带block和queue的方法适配:

Starts accelerometer updates on an operation queue and with a specified handler.

Declaration

SWIFT

func startAccelerometerUpdatesToQueue(_ queue: NSOperationQueue!,
                          
withHandler handler: CMAccelerometerHandler!)

OBJECTIVE-C

- (void)startAccelerometerUpdatesToQueue:(NSOperationQueue *)queue
                             withHandler:(CMAccelerometerHandler)handler



属性的适配:

SWIFT

var acceleration: CMAcceleration { get }


OBJECTIVE-C

@property(readonly, nonatomic) CMAcceleration acceleration


在我看来,熟悉这门语言的最好方法。就是使用swift去写代码。把之前的ocproject(从小到大)通过swift来实现,这样逐步的熟悉语言的特性。


原文地址:https://www.cnblogs.com/cynchanpin/p/6790193.html