0

我在一个 ios 应用程序上工作,该应用程序需要使用来自谷歌的附近地方 api。它与 swift 配合得很好,但是现在,当我将我的 xcode 更新到 xcode 7 并且它从 swift 更改为 swift 2 时,它不再工作了。我坚持这一点,任何帮助将不胜感激。

这是对 api 的请求:

func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, name : String){
    var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(44.942149),\(26.02453)&radius=\(radius)&types=food"
    urlString += "&key=\(apiServerKey)"

    urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

    if placesTask.taskIdentifier > 0 && placesTask.state == .Running {
        placesTask.cancel()
    }

    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    do{

        //******************Here's the line that displays error

        placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {
            (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            //var placesArray = [GooglePlace]()
            do {
                if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? NSDictionary {
                    if let results = json["results"] as? NSArray {
                        for rawPlace:AnyObject in results {
                            print(rawPlace)
                            var placename = ""
                            if let name = rawPlace["name"] as? NSString {
                                placename = name as String
                            }
                        }
                    }
                }
            } catch {
                //handle error
            }
            /*dispatch_async(dispatch_get_main_queue()) {
            completion(placesArray)
            }*/
        }
    }catch{
    }

    placesTask.resume()
}

我得到的错误:

2015-10-02 16:57:54.586 QR Code[4331:67414] -[NSURLSessionDataTask taskIdentifier]: unrecognized selector sent to instance 0x7fe791c3b840
2015-10-02 16:57:54.591 QR Code[4331:67414] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLSessionDataTask taskIdentifier]: unrecognized selector sent to instance 0x7fe791c3b840'

*** First throw call stack:(
0   CoreFoundation                      0x0000000111a8ef65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000111506deb objc_exception_throw + 48
2   CoreFoundation                      0x0000000111a9758d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x00000001119e4f7a ___forwarding___ + 970
4   CoreFoundation                      0x00000001119e4b28 _CF_forwarding_prep_0 + 120
5   QR Code                             0x000000010f57b3ff _TFC7QR_Code17MapViewController25fetchPlacesNearCoordinatefS0_FTVSC22CLLocationCoordinate2D6radiusSd4nameSS_T_ + 1215
6   QR Code                             0x000000010f57bfd3 _TFC7QR_Code17MapViewController11viewDidLoadfS0_FT_T_ + 1555
7   QR Code                             0x000000010f57c552 _TToFC7QR_Code17MapViewController11viewDidLoadfS0_FT_T_ + 34
8   UIKit                               0x00000001122ae931 -[UIViewController loadViewIfRequired] + 1344
9   UIKit                               0x00000001122b4923 -[UIViewController __viewWillAppear:] + 120
10  UIKit                               0x00000001122e418a -[UINavigationController _startCustomTransition:] + 1177
11  UIKit                               0x00000001122f37c7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
12  UIKit                               0x00000001122f467d -[UINavigationController __viewWillLayoutSubviews] + 57
13  UIKit                               0x000000011248c63d -[UILayoutContainerView layoutSubviews] + 248
14  UIKit                               0x00000001121d411c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
15  QuartzCore                          0x000000010fe7536a -[CALayer layoutSublayers] + 146
16  QuartzCore                          0x000000010fe69bd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
17  QuartzCore                          0x000000010fe69a4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
18  QuartzCore                          0x000000010fe5e1d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
19  QuartzCore                          0x000000010fe8b9f0 _ZN2CA11Transaction6commitEv + 508
20  UIKit                               0x000000011211e1b6 _UIApplicationHandleEventQueue + 7183
21  CoreFoundation                      0x00000001119bb0a1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
22  CoreFoundation                      0x00000001119b0fcc __CFRunLoopDoSources0 + 556
23  CoreFoundation                      0x00000001119b0483 __CFRunLoopRun + 867
24  CoreFoundation                      0x00000001119afe98 CFRunLoopRunSpecific + 488
25  GraphicsServices                    0x0000000115770ad2 GSEventRunModal + 161
26  UIKit                               0x0000000112123676 UIApplicationMain + 171
27  QR Code                             0x000000010f5782bd main + 109
28  libdyld.dylib                       0x0000000113dd192d start + 1)
libc++abi.dylib: terminating with uncaught exception of type NSException
4

1 回答 1

0

请不要直接从移动设备调用 Places API Web 服务 - 这会导致外部各方从您的应用程序中窃取您的 API 密钥。请遵循Ray Wenderlich 团队在本教程中制定的代理服务器模式。

于 2015-10-08T00:12:57.533 回答