iOS使用swift制作定位的pod库

  很早的时候就学习swift了,但是一直没有机会去实践,最近swift版本逐渐的稳定下来了,而且swift的语言写起来更加的简洁,加上年前已经用swift写了一个工具app,最近在做项目优化,打算把定位功能封装成一个pod库,由于功能简单,决定用swift语言来写。里面坑点仍然不少,这里做一下分享, 希望大家可以避开这些坑。

地图定位JKLocationMananger简介

  这个地图pod库非常的简单只有两个方法,第一个方法就是定位获取经纬度信息,第二个方法就是定位获取城市名字。具体代码如下:

public class func startLocate(success:((_ latitude:String? ,_ longitude:String?) ->Void)?,failure:((_ error:Error?) ->Void)?) ->Void{
        if CLLocationManager.locationServicesEnabled() {
            
            JKLocationMananger.shareInstance.failureBlock = failure
            JKLocationMananger.shareInstance.successBlock = {(_ currentLocation:CLLocation?) ->Void in
                if let success = success {
                    
                    success("\(currentLocation!.coordinate.latitude)","\(currentLocation!.coordinate.longitude)")
                    
                }
            }
            JKLocationMananger.shareInstance.locationManager .startUpdatingLocation()
        }
    }
    
   public class func locateCity(success:((_ city:String?) ->Void)?,failure:((_ error:Error?) ->Void)?) ->Void{

        if CLLocationManager.locationServicesEnabled() {
            JKLocationMananger.shareInstance.failureBlock = failure
            JKLocationMananger.shareInstance.successBlock = {(_ currentLocation:CLLocation?) ->Void in
                if let success = success {
                    let geocoder = CLGeocoder.init()
                    geocoder.reverseGeocodeLocation(currentLocation!, completionHandler: { (placemarks, error) in
                        let place = placemarks!.last
                        let city = place!.locality
                        success(city)

                    })

                }

            }
        JKLocationMananger.shareInstance.locationManager .startUpdatingLocation()
        }

    }

源码地址:https://github.com/xindizhiyin2014/JKLocationMananger
也可以使用pod更新,pod 'JKLocationMananger'
下面我就和大家分享一下制作swift版本的pod库遇到的坑吧,由于目前项目是混编,制作时也充分考虑到了混编的问题。

OC项目混编swift遇到buildingSetting的问题

  我的OC和swift的混编是参考如下这篇文章进行的,原文地址:https://www.jianshu.com/p/754396e7e1bd 根据文章的提示,我参考了OC项目混编swift文件,但是是的过程中发现没有swift的配置,让我奇怪了好一会儿呢,直到创建了test.swift文件以后,swift相关的配置才在buildSetting中出现。

关键字protect,private,public

  虽然OC中也有protect,private,public这些关键字,但是经常使用的时候发现很少用到,主要是通过方法名在头文件中是否声明来区分,但是这些遇到了OC的黑科技,就没有任何意义了。在swift中如果方法不想被外部访问一定要加private,另外如果想要外部访问, 一定要加public关键字。还有就是如果没有加public的话,混编时会找不到相关的方法。

类的关键字 @objcMembers

   一个swift类如果想要在和OC混编是一定要个这个关键字,另外还要加上public,不然找不到类。
在这里插入图片描述

swift中的类型在OC中不支持

  如果swift中的方法参数或者结果中使用了OC不支持的类型,那么混编的时候相关的方法就不会自动生成,比如Double这个类型在OC中就没有,如果参数中有这个类型的,那么混编的时候对应的方法无法自动生成支持OC的方法。具体如下:
在这里插入图片描述
这个方法的block中参数原来是Double类型的,混编后发现方法找不到,后来为了支持混编,我改成了String类型。
更多优质文章,可以微信扫码关注:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/HHL110120/article/details/88756947