0

I want the code to print the name of the variable and not the value. It just does not want to work.

I have not tried much as I have no idea how to do this. I also cannot find much documentation on this online.

import UIKit

var a = 1
var b = 2
var c = 3
var d = 4
var e = 1
var f = 2
var g = 3
var h = 4
var i = 1
var j = 2
var k = 3
var l = 4

var suggestedGroup1 = 16

var classStudents = [a,b,c,d,e,f,g,h,i,j,k,l]
var groupsNumber = 3
var peoplePerGroup = classStudents.count / groupsNumber

var totalClassPoints = a + b + c + d + e + f + g + h + i + j + k + l

var OnePerson = classStudents.randomElement()
var TwoPerson = classStudents.randomElement()
var ThreePerson = classStudents.randomElement()
var FourPerson = classStudents.randomElement()

suggestedGroup1 = OnePerson! + TwoPerson! + ThreePerson! + FourPerson!

while suggestedGroup1 != 10 {
    OnePerson = classStudents.randomElement()
    TwoPerson = classStudents.randomElement()
    ThreePerson = classStudents.randomElement()
    FourPerson = classStudents.randomElement()
    suggestedGroup1 = OnePerson! + TwoPerson! + ThreePerson! + FourPerson!

}

if suggestedGroup1 == 10 {
    print ("Group 1 is made up of", OnePerson!, ",", TwoPerson!, ",", ThreePerson!, "and", FourPerson!)
}

When it prints ("Group 1 is made up of", OnePerson!, ",", TwoPerson!, ",", ThreePerson!, "and", FourPerson!), I want the name of the person and not their "value".

  • Variable names only have meaning at compile-time, at run-time they are pointers. If you want to have "name"s, then define a struct that has a value and a name properties – ielyamani Aug 18 '19 at 09:48
  • How do i do that? I am fairly new to swift. – SAHNI BHVYA student Aug 18 '19 at 10:05
  • Should look here: https://stackoverflow.com/questions/26005654/get-a-swift-variables-actual-name-as-string. And see if use of reflecting could be a solution. – claude31 Aug 18 '19 at 11:49

1 Answers1

1

You need a separate approach for you, I created a struct and give it is name and values. It is self-explanatory, if you don't understand any part let me know.

import UIKit

struct StudentClass {
    let name: String
    let value: Int
}

var a = StudentClass(name: "a", value: 1)
var b =  StudentClass(name: "b", value: 2)
var c =  StudentClass(name: "c", value: 3)
var d =  StudentClass(name: "d", value: 4)
var e =  StudentClass(name: "e", value: 1)
var f =  StudentClass(name: "f", value: 2)
var g =  StudentClass(name: "g", value: 3)
var h =  StudentClass(name: "h", value: 4)
var i =  StudentClass(name: "i", value: 1)
var j =  StudentClass(name: "j", value: 2)
var k =  StudentClass(name: "k", value: 3)
var l =  StudentClass(name: "l", value: 4)

var suggestedGroup1 = 16

var classStudents = [a,b,c,d,e,f,g,h,i,j,k,l]
var groupsNumber = 3
var peoplePerGroup = classStudents.count / groupsNumber

var totalClassPoints = a.value + b.value + c.value + d.value + e.value + f.value + g.value + h.value + i.value + j.value + k.value + l.value

var OnePerson = classStudents.randomElement()
var TwoPerson = classStudents.randomElement()
var ThreePerson = classStudents.randomElement()
var FourPerson = classStudents.randomElement()

suggestedGroup1 = OnePerson!.value + TwoPerson!.value + ThreePerson!.value + FourPerson!.value

while suggestedGroup1 != 10 {
    OnePerson = classStudents.randomElement()
    TwoPerson = classStudents.randomElement()
    ThreePerson = classStudents.randomElement()
    FourPerson = classStudents.randomElement()
    suggestedGroup1 = OnePerson!.value + TwoPerson!.value + ThreePerson!.value + FourPerson!.value

}

if suggestedGroup1 == 10 {
    print ("Group 1 is made up of", OnePerson!.name, ",", TwoPerson!.name, ",", ThreePerson!.name, "and", FourPerson!.name)
}

Here is the output: -

Group 1 is made up of h , k , f and e
Mussa Charles
  • 4,014
  • 2
  • 29
  • 24
  • `StudentClass` is actually a struct. Simply name it `Student` – ielyamani Aug 18 '19 at 10:09
  • Can I ask how I can go about deleting elements from an array via code? – SAHNI BHVYA student Aug 18 '19 at 10:30
  • You can use Array's method: remove at index for example if you want to remove 'a' you can do so by calling: - classStudents.remove(at: 0) – Mussa Charles Aug 18 '19 at 10:34
  • I recommend you read about Swift Collections on the official Swift documentation here: - https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html – Mussa Charles Aug 18 '19 at 10:35
  • The code you sent only works if I know where the element is. What would I do if I do not know where it is? – SAHNI BHVYA student Aug 18 '19 at 12:54
  • If you don't know the index of the element you want to delete then you can use Array built in method here: - https://developer.apple.com/documentation/swift/array/3017530-removeall You have to go through the reading I suggested above so that you can understand the basics before moving on. Another cool reading, all about arrays is this Apple doc https://developer.apple.com/documentation/swift/array – Mussa Charles Aug 18 '19 at 13:03
  • Just in case the doc is difficult to follow here is an example based on your data. For example let's say you want to delete an item with class "f" but you don't know it's index here is how you would do based on your data. But I highly encourage you to read those docs so that you can understand the basics well. classStudents.removeAll(where: { $0.name == "f" }) – Mussa Charles Aug 18 '19 at 13:11