1

sorry I couldn't get the code snippet to work properly. Didn't know how to get it to recognise Swift.

My issue is that I have a class called "Crewman" and I want to generate 25 objects based off of this class within the function "generateCrew". (I am currently trying this in the testing file within Xcode) I don't know how to fix it so that they are all named differently. If I give the objects a specific name then it will just overwrite the first one the next 24 times I run the function.

I am very new to iOS/Swift so would love help... especially as dumbed down for me as possible hahahaha thanks! :D

//This is the class constructor, if thats the right word...

class Crewman {
    var name: String!
    var navSkill: Int!      // #/10
    var combatSkill: Int!   // #/10
    var morale: Int!        // #/10
    var loyalty: Int!       // #/10
}


//This is where I am trying to create the objects within a function 

    var crewNames =
    ["Chance", "Bruno", "Toby", "Damien", "Rafael", "Domenic", "Weston", "Edison", "Chris", "Grant", "Cortez", "Darren", "Nicolas", "Everette", "Jason", "Vicente", "Noe", "Ricky", "Ivory", "Thomas", "Guillermo", "Isreal", "Ben", "Winfred", "Lincoln", "Kareem", "Antwan", "Greg", "Lawrence", "Corey", "Todd", "Jeffery", "Rayford", "Julio", "Manual", "Chi", "Bradford", "Devon", "Bryan", "Andreas", "Don", "Erik", "Bobbie", "Tyree", "Felipe", "Clifton", "Carrol", "Kasey", "Cliff", "Jack"]

    //Lincoln is no.25

    var crewmenAll = [Crewman]()

    func generateCrew()
    {
        for i in 1...25
        {
            var crewNames[i]:Crewman!
            crewmenAll.append(nameOfCrew)
            print(crewmenAll)
        }
    }

    override func setUp() {

        for i in 1...25
        {
            let x = Int.random(in: 0..<50)
            let name = crewNames[x]

            let navInt = Int.random(in: 3...7)
            let combatInt = Int.random(in: 3...7)
            let moraleInt = Int.random(in: 3...7)


            crewmenAll[i] = Crewman(name: name, navSkill: navInt, combatSkill: combatInt, morale: moraleInt, loyalty: 5)

        }


2 Answers 2

1

First of all you should change your class because there is no point in force unwrapping (!) normal properties like you do but instead add a proper init method

class Crewman {
    var name: String
    var navSkill: Int      
    var combatSkill: Int
    var morale: Int     
    var loyalty: Int    

    init(name: String, navSkill: Int, combatSkill: Int, morale: Int, loyalty: Int) {
        self.name = name

        self.navSkill = navSkill
        self.combatSkill = combatSkill
        self.morale = morale
        self.loyalty = loyalty
    }
}

I don't understand why you have both the functions generateCrew and setUp since they look similar so I just keep setUp.

In setUp I have kept most of the code and make use of the new init method and then I add each new Crewman object to the end of the array buy using append.

To get random names I shuffle the names array which puts them in a random order and then I can just pick them from that array using the index i. Also be aware that the index of an array starts at 0 not 1 so I changed the for loop a bit

var crewmenAll = [Crewman]()
var crewmen = [String: Crewman]()

func setUp() {
    let randomOrder = crewNames.shuffled()

    for i in 0..<25 {
        let name = randomOrder[i]

        let navInt = Int.random(in: 3...7)
        let combatInt = Int.random(in: 3...7)
        let moraleInt = Int.random(in: 3...7)

        crewmenAll.append(Crewman(name: name, navSkill: navInt, combatSkill: combatInt, morale: moraleInt, loyalty: 5))
    }
    crewmen = crewmenAll.reduce(into: [:]) {$0[$1.name] = $1 }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the help, this seems to have solved the errors on my page. However this doesn't assign the Objects names. I'm not talking about the internal name variable but as in if I wanted to call the object without using crewmenAll[0] or whichever number I wouldn't know how to call them. Or would I need to? Sorry if I'm being dense haha
@FrazerMacRostie If I understand you correctly you want a dictionary, see my update answer
@FrazerMacRostie any feedback?
0

You need

// use struct , remove ! and have a free init
struct Crewman {
    var name: String
    var navSkill,combatSkill,morale,loyalty: Int   
}

letcrewNames = ["Chance", "Bruno", "Toby", "Damien", "Rafael", "Domenic", "Weston", "Edison", "Chris", "Grant", "Cortez", "Darren", "Nicolas", "Everette", "Jason", "Vicente", "Noe", "Ricky", "Ivory", "Thomas", "Guillermo", "Isreal", "Ben", "Winfred", "Lincoln", "Kareem", "Antwan", "Greg", "Lawrence", "Corey", "Todd", "Jeffery", "Rayford", "Julio", "Manual", "Chi", "Bradford", "Devon", "Bryan", "Andreas", "Don", "Erik", "Bobbie", "Tyree", "Felipe", "Clifton", "Carrol", "Kasey", "Cliff", "Jack"] 
var crewmenAll = [Crewman]()  

func generateCrew() {  
    var currentNames = [String]()  // hold temporary names 
    for i in 1...25 { 
        // generate unique random name 
        var name = crewNames[ Int.random(in: 0..<50)] 
        while currentNames.contains(name) { 
           name = crewNames[Int.random(in: 0..<50)]
        }
        currntNames.append(name)
        let navInt = Int.random(in: 3...7)
        let combatInt = Int.random(in: 3...7)
        let moraleInt = Int.random(in: 3...7)  
        crewmenAll.append(Crewman(name: name, navSkill: navInt, combatSkill: combatInt, morale: moraleInt, loyalty: 5))

    }
}
override func viewDidLoad() {   
   super.viewDidLoad()
   generateCrew() 
}

you don't have to create nil objects inside the array first and then access them with [i] use append directly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.