iOS编程学习二十二(MapKit, CoreData和CloudKit)

今天用一个小时快速阅读了这本iOS开发的入门书,这本书据说很受欢迎。

Simon NG 的《AppCoda Beginning iOS 8 Programming with Swift》

这个作者水平非常高,写的很不错,强力推荐。因为质量很高,所以定价好像也不便宜。

虽然是入门,但是还是有很多知识点值得再巩固学习、补漏、加强。

比如MapKit、CoreData、CloudKit这几个章节,可以好好看看,还是收获颇丰的。

像Map,data,cloud都是几个框架,主要学习它们API的调用,这些功能非常实用,但是也不难。


Map Kit
通过一个地址来查找位置
let geoCoder = CLGeocoder ()geoCoder. geocodeAddressString ( "524 Ct St, Brooklyn, NY 11231" , completionHandler: { placemarks,error in
// Process the placemark
})

如果要增加一个提示:
let annotation = MKPointAnnotation() annotation. title = "Times Square" annotation.coordinate = placemark.location.coordinate
self . mapView . showAnnotations ([annotation], animated: true ) self . mapView . selectAnnotation (annotation, animated: true )

Core Data
Managed Object Context
在保存到数据库之前,主要是和这个Context打交道。
Persistent Store Coordinator
不同的数据库都可以,它来提供了具体功能的实现。
Managed Object Model
对象模型,数据类型,文件格式.xcdatamodeld

保存一个对象的示例:
if let managedObjectContext = ( UIApplication . sharedApplication (). delegate as AppDelegate ). managedObjectContext {
restaurant = NSEntityDescription .insertNewObjectForEntityForName( "Restaurant" , inManagedObjectContext: managedObjectContext) as Restaurant
restaurant.name = nameTextField. text restaurant.type = typeTextField. text restaurant.location = locationTextField. text restaurant . image = UIImagePNGRepresentation( imageView . image ) restaurant . isVisited = isVisited
var e: NSError ? if managedObjectContext. save (&e) != true {
println ( "insert error: \(e!.localizedDescription) " )
return
} }

CloudKit
Containers和Databases是最基础的对象。
CKContainer, CKDatabase

Record Zone保存的地方, CKRecord保存的记录

CloudKit提供了两种操作数据的API:
convenience API和operational API

从cloud database抓取数据:
let cloudContainer = CKContainer . defaultContainer () let publicDatabase = CKContainer. defaultContainer ().publicCloudDatabase let predicate = NSPredicate (value: true ) let query = CKQuery (recordType: "Restaurant" , predicate: predicate)publicDatabase. performQuery (query, inZoneWithID: nil , completionHandler: {
results, error in // Process the records })


在第22章看到了Jack Ma的名字。。。





猜你喜欢

转载自blog.csdn.net/starshus/article/details/77140907