I want to create a function that returns both text and integer. And I want to have an if loop inside this function. If the number of steps of the 1st user is more than the 2nd, the step difference of these two users and a text that says you are ahead should be returned.If the 2nd user has more steps than the 1st user, I want it to return a text saying that there is a difference in the number of steps between them and you are behind. In my hand like this.
struct ActiveDuel{
let state: String
let user1StepCount: Int
let user2StepCount: Int
let user1Name: String
let user2Name: String
let user1Phote: String
let user2Phote: String
let stepDif: Int
let goldAmount: Int
let time: Int
let lastUpdateTime: Int
let startTime: Int
let docId: String
}
I created the code like this but it looks wrong and also doesn't contain text.
func DıfferenceStep(user1StepCount:Int , user2StepCount: Int ) -> Int{
if user1StepCount > user2StepCount {
let DıfferenceStepFunc = user1StepCount - user2StepCount
}else if user2StepCount > user1StepCount {
let DıfferenceFalseStep = user2StepCount - user1StepCount
}
}
I don't know how to use if statement inside functions and besides that I want it to return both integer and text.I would be glad if you help me in this regard.
CodePudding user response:
Here is an example of your struct with a func inside it. You create the struct and then call the func on the struct instance.
struct ActiveDuel
{
var user1StepCount: Int = 0
var user2StepCount: Int = 0
var user1Name: String?
var user2Name: String?
func stepsDifference() -> Int
{
return abs(user1StepCount - user2StepCount)
}
}
let duel = ActiveDuel(user1StepCount: 30, user2StepCount: 5, user1Name: "Bill", user2Name: "Ted")
print(duel.stepsDifference())
I would consider making a Duelist struct and store each duelist's individual information in that instead of in the general ActiveDuel struct. Then you could have a Duel struct that had an active Bool property (true/false), and any number of Duelists (you could limit it to two).
CodePudding user response:
Depending on how you use this it would be better to create 2 different computed properties in your ActiveDuell
struct. One returning the difference in points and one returning a string
that gives the information you want.
struct ActiveDuel{
let state: String
let user1StepCount: Int
let user2StepCount: Int
let user1Name: String
let user2Name: String
let user1Phote: String
let user2Phote: String
let stepDif: Int
let goldAmount: Int
let time: Int
let lastUpdateTime: Int
let startTime: Int
let docId: String
var pointDifference: Int{
// calculate the difference and make the result positive
abs(user1StepCount - user2StepCount)
}
var standings: String{
// if the difference is 0 the game is tied
if user1StepCount == user2StepCount{
return "you are tied"
} else {
// put the name of the leader and the points they lead in to the string
return "\(user1StepCount > user2StepCount ? user1Name : user2Name), you lead by \(pointDifference) points"
}
}
}
you can use it like this:
let activeDuel = ActiveDuel(user1StepCount: 12, user2StepCount: 10, user1Name: "Player1",.....
print(activeDuel.standings) // prints "Player1, you lead by 2 points