var v = 0; var result = !v?.toString();这里result的结果是什么,是如何得出的结果
时间: 2024-03-11 18:49:30 浏览: 8
根据运算符优先级,成员访问(.)的优先级高于条件运算符(?:),而条件运算符的优先级又高于逻辑非(!)运算符。因此,上述代码实际上等价于以下代码:
```
var v = 0;
var result = (!v) ? undefined : v.toString();
```
因为0是一个假值,所以!v的结果为true。因此,条件运算符返回undefined作为result的值。需要注意的是,虽然toString()方法可以将数字转换为字符串,但因为result的值是undefined,所以调用toString()方法会引发TypeError错误。
相关问题
QString script = "var iframe = document.activeElement;" "var iframeDoc = iframe.contentWindow.document;" "var attrValue = iframeDoc.activeElement.Name;" "attrValue;"; //page()->runJavaScript(script, [&eTagName2](const QVariant &v) { eTagName2 = v.toString(); }); page()->runJavaScript(script, [this](const QVariant& result) { QString attrValue = result.toString(); // ????????? });返回invalid值
问题可能在于您正在尝试访问 iframe 内部的 `activeElement` 属性,但是 iframe 中并没有元素被选中。这会导致 JavaScript 返回 `undefined` 或 `null`,从而导致 QVariant 转换为 QString 时得到 `invalid` 值。
您可以在运行 JavaScript 之前检查 iframe 是否具有焦点元素,例如:
```cpp
page()->runJavaScript("var iframe = document.activeElement;"
"var iframeDoc = iframe.contentWindow.document;"
"var activeElem = iframeDoc.activeElement;"
"activeElem ? activeElem.Name : null;",
[this](const QVariant& result) {
QString attrValue = result.toString();
if (attrValue == "null") {
// 没有选中元素
} else {
// 选中元素的 Name 属性值为 attrValue
}
});
```
如果 iframe 中有选中元素,将返回该元素的 `Name` 属性值。否则,返回 `null`。
unity2021下载文件,支持断点续传,支持MD5,支持进度条显示 webRequest.result != UnityWebRequest.Result.Success
你可以使用UnityWebRequest来实现这个功能。UnityWebRequest支持断点续传、MD5校验和进度条显示,并且可以通过检查webRequest.result属性来判断请求是否成功。
以下是一个示例代码,演示如何下载文件并显示下载进度:
```csharp
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class FileDownloader : MonoBehaviour
{
public string fileUrl;
public string savePath;
private UnityWebRequest webRequest;
public IEnumerator DownloadFile()
{
webRequest = UnityWebRequest.Get(fileUrl);
webRequest.downloadHandler = new DownloadHandlerFile(savePath);
// 获取已下载的字节数
long startPos = GetExistingFileSize(savePath);
if (startPos > 0)
{
// 设置断点续传的起始位置
webRequest.SetRequestHeader("Range", "bytes=" + startPos + "-");
}
webRequest.SendWebRequest();
while (!webRequest.isDone)
{
float progress = webRequest.downloadProgress;
Debug.Log("Download progress: " + progress);
yield return null;
}
if (webRequest.result == UnityWebRequest.Result.ConnectionError ||
webRequest.result == UnityWebRequest.Result.DataProcessingError ||
webRequest.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError("Download failed: " + webRequest.error);
}
else
{
Debug.Log("Download complete!");
// 验证文件MD5
string md5 = ComputeMD5(savePath);
if (md5 != GetFileMD5(fileUrl))
{
Debug.LogError("MD5 check failed!");
}
}
}
// 获取已下载的字节数
private long GetExistingFileSize(string filePath)
{
if (File.Exists(filePath))
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length;
}
return 0;
}
// 计算文件的MD5值
private string ComputeMD5(string filePath)
{
using (var md5 = System.Security.Cryptography.MD5.Create())
{
using (var stream = File.OpenRead(filePath))
{
byte[] hash = md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}
}
// 获取文件的MD5值
private string GetFileMD5(string fileUrl)
{
using (var webClient = new WebClient())
{
byte[] data = webClient.DownloadData(fileUrl);
using (var md5 = System.Security.Cryptography.MD5.Create())
{
byte[] hash = md5.ComputeHash(data);
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}
}
}
```
在上面的示例中,我们通过UnityWebRequest.Get方法创建一个下载请求,设置下载进度和下载完成后保存文件的路径。我们使用SetRequestHeader方法设置断点续传的起始位置,使用ComputeMD5方法计算下载完成后文件的MD5值,并使用GetFileMD5方法获取文件在服务器上的MD5值,最后对比两个MD5值是否相同。
注意:在下载文件之前,需要确保设置了网络权限。在Android平台上,需要在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.INTERNET" />
```
这样就可以通过UnityWebRequest来下载文件,并支持断点续传、MD5校验和进度条显示了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)