![](/img/trans.png)
[英]How to access and refresh a UITableView from another class in Swift
[英]How to access a String from another class in swift?
我有兩個類,一個叫做ViewController
,看起來像這樣
import UIKit
class ViewController: UIViewController {
let demoURLs = DemoURLs
let ImgURL = NSURL(string: demoURLs.photoURL)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
demoURLs.swift
文件如下所示
import Foundation
struct DemoURLs{
var photoURL = " xyz " }
但是我得到一個錯誤。 我了解我的方法不正確。 什么是訪問此字符串的正確方法,以便可以在NSString
使用它? 我的最終目的是為了虛增imageView
與該URL的圖像。
您實例化結構的方法不正確。 更改
let demoURLs = DemoURLs
至
let demoURLs = DemoURLs() // the () calls init()
並且您將能夠訪問該結構的成員。
struct DemoURLs {
static var photoURL = " xyz "
}
應該做的工作。
要從任何類閱讀,請使用:
let myString = DemoURLs.photoURL
來寫:
DemoURLs.photoURL = "new name for xyz"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.