我有以下视图,我需要将item内容传递到另一个视图(DetailsEvent.swift),我正在使用NavigationLink. (我使用的是 Xcode 11 GM)
struct Events: View {
@ObservedObject var networkManager = NetworkManager()
var body: some View {
NavigationView{
List(self.networkManager.eventos.events){ item in
NavigationLink(destination: DetailsEvent(item: item)) {
VStack(alignment: .leading) {
HStack {
Image(systemName: "calendar")
.foregroundColor(.gray)
Text(item.start_date)
.font(.subheadline)
.foregroundColor(.gray)
}
Text(item.title)
.font(.headline)
}
}
}
.navigationBarTitle("Events")
}
}
Run Code Online (Sandbox Code Playgroud)
但它给了我以下错误:
传递给不带参数的调用的参数
不传递任何变量:NavigationLink (destination: DetailsEvent ()一切正常。
详情Event.swift
struct DetailsEvent: View {
var body: some View {
Text("here details content")
}
}
Run Code Online (Sandbox Code Playgroud)
我有以下代码允许我从 URL 下载 PDF 文件,它可以正常工作:
class ViewController: UIViewController {
@IBOutlet weak var progressView: UIProgressView!
override func viewDidLoad() {
let _ = DownloadManager.shared.activate()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DownloadManager.shared.onProgress = { (progress) in
OperationQueue.main.addOperation {
self.progressView.progress = progress
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
DownloadManager.shared.onProgress = nil
}
@IBAction func startDownload(_ sender: Any) {
let url = URL(string: "https://d0.awsstatic.com/whitepapers/KMS-Cryptographic-Details.pdf")!
let task = DownloadManager.shared.activate().downloadTask(with: url)
task.resume()
}
}
Run Code Online (Sandbox Code Playgroud)
该文件目前将: file:///Users/cybermac/Library/Developer/CoreSimulator/Devices/CAEC75D0-423A-4FB2-B0D6-9E7CADB190A1/data/Containers/Data/Application/8B5CBFC8-7058-48DB-A1C4-872302A80610/Library/Caches/com.apple.nsurlsessiond/Downloads/com.example.DownloadTaskExample/CFNetworkDownload_Q7OVlf.tmp
我如何保存它/Documents/?
像这样的东西: file:///Users/cybermac/Library/Developer/CoreSimulator/Devices/CAEC75D0-423A-4FB2-B0D6-9E7CADB190A1/data/Containers/Data/Application/64370B29-2C01-470F-AE76-17EF1A7BC918/Documents/ …
我正在VStack显示图像(与json相比,它们将具有不同的大小)
我需要显示它以占据屏幕宽度(vstack的宽度并保持宽高比)并适当调整其大小,同时注意根据屏幕宽度的高度。我尝试了不同的方法,但是我设法正确显示了图像。
我的看法是:
struct ContentView: View {
var body: some View {
VStack {
GeometryReader { geometry in
VStack {
Text("Width: \(geometry.size.width)")
Text("Height: \(geometry.size.height)")
}
.foregroundColor(.white)
}
.padding()
.frame(alignment: .topLeading)
.foregroundColor(Color.white) .background(RoundedRectangle(cornerRadius: 10) .foregroundColor(.blue))
.padding()
GeometryReader { geometry in
VStack {
Image("sample")
.resizable()
//.frame(width: geometry.size.width)
.aspectRatio(contentMode: .fit)
}
.foregroundColor(.white)
}
.frame(alignment: .topLeading)
.foregroundColor(Color.white) .background(RoundedRectangle(cornerRadius: 10) .foregroundColor(.blue))
.padding()
}
.font(.title)
}
}
Run Code Online (Sandbox Code Playgroud)
当我.frame (width: geometry.size.width)通过分配的宽度使用时geometry,该宽度会显示在整个屏幕上,但高度不能保持纵横比。(看起来被压碎了)
如何获取图像的尺寸并找到其比例以用于 .aspectRatio (myratio, contentMode: .fit)
还有另一种正确显示图像的方法,任何建议
在下面的代码中,我正在练习GUARD的使用(本书:OReilly Learning Swift)
guard 2+2 == 4 else {
print("The universe makes no sense")
return // this is mandatory!
}
print("We can continue with our daily lives")
Run Code Online (Sandbox Code Playgroud)
为什么会出现以下代码错误?
error: return invalid outside of a func
还是仅在功能内使用GUARD?
我有一个功能正常的按钮,增量和减量功能正常。但我需要我也可以使用 EdidText 手动输入值。
我的代码如下:
活动.tk
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
internal var minteger = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val plus = findViewById<Button>(R.id.increase)
val minus = findViewById<Button>(R.id.decrease)
plus.setOnClickListener {
increaseInteger(plus)
}
minus.setOnClickListener {
decreaseInteger(minus)
}
}
fun increaseInteger(view: View) {
minteger += 1
display(minteger)
}
fun decreaseInteger(view: View) {
minteger -= 1
display(minteger)
}
private fun display(number: Int) {
val displayInteger = findViewById<View>(
R.id.integer_number) as TextView
displayInteger.text …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
let hello = "Hola"
let indexI = hello.startIndex
let indexF = hello.endIndex
hello[indexI] // "H"
hello[hello.startIndex] // H
hello[hello.index(after: indexI)] // o
hello[indexF] // Fatal error: Can't form a Character from an empty String
Run Code Online (Sandbox Code Playgroud)
但是我hello[indexF]为什么会出错 ?