小编Nic*_*rbo的帖子

CLLocationManager获取位置很慢,Swift

我正在创建这个应用程序,它需要获取用户位置 - 它一切正常,事实上,从接受使用位置服务到获取实际位置需要5秒的时间 - 这是正常的吗?我使用过其他应用程序,速度更快..

这是我的代码的样子:

override func viewDidLoad() {
    super.viewDidLoad()

    // Ask for Location-Authorisation from the User.
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.requestLocation()
    }

    mapView.delegate = self

}



func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue: CLLocationCoordinate2D = manager.location!.coordinate

    let initialLocation = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)

    self.centerMapOnLocation(initialLocation)
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("could not get location")
}
Run Code Online (Sandbox Code Playgroud)

但是从应用程序获取位置放入centerMapOnLocation函数的时间似乎只是很长.获取用户位置时会出现什么情况?我正在测试一个wifi连接,所以我知道它不是因为互联网很慢,或者连接不好......

有人有想法吗?:)

最好的祝福!

mapkit cllocationmanager swift location-services

7
推荐指数
2
解决办法
3185
查看次数

Firebase FCM giving Internal Server Error 6/10 times

I've implemented Firebase FCM for push notifications in my app, and it works great - except from, as my topic describes, it gives me this response aprox 6/10 times:

{
    error = InternalServerError;
}
)
, "multicast_id": 8370179651570531666, "failure": 1, "canonical_ids": 0, "success": 0]
Run Code Online (Sandbox Code Playgroud)

The rest of the times, it delivers the notification as expected!

I'm sending my notifications from my app, through https://fcm.googleapis.com/fcm/send as a POST-request like this:

func sendFcmNotificationToUser(title: String, message: String, to: String, sound: Bool, badge: Bool) …
Run Code Online (Sandbox Code Playgroud)

objective-c firebase swift firebase-cloud-messaging

5
推荐指数
1
解决办法
464
查看次数

在 Swift 中编辑 textField 时如何刷新键盘/返回键?

当我的所有文本字段都包含文本时,我试图让我的应用程序将 returnKeyType 从 Next 更改为 Go。我一直在寻找的任何地方,人们都在谈论 textField.reloadInputViews() - 但他们也谈到在 iOS8 之后这个方法不再起作用了。那么有什么替代方法呢?我试着做一个观察者,检查文本字段是否包含东西,这似乎工作正常 - 但我的 returnKey 没有改变,在我改变 textField 之前 - 我需要它“实时”改变,只要有我所有字段中的文本。

任何人都可以帮我解决问题吗?:-)

此致!

ios swift swift2

3
推荐指数
1
解决办法
1092
查看次数

可解码和 JSON,同一变量有 2 种数据类型

我正在使用 Decodable 协议来解码一些 json,但我遇到了一个问题:

我得到了一个答案,其中经度和纬度可以是整数(纬度 = 0),如果没有添加到元素的地理位置数据,如果有地理数据,则可以是字符串(fx。纬度 =“25.047880”)可用的。现在,当我解码 json 时,我不知道如何构建我的 Struct,因为 long 和 lat 不能同时是 String 和 Int。

有关如何解决此问题的任何建议?我尝试使用“Any”作为数据类型,但这不符合可解码协议

struct JPhoto: Decodable {
  let id: String
  let farm: Int
  let secret: String
  let server: String
  let owner: String
  let title: String
  let latitude: String //Can both be Int and String
  let longitude: String //Can both be Int and String
}
Run Code Online (Sandbox Code Playgroud)

json swift4 decodable swift4.1

3
推荐指数
1
解决办法
474
查看次数

使用NSPredicate过滤属性上的对象数组

我有一系列的工作室.每个工作室都有一个名为studioName的属性.

我需要我的搜索字段来过滤工作室,基于studioName,由用户应用于搜索字段.

这是我目前的代码:

var studios = [Studio]()
var filteredStudios = [Studio]()
var studiosToDisplay = [Studio]()

func updateSearchResultsForSearchController(searchController: UISearchController) {
    let searchText = searchController.searchBar.text

    print("SEARCH TEXT: \(searchText)")

    if searchText == nil || searchText!.isEmpty {
        studiosToDisplay = studios
        self.resultTableView.reloadData()
        NSNotificationCenter.defaultCenter().postNotificationName("showResultsBeforeSearchingNotification", object: nil) //Calls SearchVC
    } else {
        studiosToDisplay.removeAll()
        let searchPredicate = NSPredicate(format: "studioName CONTAINS[c] %@", searchText!)
        let array = (self.studios as NSArray).filteredArrayUsingPredicate(searchPredicate)
        studiosToDisplay = array as! [Studio]
        self.resultTableView.reloadData()
    }
}
Run Code Online (Sandbox Code Playgroud)

它现在只是没有工作..它过滤了,但无论我放在搜索域中,我每次都会留下同样的工作室.

我想我需要知道谓词,每次都必须查看数组中的单个对象.但我无法弄清楚如何.我尝试在格式中添加"ANY",但这会导致我的应用崩溃.

arrays filtering nspredicate swift

2
推荐指数
1
解决办法
2582
查看次数

如何在UILabel中添加函数调用(不是超链接)到NSAttributedString的一部分?

我需要在文本的某些部分添加一些tapGestures,即UILabel中的那些部分.它似乎能够创建一个超链接 - 所以我想它也可以做一个函数调用,但我只是不知道该怎么做.

继承我的代码:

    let regularFont = UIFont(name: Constants.robotoRegular, size: 13)!
    let att1 = [NSFontAttributeName: regularFont]

    let turqoiseFont = UIFont(name: Constants.robotoBold, size: 13)!
    let att2 = [NSFontAttributeName: turqoiseFont]

    let attString1 = NSMutableAttributedString(string: "By creating a profile, I accept ", attributes: att1)
    attString1.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSMakeRange(0, attString1.length))

    let attString2 = NSMutableAttributedString(string: "Terms and Conditions ", attributes: att2)
    attString2.addAttribute(NSForegroundColorAttributeName, value: Colors.loginButtonColor, range: NSMakeRange(0, attString2.length))

    let attString3 = NSMutableAttributedString(string: "and ", attributes: att1)
    attString3.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSMakeRange(0, attString3.length))

    let attString4 = …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring uilabel ios uitapgesturerecognizer swift

1
推荐指数
1
解决办法
1151
查看次数