gpt4 book ai didi

Swift 结构项值不随函数改变

转载 作者:行者123 更新时间:2023-11-30 11:24:27 25 4
gpt4 key购买 nike

我有下面的代码,但即使将函数 newMethod(newName: String) 添加到类中,变量的 username 值也不会更改用户。 Profile 结构是在不同的类中定义的。我究竟做错了什么?注意:我正在使用 LBTAComponents pod。

import LBTAComponents

class ProfileDatasource: Datasource {
let placeholderIV: UIImageView = {
let imageView = UIImageView()
return imageView
}()

var user = Profile(username: String(), school: "fat", rating: 48, friends: 73, posts: 98, docs: 3, bioText: "Hi I'm a sophomore from Cyprus Bay and can help with most fields relating to high school math.", profileImage: UIImageView(), bgImage: UIImageView())

func newThing(newName: String) {
let newName = "Hi"
print(newName)
ProfileDatasource().user.username = newName
}
}

最佳答案

这一行

ProfileDatasource().user.username = newName

主要通过这个 ProfileDatasource() 创建一个新的 var ,你需要

self.user.username = newName

关于Swift 结构项值不随函数改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50918794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com