【发布时间】:2018-12-01 02:47:46
【问题描述】:
我是新手 我试着解释我的问题是什么:
我有一个名为 feedmodel.swift 的 swift 文件:
import Foundation
protocol FeedmodelProtocol: class {
func itemsDownloaded(items: NSArray)
}
class Feedmodel: NSObject, URLSessionDataDelegate {
weak var delegate: FeedmodelProtocol!
func downloadItems() {
let myUrl = URL(string: "http://example.net/stock_service4.php");
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
var request = URLRequest(url:myUrl!)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "Latitudee=19.4&Longitudee=-99.1";
request.httpBody = postString.data(using: .utf8)
let task = defaultSession.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
self.parseJSON(data)
}
task.resume()
}
我有一个名为 NeuerBeitragViewController 的 swift 文件:
import UIKit
import CoreLocation
class NeuerBeitragViewController: UIViewController,CLLocationManagerDelegate {
@IBOutlet weak var Tankstelle: UITextField!
@IBOutlet weak var Kraftstoff1: UITextField!
@IBOutlet weak var Preis1: UITextField!
@IBOutlet weak var Kraftstoff2: UITextField!
@IBOutlet weak var Preis2: UITextField!
@IBOutlet weak var Notiz: UITextField!
@IBOutlet weak var Longitude: UITextField!
@IBOutlet weak var Latitude: UITextField!
var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
startLocation = nil
}
@IBAction func startWhenInUse(_ sender: Any) {
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
let latestLocation: CLLocation = locations[locations.count - 1]
Latitude.text = String(format: "%.4f",
latestLocation.coordinate.latitude)
Longitude.text = String(format: "%.4f",
latestLocation.coordinate.longitude)
}
func locationManager(_ manager: CLLocationManager,
didFailWithError error: Error) {
print(error.localizedDescription)
}
In my NeuerBeitragViewController.Swift
I have this line:
Latitude.text = String(format: "%.4f",
latestLocation.coordinate.latitude)
Longitude.text = String(format: "%.4f",
latestLocation.coordinate.longitude)
我想获取 Latitude.text 和 Longitude.text 的值 在我的 Feedmodel.swift 中的这一行中:
let postString = "firstName=19.4&lastName=-99.1";
这样我就可以在这里做:
let postString = "firstName=\(Latitude.text)&lastName=\Longitude.text";
希望你们理解我的需要并能提供帮助。
谢谢!
【问题讨论】:
-
调用时必须将两个文本字段的
text属性传递给downloadItems。无论如何,您实际上在哪里称呼它? -
我不知道你是什么意思
标签: ios swift request pass-data