小编Ada*_*son的帖子

对Swift 2中registerUserNotificationSettings的更改?

我似乎找不到有关registerUserNotificationSettings的任何文档,超出了去年11月(这里)生成的文件,但我的旧代码似乎在Xcode 7和Swift 2中不再适用于我.

我在App Delegate中有这个代码:

let endGameAction = UIMutableUserNotificationAction()
endGameAction.identifier = "END_GAME"
endGameAction.title = "End Game"
endGameAction.activationMode = .Background
endGameAction.authenticationRequired = false
endGameAction.destructive = true

let continueGameAction = UIMutableUserNotificationAction()
continueGameAction.identifier = "CONTINUE_GAME"
continueGameAction.title = "Continue"
continueGameAction.activationMode = .Foreground
continueGameAction.authenticationRequired = false
continueGameAction.destructive = false

let restartGameCategory = UIMutableUserNotificationCategory()
restartGameCategory.identifier = "RESTART_CATEGORY"
restartGameCategory.setActions([continueGameAction, endGameAction], forContext: .Default)
restartGameCategory.setActions([endGameAction, continueGameAction], forContext: .Minimal)

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: (NSSet(array: [restartGameCategory])) as Set<NSObject>))
Run Code Online (Sandbox Code Playgroud)

我现在在最后一行代码中收到以下两个错误:

'Element.Protocol'没有名为'Alert'的成员

无法使用类型为'(UIUserNotificationSettings)'的参数列表调用'registerUserNotificationSettings'

我搜索了有关任何变化的信息,但我找不到任何东西.我错过了一些明显的东西吗

uilocalnotification swift swift2

15
推荐指数
2
解决办法
8878
查看次数

MKPolyLine没有出现在Swift的MapView上

我在Swift中有以下代码将MKPolyline添加到MapView.XCode没有告诉我有一个问题,据我所知,这应该是有效的.

MapView的插座:

@IBOutlet weak var mapView: MKMapView!
Run Code Online (Sandbox Code Playgroud)

用于保持坐标的变量:

var coordinates: [CLLocationCoordinate2D] = []
Run Code Online (Sandbox Code Playgroud)

从Core Data获取我保存的坐标:

var contextMap = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
var requestMap = NSFetchRequest(entityName: "Locations")
let predMap = NSPredicate(format: "game = %d", passedGameNumber)
requestMap.predicate = predMap
requestMap.sortDescriptors = [NSSortDescriptor(key:"time", ascending: false)]
    self.locationsList = contextMap.executeFetchRequest(requestMap, error: nil)! as [Locations]
Run Code Online (Sandbox Code Playgroud)

将Core Data中的坐标添加到我的新数组中:

for index in 1..<self.locationsList.count{
    var lat = Double(self.locationsList[index].latitude)
    var long = Double(self.locationsList[index].longitude)
    var coordinatesToAppend = CLLocationCoordinate2D(latitude: lat, longitude: long)
    coordinates.append(coordinatesToAppend)
}
Run Code Online (Sandbox Code Playgroud)

创建折线:

var polyLine = MKPolyline(coordinates: &coordinates, count: coordinates.count) …
Run Code Online (Sandbox Code Playgroud)

mkmapview mkpolyline cllocationcoordinate2d swift

6
推荐指数
2
解决办法
6007
查看次数

缩放MKMapView以适应Swift中的折线点

我一直在寻找一种解决方案来缩放MapView以适应Swift中MKPolyline的边界.我已经能够在SO上找到Objective-C的示例代码,但我并不熟悉Objective-C或者如何将它转换为Swift.

有没有人在Swift中有这样的例子?谢谢.

-(void)zoomToPolyLine: (MKMapView*)map polyline: (MKPolyline*)polyline animated: (BOOL)animated
{
[map setVisibleMapRect:[polyline boundingMapRect] edgePadding:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0) animated:animated];
}
Run Code Online (Sandbox Code Playgroud)

objective-c mkmapview mkpolyline swift

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

从Swift中的UICollectionView查询

这个问题可能有一个非常简单的答案,但我无法理解它.我也找不到可以在网上工作的特别有用的答案.

我根本无法从UICollectionViewCell获得一个segue.我可能从错误的角度来看它,因为我认为它的工作方式非常类似于表格视图.在这个前提下工作,如果我使用表视图,它看起来像这样:

if segue.identifier == "TripsToPastTripDetailsSegue" {
        let newViewController = segue.destinationViewController as! pastTripDetailViewController
        let selectedRow: NSManagedObject = tripsList[tableView.indexPathForSelectedRow!.row] as NSManagedObject
        newViewController.passedTrip = selectedRow as! Trips   
    }
Run Code Online (Sandbox Code Playgroud)

因此,使用表视图,我将一个对象(从Core Data)传递到另一个视图控制器.简单.但我无法将其转化为集合视图.

我一直在尝试这些方面:

if segue.identifier == "pastTripCollectionToSavedLocationSegue" {
        let newViewController = segue.destinationViewController as! pastTripDetailViewController
        let selectedRow: NSManagedObject = locationsList[collectionView.indexPathForCell(pastLocationImageCollectionViewCell)] as! NSManagedObject
        newViewController.passedTrip = selectedRow as! Trips   
    }
Run Code Online (Sandbox Code Playgroud)

但这给了我一个无法找到成员'indexPathForCell'的错误,所以我显然做了一些非常错误的事情.这是我在玩代码时遇到的许多错误之一.

有人能指出我正确的方向吗?

编辑:

根据@Laffen的要求,UICollectionView出现在代码中的以下位置:

class pastTripDetailViewController: UIViewController,  UICollectionViewDataSource, UICollectionViewDelegate, MKMapViewDelegate {
Run Code Online (Sandbox Code Playgroud)

作为IBOutlet:

@IBOutlet weak var collectionView: UICollectionView!
Run Code Online (Sandbox Code Playgroud)

ViewDidLoad中的数据源和委托:

self.collectionView.dataSource = self
self.collectionView.delegate = self
Run Code Online (Sandbox Code Playgroud)

剩下的就是: …

uicollectionview uicollectionviewcell swift swift2

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

Swift中的CLGeocoder - 使用reverseGeocodeLocation时无法返回字符串

我正在尝试使用CLGeocoder返回字符串中坐标的位置.我的代码目前看起来像这样:

func getPlaceName(latitude: Double, longitude: Double) -> String {

let coordinates = CLLocation(latitude: latitude, longitude: longitude)
var answer = ""

CLGeocoder().reverseGeocodeLocation(coordinates, completionHandler: {(placemarks, error) -> Void in
    if (error != nil) {
        println("Reverse geocoder failed with an error" + error.localizedDescription)
answer = ""
    }

    if placemarks.count > 0 {
        let pm = placemarks[0] as CLPlacemark
        answer = displayLocationInfo(pm)
    } else {
        println("Problems with the data received from geocoder.")
answer = ""
    }
})

return answer

}

func displayLocationInfo(placemark: CLPlacemark?) -> …
Run Code Online (Sandbox Code Playgroud)

clgeocoder swift

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

从 Core Data 创建一个 CLLocation 坐标数组

也许我找不到这个问题的答案,因为它非常简单,我应该能够弄清楚,但我很难过。哦,如果我的一些术语被关闭,我很抱歉 - 我还在学习。

我正在使用 Swift 并且有一个从 Core Data 派生的数组。到现在为止还挺好。此数组中的两个元素是存储纬度和经度的 Doubles/NSNumbers。我将使用这两个元素在地图上绘制,但我不知道如何将这两个元素放入它们自己的 CLLocations 数组中。

所以,我在 Core Data 中得到了所有数据的数组:

var locationsList: [Locations] = []

 var context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
    var request = NSFetchRequest(entityName: "Locations")
    let pred = NSPredicate(format: "game = %d", passedGameNumber)
    request.predicate = pred
    request.sortDescriptors = [NSSortDescriptor(key:"time", ascending: false)]
    self.locationsList = context.executeFetchRequest(requestMap, error: nil)! as [Locations]
Run Code Online (Sandbox Code Playgroud)

但它保存了 Core Data 中所有内容的数据(按游戏过滤):

class Locations: NSManagedObject {

@NSManaged var game: NSNumber
@NSManaged var time: NSDate
@NSManaged var latitude: NSNumber
@NSManaged var longitude: NSNumber

} …
Run Code Online (Sandbox Code Playgroud)

arrays core-data cllocation swift

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