if URL(string: "https://www.google.com") != nil { import UIKit UIApplication.shared.open(url, options: [:]) { success in if success { print("The URL was delivered successfully.") } else { print("The URL failed to open.") } } } }报错Declaration is only valid at file scope
时间: 2023-04-03 22:04:16 浏览: 62
这个错误的意思是说,这个声明(代码块)只能在文件范围内有效。这通常是由于代码块被放在了一个错误的位置引起的,比如在函数内部或者其他语句块内部。
要解决这个问题,应该将代码块移到文件范围内,或者把它放到合适的函数内。如果你想在函数内部打开一个 URL,可以把这个代码块放到函数内部,并且确保函数已经被声明。
希望这能帮到你!
相关问题
openapp.jdmobile://virtual?params={"category":"jump","des":"m","url":"https://u.jd.com/nQo1Y5A","union_open":"union_cps"}
这个链接"openapp.jdmobile://virtual?params={"category":"jump","des":"m","url":"https://u.jd.com/nQo1Y5A","union_open":"union_cps"}"是用来在京东APP中打开指定商品的。
具体使用的代码如下:
```swift
static func goJD(_ url : String){
KeplerApiManager.sharedKPService()?.openKeplerPage(withURL: url, userInfo: nil, successCallback: {
debugPrint("open jd app success")
}, failedCallback: { code, msg in
debugPrint("open jd error code: ", code)
otherGoJD(url)
})
}
static func otherGoJD(_ url : String) {
let jdDict = ["category":"jump", "des":"getCoupon", "url":url ]
guard let jdStr = jdDict.formatJSON() else { return }
let urlStr = "openapp.jdmobile://virtual?params=\(jdStr)"
let utf8Str = urlStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
var openSuccess = false
if let jumpURL = URL(string: utf8Str ?? "") {
let isCanOpen = UIApplication.shared.canOpenURL(jumpURL)
if isCanOpen {
UIApplication.shared.open(jumpURL, options: [:], completionHandler: nil)
openSuccess = true
}
}
if (openSuccess == false) {
UIApplication.shared.open(URL(string: url)!, options: [:], completionHandler: nil)
}
}
```
通过调用goJD方法,传入京东商品的URL,即可在京东APP中打开该商品页面。如果无法打开京东APP,则会在浏览器中打开该商品链接。
对于京东的URL Scheme,可以在网上搜索到具体的参数传递方式。
https://github.com/extrame/xls读取
要使用github.com/extrame/xls包读取xls格式的文件,您可以按照以下步骤进行操作:
1. 首先,在您的Go项目中导入extrame/xls包。您可以使用以下命令获取该包:
```
go get github.com/extrame/xls
```
2. 在您的Go代码中引入extrame/xls包:
```go
import (
"github.com/extrame/xls"
)
```
3. 打开并解析您的xls文件:
```go
xlFile, err := xls.Open("your_file.xls", "utf-8")
if err != nil {
fmt.Println(err)
return
}
```
4. 获取工作表,并遍历读取其中的数据:
```go
if sheet1 := xlFile.GetSheet(0); sheet1 != nil {
for i := 0; i <= int(sheet1.MaxRow); i++ {
if row := sheet1.Row(i); row != nil {
for j := 0; j < row.LastCol(); j++ {
if cell := row.Col(j); cell != nil {
fmt.Println("Cell value:", cell.String())
}
}
}
}
}
```
以上是一个简单的示例,演示了如何使用extrame/xls包打开和读取xls文件中的数据。您可以根据需要进行修改和扩展。请注意,extrame/xls包还提供了许多其他功能,如写入、修改和操作Excel文件的功能。您可以查看extrame/xls的文档以了解更多详细信息:https://pkg.go.dev/github.com/extrame/xls