【发布时间】:2020-01-07 23:58:31
【问题描述】:
我想通过链接打开网页。我将地址用作全局变量。但是,当我运行全局函数时,会发生错误。是什么原因?
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
}
}
用法
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.")
}
}
错误是:
2019-09-05 15:03:06.094723+0900 testap[24311:376425]-[testap.ViewController Detailfuc:]:无法识别的选择器发送到实例 0x7ff27fe2d480 2019-09-05 15:03:06.102115+0900 testap[24311:376425] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[testap.ViewController Detailfuc:]:无法识别的选择器发送到实例 0x7ff27fe2d480” *** 首先抛出调用堆栈: ( 0 核心基础 0x0000000105a618db __exceptionPreprocess + 331 1 libobjc.A.dylib 0x0000000103fb8ac5 objc_exception_throw + 48 2核心基础0x0000000105a7fc94-[NSObject(NSObject)不识别选择器:]+132 3 UIKitCore 0x000000010f0bb235-[UIResponder doesNotRecognizeSelector:] + 287 4 核心基础 0x0000000105a66623 ___转发___ + 1443 5 核心基础 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 发送事件:] + 4079 12 UIKitCore 0x000000010f0a930e -[UIApplication sendEvent:] + 356 13 UIKitCore 0x000000010f1792b3 __dispatchPreprocessedEventFromEventQueue + 3232 14 UIKitCore 0x000000010f17bbd9 __handleEventQueueInternal + 5911 15 核心基础 0x00000001059c8db1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 16 核心基础 0x00000001059c8633 __CFRunLoopDoSources0 + 243 17 核心基础 0x00000001059c2cef __CFRunLoopRun + 1231 18 核心基础 0x00000001059c24d2 CFRunLoopRunSpecific + 626 19 图形服务 0x000000010a69f2fe GSEventRunModal + 65 20 UIKitCore 0x000000010f08efc2 UIApplicationMain + 140 21 测试 0x00000001035c31eb 主要 + 75 22 libdyld.dylib 0x0000000107dcf541 开始 + 1 ) l ibc++abi.dylib:以 NSException 类型的未捕获异常终止我设置全局变量时一定有问题,但我不知道出了什么问题。请让我知道如何解决这个问题。调用网页的方法对吗?
【问题讨论】: