Я пытаюсь построить конвертер валют, используя Swift 4.
Моя главная проблема остается в том, что мое приложение падает при нажатии кнопки конвертировать. Я застрял с этой проблемой в течение 1 недели, и я немного отчаялся. Я не нашел соответствующих решений в сети и до сих пор не понимаю, где мои ошибки, и это действительно раздражает.
У меня есть сообщение об ошибке:
Резьба 1: сигнал SIGABRT
из этой строки кода:
class AppDelegate: UIResponder, UIApplicationDelegate {
и вот полное сообщение:
2018-08-31 10:43:19.483566+0200 Baluchon[21101:1535106] -[Baluchon.CurrencyChangeViewController convert:]: unrecognized selector sent to instance 0x7fc51ef06680
2018-08-31 10:43:19.491934+0200 Baluchon[21101:1535106] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Baluchon.CurrencyChangeViewController convert:]: unrecognized selector sent to instance 0x7fc51ef06680'
*** First throw call stack:
(
0 CoreFoundation 0x000000010cfa71e6 __exceptionPreprocess + 294
1 libobjc.A.dylib 0x0000000109560031 objc_exception_throw + 48
2 CoreFoundation 0x000000010d028784 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 UIKit 0x000000010a0226db -[UIResponder doesNotRecognizeSelector:] + 295
4 CoreFoundation 0x000000010cf29898 ___forwarding___ + 1432
5 CoreFoundation 0x000000010cf29278 _CF_forwarding_prep_0 + 120
6 UIKit 0x0000000109df53e8 -[UIApplication sendAction:to:from:forEvent:] + 83
7 UIKit 0x0000000109f707a4 -[UIControl sendAction:to:forEvent:] + 67
8 UIKit 0x0000000109f70ac1 -[UIControl _sendActionsForEvents:withEvent:] + 450
9 UIKit 0x0000000109f6fa09 -[UIControl touchesEnded:withEvent:] + 580
10 UIKit 0x0000000109e6a0bf -[UIWindow _sendTouchesForEvent:] + 2729
11 UIKit 0x0000000109e6b7c1 -[UIWindow sendEvent:] + 4086
12 UIKit 0x0000000109e0f310 -[UIApplication sendEvent:] + 352
13 UIKit 0x000000010a7506af __dispatchPreprocessedEventFromEventQueue + 2796
14 UIKit 0x000000010a7532c4 __handleEventQueueInternal + 5949
15 CoreFoundation 0x000000010cf49bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
16 CoreFoundation 0x000000010cf2e4af __CFRunLoopDoSources0 + 271
17 CoreFoundation 0x000000010cf2da6f __CFRunLoopRun + 1263
18 CoreFoundation 0x000000010cf2d30b CFRunLoopRunSpecific + 635
19 GraphicsServices 0x000000010f7daa73 GSEventRunModal + 62
20 UIKit 0x0000000109df4057 UIApplicationMain + 159
21 Baluchon 0x000000010890e0f7 main + 55
22 libdyld.dylib 0x000000010e0c3955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Я следую шаблону MVC, поэтому вот моя модель:
struct Currency: Decodable {
let rates: [String: Double]?
}
class CurrencyChangeService {
var currencies = [String]()
var rate = [Double]()
static var shared = CurrencyChangeService()
private init() {}
private static let currencyChangeUrl = URL(string: "http://data.fixer.io/api/latest?access_key=mykey")!
var task: URLSessionDataTask?
private var currencySession = URLSession(configuration: .default)
init(currencySession: URLSession) {
self.currencySession = currencySession
}
func getCurrencyChange(callback: @escaping (Bool, Currency?) -> Void) {
task?.cancel()
task = currencySession.dataTask(with: CurrencyChangeService.currencyChangeUrl) { data, response, error in
DispatchQueue.main.async {
guard let data = data, error == nil else {
callback(false, nil)
return
}
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
callback(false, nil)
return
}
guard let responseJSON = try? JSONDecoder().decode(Currency.self, from: data) else {
callback(false, nil)
return
}
for (key, value) in responseJSON.rates! {
self.currencies.append(key)
self.rate.append(value)
}
callback(true, responseJSON)
}
}
task?.resume()
}
}
А вот и мой контроллер:
import UIKit
class CurrencyChangeViewController: UIViewController {
//MARK: - Outlets
@IBOutlet weak var myValueTextField: UITextField!
@IBOutlet weak var myCurrencyTextField: UITextField!
@IBOutlet weak var myConvertedValueLabel: UILabel!
@IBOutlet weak var convertButton: UIButton!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
//MARK: - Properties
var selectedCurrency: Double = 0
//MARK: - View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
currencyChange()
createCurrencyPicker()
createNumberPad()
}
//MARK: - Action
@IBAction func convert() {
if myValueTextField.text != "" {
myConvertedValueLabel.text = String(Double(myValueTextField.text!)! * selectedCurrency)
myConvertedValueLabel.text = String(format: "%.2f")
}
}
@IBAction func dismissKeyboard(_ sender: UITapGestureRecognizer) {
myValueTextField.resignFirstResponder()
myCurrencyTextField.resignFirstResponder()
}
//MARK: - Methods
func currencyChange() {
CurrencyChangeService.shared.getCurrencyChange { (success, rate) in
self.toggleActivityIndicator(shown: true)
if success {
self.toggleActivityIndicator(shown: false)
} else {
self.showAlert(title: "Error", message: "Rates download failed!")
}
}
}
func toggleActivityIndicator(shown: Bool) {
activityIndicator.isHidden = !shown
convertButton.isHidden = shown
}
func createNumberPad() {
myValueTextField.keyboardType = UIKeyboardType.numberPad
}
func createCurrencyPicker() {
let currencyPicker = UIPickerView()
currencyPicker.delegate = self
myCurrencyTextField.inputView = currencyPicker
}
}
extension CurrencyChangeViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return CurrencyChangeService.shared.currencies.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
CurrencyChangeService.shared.currencies.sort()
return CurrencyChangeService.shared.currencies[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedCurrency = CurrencyChangeService.shared.rate[row]
myCurrencyTextField.text = CurrencyChangeService.shared.currencies[row]
}
}