1
votes

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?

3

3 Answers

0
votes

As you can see here:

reason: '-[testap.ViewController Detailfuc:]: unrecognized selector sent to instance 0x7ff27fe2d480'

That's not the issue. The reason of the crash is you renamed Detailfuc to DetailFuc with capital F. So it can not find the original function with lowercase f and crashed.

You should disconnect the wrong IBAction from the button eachtime you change the target.

You can right click on the button to see the connections and then click on x for the wrong connection with lowercase f

0
votes

Have you been using an old version of Xcode eight beta?

When I tried the same in Xcode eight beta 6:

I drag over to the category file and say develop an action with sender UIButton as well as title it' doPrint'.

Xcode generated this:

@IBAction func doPrint(_ sender: AnyObject) {  
}  

When you wish to make a way with Objective C selector "doPrint:", you have to place' _' before the very first parameter, in Swift three. more info here

0
votes

(reason: '-[testap.ViewController Detailfuc:]: unrecognized selector)

when you connect the outlet object in storyboard to it's code in the view controller, you can't modify the name any time before you disconnect it by right clicking on the object in story board and press on the x button that link the object with the code, and after modifying the name from code link it again.