Ios 计算财产还是存储财产?

Ios 计算财产还是存储财产?,ios,swift,properties,Ios,Swift,Properties,所以,我正在将我的Android应用程序移植到iOS上,我的脚被Swift(3)弄湿了 我理解计算属性的概念,我正在尽可能多地使用它们,但这让我想到,也许我做得太多了,这会对我的应用程序的性能产生影响 因此,我的设想是: 我有一个有两个字符串的类,其中一个是我的用户名,这是我的应用程序,另一个是对手的用户名。我的代码是这样的: class Game { let player: String let opponent: String var iAmPlayer: Bool

所以,我正在将我的Android应用程序移植到iOS上,我的脚被Swift(3)弄湿了

我理解计算属性的概念,我正在尽可能多地使用它们,但这让我想到,也许我做得太多了,这会对我的应用程序的性能产生影响

因此,我的设想是: 我有一个有两个字符串的类,其中一个是我的用户名,这是我的应用程序,另一个是对手的用户名。我的代码是这样的:

class Game {
    let player: String
    let opponent: String
    var iAmPlayer: Bool {
        return player.caseInsensitiveCompare(GlobalUsername) == .orderedSame
    }

    init(player: String, opponent: String) {
        self.player = player
        self.opponent = opponent
    }
}
我经常检查我的iAmPlayer布尔值,看看用户是否启动了游戏,尤其是在订购游戏对象时。在java中,我只是在初始化对象时设置了一个布尔变量。我在想,如果我只是在这里使用存储的属性而不是我正在做的事情,会更好吗?是否优化了计算属性,或者每次尝试访问iAmPlayer var时都会进行计算


附言:一旦游戏对象被创建,玩家和对手不会改变。(因此let)

谢谢对下面问题的其他评论 我使用了
Usermanager.instance.username
而不是
GlobalUsername

1.解决方案:将计算移到初始
  • 赞成:不改变任何其他代码
  • con:仍然有对UserManager的引用
代码:

2.解决方案:为init提供当前用户名
  • 赞成:无隐式输出连接-对象解耦
  • pro:可测试,无需设置Usermanager
  • 缺点:你必须改变游戏初始化的创建
代码:

class Game {
    let playerName: String
    let opponentName: String
    var isCurrentUser: Bool

    init(playerName: String, opponentName: String, currentUserName: String) {
        self.playerName = playerName
        self.opponentName = opponentName
        self.isCurrentUser = playerName.caseInsensitiveCompare(currentUserName) == .orderedSame
    }
}

// Create Game object

Game(playerName: gameData.playerName, 
    opponentName: gameData.opponentName, 
    currentUserName: UserManager.instance.currentUserName)
class Game {
    let playerName: String
    let opponentName: String
    var isCurrentUser: Bool

    init(playerName: String, opponentName: String, isCurrentUser: Bool) {
        self.playerName = playerName
        self.opponentName = opponentName
        self.isCurrentUser = isCurrentUser
    }
}

// Create Game object

var currentUserName = UserManager.instance.currentUserName
var isCurrentUser = dameData.playerName.caseInsensitiveCompare(currentUserName) == .orderedSame

Game(playerName: gameData.playerName, 
    opponentName: gameData.opponentName, 
    isCurrentUser: isCurrentUser)


// Create Game object

var isCurrentUser = UserManager.instance.isCurrentUser(gameData.playerName)

Game(playerName: gameData.playerName, 
    opponentName: gameData.opponentName, 
    isCurrentUser: isCurrentUser)
3.解决方案:将布尔的计算移到创建之外
  • 赞成:无隐式输出连接-对象解耦
  • pro:可测试,无需设置Usermanager
  • pro:在不更改游戏对象的情况下更改“isCurrentUser”的计算逻辑
  • 缺点:你必须改变游戏初始化的创建
代码:

class Game {
    let playerName: String
    let opponentName: String
    var isCurrentUser: Bool

    init(playerName: String, opponentName: String, currentUserName: String) {
        self.playerName = playerName
        self.opponentName = opponentName
        self.isCurrentUser = playerName.caseInsensitiveCompare(currentUserName) == .orderedSame
    }
}

// Create Game object

Game(playerName: gameData.playerName, 
    opponentName: gameData.opponentName, 
    currentUserName: UserManager.instance.currentUserName)
class Game {
    let playerName: String
    let opponentName: String
    var isCurrentUser: Bool

    init(playerName: String, opponentName: String, isCurrentUser: Bool) {
        self.playerName = playerName
        self.opponentName = opponentName
        self.isCurrentUser = isCurrentUser
    }
}

// Create Game object

var currentUserName = UserManager.instance.currentUserName
var isCurrentUser = dameData.playerName.caseInsensitiveCompare(currentUserName) == .orderedSame

Game(playerName: gameData.playerName, 
    opponentName: gameData.opponentName, 
    isCurrentUser: isCurrentUser)


// Create Game object

var isCurrentUser = UserManager.instance.isCurrentUser(gameData.playerName)

Game(playerName: gameData.playerName, 
    opponentName: gameData.opponentName, 
    isCurrentUser: isCurrentUser)

是的,每次引用iAmPlayer时都会进行计算。因此,存储属性的效率会稍微高一点。(我假设不仅
玩家
对手
都不能改变,而且
全局服务器名
也不能改变。)这只能在
玩家
全局服务器名
没有改变的情况下进行优化,而且只有当编译器能够推断出
caseInsensitiveCompare
是引用透明的。当然有可能,但您必须查看生成的代码才能亲自查看。@Rob是的,所有值都不能更改。如果有任何变化,将创建一个新的游戏对象,因为这些对象是从我的服务器获取的。我还有一些其他的类,在这些类中,我对vars做了相同的操作,这是可以改变的。计算属性更适合吗?@Alexander所以你的建议是,我应该为布尔值使用一个存储属性,因为它不能被优化,还是你真的想查看生成的代码?(如果是,怎么做?@LucasP。在您分析应用程序并将其确定为性能问题之前,我将坚持使用您认为最干净、最简单的任何技术。我个人更喜欢存储的财产,但那只是我自己