gpt4 book ai didi

xcode - 在我的 TextView 中可选()

转载 作者:行者123 更新时间:2023-11-30 10:12:40 25 4
gpt4 key购买 nike

对于我的主 Storyboard上的静态标签之一,它打印出“可选(“美国”)。但是,我希望它打印出“美国”。所以我的问题是,我如何得到去掉“可选”部分?我已经尝试过这样做:

if let p = placemarks!.first{
self.addressLabel.text = "\(p.country)"
}

我认为感叹号应该“解开”一些值,对吧?但是,即使我执行 p = placemarks!.first,它也会打印出“Optional("United States")。

下面是我的其余代码,以防您需要一些上下文:

//
// ViewController.swift
// Map Demo Rob 2
//
// Created by Jae Hyun Kim on 8/17/15.
// Copyright © 2015 Jae Hyun Kim. All rights reserved.
//

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var latitudeLabel: UILabel!
@IBOutlet weak var longitudeLabel: UILabel!
@IBOutlet weak var courseLabel: UILabel!
@IBOutlet weak var speedLabel: UILabel!
@IBOutlet weak var altitudeLabel: UILabel!
@IBOutlet weak var addressLabel: UILabel!
var manager: CLLocationManager!

override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)

let userLocation: CLLocation = locations[0]
self.latitudeLabel.text = "\(userLocation.coordinate.latitude)"
self.longitudeLabel.text = "\(userLocation.coordinate.longitude)"
self.courseLabel.text = "\(userLocation.course)"
self.speedLabel.text = "\(userLocation.speed)"
self.altitudeLabel.text = "\(userLocation.altitude)"

CLGeocoder().reverseGeocodeLocation(userLocation, completionHandler: {(placemarks, error) -> Void in
print(userLocation)

if error != nil {
print(error)
return
}
else {
if let p = placemarks?.first{
self.addressLabel.text = "\(p.country)"
}
}
})




}

}

最佳答案

if let p = placemarks!.first{
self.addressLabel.text = "\(p.country)"
}

p.countryOptional<String> 。您还需要将其解开,以便仅输出其内容(如果存在)。

if let country = placemarks?.first?.country {
self.addressLabel.text = country
}

关于xcode - 在我的 TextView 中可选(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32101920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com