我在这里有点麻烦:我有一堂课
class TempC {
func GetData(){
//do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
在 ContentView 中,我想调用该函数,但我无法做到,出现错误...
struct ContentView: View {
var variable : TempC
variable.GetData()
var body: some View {
Text("Hello World")
}
}
Run Code Online (Sandbox Code Playgroud)
或以任何其他方法。我现在如何调用外部函数?
PS:我得到的错误variable.GetData()是:
- 一行中的连续声明必须用“;”分隔
- 预期“(”在州声明的参数列表中
- 函数声明主体中应为“{”
- 实例方法声明中需要“func”关键字
- 'variable()' 的重新声明无效
就好像它期望创建一个新函数而不是获取已经存在的函数。
我内部ArrayList有一个有限数量的对象。我想在列表中查找字段中的特定值,然后进行更改。
我有以下实现。
String searchName = searchedName; //from outside we provide this
Person person = personList.stream()
.filter(personList -> searchName.equals(personList.getName()))
.findFirst()
.orElse(null);
if (person != null){
person.setName(replacement);
}
Run Code Online (Sandbox Code Playgroud)
这段代码可以工作,但是流不是我的专长,我的问题是:我是否可以具有相同的行为,而不必付出额外的代价呢?我觉得“如果”是多余的东西,我想使我的代码更有效。
附带的问题是,如果我们有多个具有相同名称的条目,则有一种使用流的方法,因此,除了“ .findFirst().orElse(null)”之外,我还可以使用哪种方法将其存储在person对象或可能的new对象中对象数组。
因此,我在 Xcode11 中创建了一个新项目,将 AppDelegate 设置为我的新 VC,并将 xxx 场景委托中的代码注释为没有 UIKit 部分:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow()
window?.makeKeyAndVisible()
let controller = MainVC()
window?.rootViewController = controller
return true
}
Run Code Online (Sandbox Code Playgroud)
在我的 UIViewController 中,我想设置背景颜色,
import UIKit
class MainVC : UIViewController {
override func viewDidLoad() {
view.backgroundColor = .red
self.view.backgroundColor = .blue
print("main Screen showing")
ConfigureUI()
setupUI()
}
Run Code Online (Sandbox Code Playgroud)
但结果是模拟器中的黑屏。甚至从其他项目中获取代码也无济于事……我之前在其他 Xcode 版本中已经这样做过,应该可以工作。有任何想法吗?
PS:App进入ViewController,可以在控制台打印,但是黑屏。