快做了三个月了,现在好好来总结一下自己做的第一个项目,istudy,虽然还没做完,现总结一部分吧
swift中正则表达式的应用,正则表达式,主要是应用在对于邮箱,手机,QQ号等一些字符串的处理上的
首先定义正则表达式
struct RegexHelper {
let regex: NSRegularExpression
init(_ pattern: String) throws {
try regex = NSRegularExpression(pattern: pattern,
options: .CaseInsensitive)
}
func match(input: String) -> Bool {
let matches = regex.matchesInString(input,
options: [],
range: NSMakeRange(0, input.utf16.count))
return matches.count > 0
}
}
随后初始化正则表达式
let PhonePattern = "^((13[0-9])|(17[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"
随后初始化正则表达式,进行匹配
let matcher: RegexHelper
do {
var phoneText = self.changeNameTextField?.text
phoneText = phoneText!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
matcher = try RegexHelper(PhonePattern)
if matcher.match(phoneText!){
userDefault.setValue(phoneText, forKey:dic[(self.name?.text)!]!)
ProgressHUD.showSuccess("保存成功")
self.navigationController?.popViewControllerAnimated(true)
}else{
ProgressHUD.showError("填写手机格式错误")
}
}catch{
print("error")
}
地图定位的使用 之前比较生疏
实现代理
CLLocationManagerDelegate
记录位置的字符串和定位服务管理类和经纬度管理类
//记录位置
var address = NSString()
var locationManager:CLLocationManager!
//经纬度
var currLocation:CLLocation?
定义位置管理的精度问题和移动多少距离开始跟新定位
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
//精度
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
//指定水平或者竖直位置发生多少平移后开始跟新
locationManager.distanceFilter = 200
发出定位的邀请
//发出授权
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.requestAlwaysAuthorization()
获得设备是否允许定位服务
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.NotDetermined || status == CLAuthorizationStatus.Denied
{
//允许使用定位服务
locationManager.startUpdatingLocation()
}
}
当距离改变的时候开始重新定位
//当距离改变的时候回调用下面的代码
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let currLocation = locations.last! as CLLocation
self.currLocation = currLocation
self.reverseGeocoder()
}
//当定位失败时出现一下信息
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
ProgressHUD.showError("定位失败")
}
输出地理的反编码
//输出反编码的信息
func reverseGeocoder() {
let geocoder = CLGeocoder()
var p: CLPlacemark?
geocoder.reverseGeocodeLocation(currLocation!, completionHandler: { (placemarks, error) -> Void in
//强制成中文
//显示所有信息
if error != nil
{
ProgressHUD.showError("定位失败")
return
}
let pm = placemarks! as [CLPlacemark]
if pm.count > 0
{
p = placemarks!.last! as CLPlacemark
self.address = (p?.name)!//输出反编码信息
self.addressTextField?.text = self.address as String
}
else
{
ProgressHUD.showError("定位失败")
}
})
}
还要在plist文件中增添配置
这样定位就基本可以了
Core Data的使用
首先import CoreData
随后定义Core Data
随后创建继承自该类的文件
import Foundation
import CoreData
extension PersonalHeadPortrait {
@NSManaged var headPortraitData: NSData?
}
创建该类的例子
var fetchedResults = [PersonalHeadPortrait]()
这里是头像的二进制编码,将得到的图片保存到coreData
//做save的一些事情 将头像保存 还有后台数据也要存储
let app = UIApplication.sharedApplication().delegate as! AppDelegate
self.managedContext = app.managedObjectContext
let entity = NSEntityDescription.entityForName("PersonalHeadPortrait", inManagedObjectContext: self.managedContext!)
let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: self.managedContext) as! PersonalHeadPortrait
person.headPortraitData = self.selectedImageData
do {try self.managedContext?.save()
ProgressHUD.showSuccess("保存成功")
self.navigationController?.popViewControllerAnimated(true)
}catch{
ProgressHUD.showError("保存失败")
}
}
随后获取的时候
let app = UIApplication.sharedApplication().delegate as! AppDelegate
dispatch_async(dispatch_get_main_queue()) {
self.managedContext = app.managedObjectContext
if(self.isFromFromImagePicker == false){
let fetchResquest = NSFetchRequest(entityName: "PersonalHeadPortrait")
do { self.fetchedResults = try self.managedContext?.executeFetchRequest(fetchResquest) as! [PersonalHeadPortrait]
if(self.fetchedResults.count > 0){
self.selectedImageData = (self.fetchedResults.last?.headPortraitData)!
}else{
self.selectedImageData = UIImagePNGRepresentation(UIImage(named: "默认头像")!)!
}
self.headPortraitImageView?.image = UIImage(data: self.selectedImageData)
}catch{
ProgressHUD.showError("读取图像失败")
}
}
这样即可