I want to open a web page through a link. I'm using the address as a global variable. However, when I run a global function, an error occurs. What is the reason?
GlobalValue.swift
import Foundation
struct Global {
let apiAddress = "http://11.111.111.111:11111/"
let LinkURL: String
let LinkSecond : String
init()
{
agreeLinkURL = "User?type=webview"
agreeLinkSecond = "User2?type=webview"
}
func getURL() -> String {
return apiAddress + LinkURL
}
func getURLSecond() -> String {
return apiAddress + LinkSecond
}
}
Usage
let dataUrl = Global()
class ViewController: UIViewController {
...
@IBAction func DetailFuc(_ sender: Any) {
let detailUrl = dataUrl.getURL() // get error
print("***********************************************")
print(detailUrl)
print("***********************************************")
if let appURL = URL(string: detailUrl) {
UIApplication.shared.open(appURL) { success in
if success {
print("The URL was delivered successfully.")
} else {
print("The URL failed to open.")
}
}
} else {
print("Invalid URL specified.")
}
}
Error is:
2019-09-05 15:03:06.094723+0900 testap[24311:376425] -[testap.ViewController Detailfuc:]: unrecognized selector sent to instance 0x7ff27fe2d480 2019-09-05 15:03:06.102115+0900 testap[24311:376425] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[testap.ViewController Detailfuc:]: unrecognized selector sent to instance 0x7ff27fe2d480' *** First throw call stack: ( 0 CoreFoundation 0x0000000105a618db __exceptionPreprocess + 331 1 libobjc.A.dylib 0x0000000103fb8ac5 objc_exception_throw + 48 2 CoreFoundation 0x0000000105a7fc94 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 UIKitCore 0x000000010f0bb235 -[UIResponder doesNotRecognizeSelector:] + 287 4 CoreFoundation 0x0000000105a66623 ___forwarding___ + 1443 5 CoreFoundation 0x0000000105a68418 _CF_forwarding_prep_0 + 120 6 UIKitCore 0x000000010f090624 -[UIApplication sendAction:to:from:forEvent:] + 83 7 UIKitCore 0x000000010eae58d5 -[UIControl sendAction:to:forEvent:] + 67 8 UIKitCore 0x000000010eae5bf2 -[UIControl _sendActionsForEvents:withEvent:] + 450 9 UIKitCore 0x000000010eae4ba8 -[UIControl touchesEnded:withEvent:] + 583 10 UIKitCore 0x000000010f0c94e6 -[UIWindow _sendTouchesForEvent:] + 2547 11 UIKitCore 0x000000010f0cabca -[UIWindow sendEvent:] + 4079 12 UIKitCore 0x000000010f0a930e -[UIApplication sendEvent:] + 356 13 UIKitCore 0x000000010f1792b3 __dispatchPreprocessedEventFromEventQueue + 3232 14 UIKitCore 0x000000010f17bbd9 __handleEventQueueInternal + 5911 15 CoreFoundation 0x00000001059c8db1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 16 CoreFoundation 0x00000001059c8633 __CFRunLoopDoSources0 + 243 17 CoreFoundation 0x00000001059c2cef __CFRunLoopRun + 1231 18 CoreFoundation 0x00000001059c24d2 CFRunLoopRunSpecific + 626 19 GraphicsServices 0x000000010a69f2fe GSEventRunModal + 65 20 UIKitCore 0x000000010f08efc2 UIApplicationMain + 140 21 testap 0x00000001035c31eb main + 75 22 libdyld.dylib 0x0000000107dcf541 start + 1 ) l ibc++abi.dylib: terminating with uncaught exception of type NSException
Something must have been wrong when I set up a global variable, but I don't know what went wrong. Please let me know how to solve this. Is the method of calling a webpage the right way?